Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
POC, still WIP
  • Loading branch information
stupid-genius committed May 30, 2024
0 parents commit ed0b51d
Show file tree
Hide file tree
Showing 17 changed files with 3,964 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/ci-master.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: CI - master

on:
push:
branches:
- master

jobs:
ci-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Cache node_modules
uses: actions/cache@v4
id: tests
env:
cache-name: tests
with:
path: ./node_modules/
key: tests-${{ hashFiles('./package-lock.json') }}
restore-keys: tests-${{ hashFiles('./package-lock.json') }}
timeout-minutes: 4
- name: Install dependencies
if: steps.tests.outputs.cache-hit != true
run: npm ci
- name: Run tests
run: npm test
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.swp
.idea
node_modules
*.log
*.log.gz
*.pem
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Log-Ng
Provides both a node.js and a browser logger, inspired by loggers like [log4j](https://logging.apache.org/log4j/2.x/) and [Winston](https://github.com/winstonjs/winston) (The node.js logger uses Winston under the hood).
## Browser Logger
The browser logger was inspired by Winston, and aims to provide configurable log levels, allowing logging to be left in place without needing to modify the code to enable or disable logging. It uses `ConsoleTransport` by default, but also comes with `APITransport` for sending logs to a server and `FileTransport` for saving logs to a file (requires support for the File System API).
### Example
#### ConsoleTransport (default)
```javascript
import { Logger } from 'log-ng';
const logger = new Logger('thisFile');
Logger.setLevel('info');
logger.info('Hello, World!');
```
#### APITransport
```javascript
import { Logger, APITransport } from 'log-ng';
Logger.setLevel('info');
Logger.addTransport('api', new APITransport({
url: 'http://localhost:3000/log',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: {
msg: '{{msg}}',
level: '{{level}}',
category: '{{category}}',
timestamp: '{{timestamp}}',
group: '{{group}}',
isTable: '{{isTable}}',
args: '{{args}}'
}
}));
const logger = new Logger('thisFile');
logger.info('Hello, World!');
````
#### FileTransport
```javascript
import { Logger, FileTransport } from 'log-ng';
Logger.setLevel('info');
const fileTransport = new FileTransport({});
await fileTransport.initialize();
Logger.addTransport('file', fileTransport);
const logger = new Logger('thisFile');
logger.info('Hello, World!');
```
## Node.js Logger
The node.js logger is a wrapper around Winston, and provides a simple way to log messages to the console. Additional Winston transports can be added, as needed. The primary purpose of this Logger is to simplify creation of pre-configured Winston child instances of the main singleton instance.
### Example
#### Basic Usage
```javascript
const { Logger } = require('log-ng');
const logger = new Logger(path.basename(__filename));
logger.info('Hello, World!');
```
#### Adding Transports
```javascript
const { Logger } = require('log-ng');
const { File } = require('winston').transports;
Logger.addTransport('file', new File({ filename: 'log.log' }));
const logger = new Logger(path.basename(__filename));
logger.info('Hello, World!');
```

## TODO
- [ ] Implement table and group rendering in the API and File transports
- [ ] Implement console placeholder for API and File transports
- [ ] Implement escaping of {{ and }} in interpolate function
Loading

0 comments on commit ed0b51d

Please sign in to comment.