-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Improved logger using winston package
- Loading branch information
1 parent
a90e986
commit dffb38c
Showing
14 changed files
with
426 additions
and
469 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Configuring the logger | ||
|
||
`Venom Bot` use [winston](https://github.com/winstonjs/winston) package for log management. | ||
|
||
`venom.defaultLogger` is a instance of `winston.createLogger`. | ||
|
||
## Default Log level | ||
|
||
The default log level is `info` | ||
|
||
```javascript | ||
// Supports ES6 | ||
// import * as venom from 'venom-bot'; | ||
const venom = require('venom-bot'); | ||
|
||
// Levels: 'error', 'warn', 'info', 'http', 'verbose', 'debug', 'silly' | ||
// All logs: 'silly' | ||
venom.defaultLogger.level = 'silly'; | ||
|
||
// If you want stop console logging | ||
venom.defaultLogger.transports.forEach((t) => (t.silent = true)); | ||
``` | ||
|
||
## Using a custon logger | ||
|
||
```javascript | ||
// Supports ES6 | ||
// import * as venom from 'venom-bot'; | ||
// import * as winston from 'winston'; | ||
const venom = require('venom-bot'); | ||
const winston = require('winston'); | ||
|
||
const logger = winston.createLogger({ | ||
level: 'info', | ||
format: winston.format.json(), | ||
defaultMeta: { service: 'user-service' }, | ||
transports: [ | ||
// | ||
// - Write all logs with level `error` and below to `error.log` | ||
// - Write all logs with level `info` and below to `combined.log` | ||
// | ||
new winston.transports.File({ filename: 'error.log', level: 'error' }), | ||
new winston.transports.File({ filename: 'combined.log' }), | ||
], | ||
}); | ||
|
||
venom | ||
.create( | ||
'sessionName', | ||
undefined, | ||
undefined, | ||
{ | ||
logger: logger | ||
} | ||
) | ||
.then((client) => { | ||
start(client); | ||
}) | ||
.catch((erro) => { | ||
console.log(erro); | ||
}); | ||
``` | ||
|
||
## Log to file | ||
|
||
By default, venom-bot use the Console transport for logging. | ||
|
||
If you want to save the log to a file, you can configure | ||
using the [winston transport](https://github.com/winstonjs/winston#transports) | ||
|
||
```javascript | ||
// Supports ES6 | ||
// import * as venom from 'venom-bot'; | ||
// import * as winston from 'winston'; | ||
const venom = require('venom-bot'); | ||
const winston = require('winston'); | ||
|
||
// Optional: Remove all default transports | ||
venom.defaultLogger.clear(); // Remove all transports | ||
|
||
// Create a file transport | ||
const files = new winston.transports.File({ filename: 'combined.log' }); | ||
venom.defaultLogger.add(files); // Add file transport | ||
|
||
// Optinal: create a custom console with error level | ||
const console = new winston.transports.Console({ level: 'erro' }); | ||
venom.defaultLogger.add(console); // Add console transport | ||
|
||
// Optinal: Remove the custom transport | ||
venom.defaultLogger.remove(console); // Remove console transport | ||
``` |
Oops, something went wrong.