@dorianb/logger-js v0.7.6
Logger library
logger-js is a npm logger library for NodeJS
This logger is primarly designed for a backend usage, to handle the logs of a node server, or for a bot. It's lighter and easier to use than other libraries and does not require any configuration (at least for a small/medium project). It's particulary fast and require almost no running time.
The logger will write the logs by default in the logs/ directory to the root of your project. You can change the location and the name of this folder. The default log file is logs.log.
This project is part of the vener.fr project, to collect the errors and different information of the server (express).
Installation
To install the package, just run :
npm install --save @dorianb/logger-jsThen in the .js file :
const Logger = require('@dorianb/logger-js')
Logger.info('Server is starting on port 9000')
Logger.debug('172 clients are currently connected')
Logger.warn('Unsafe call from client @7655671')
Logger.error('Socket &757@127.0.0.1 doesn\'t exist')
Logger.fatal('Internet connection lost')Documentation
Classes
Typedefs
Logger
- Logger
- .options ⇒ OptionsObject
- .version ⇒ string
- .levels ⇒ LevelsObject
- .setOptions(opts)
- .log(filename, level, message) ↩︎
- .info(info, [filename]) ↩︎
- .debug(debug, [filename]) ↩︎
- .warn(warning, [filename]) ↩︎
- .error(error, [filename], [opts]) ↩︎
- .fatal(error, [filename]) ↩︎
- .clear([...filename]) ↩︎
- .getLevel(level) ⇒ array
- .addLevel(newLevel) ⇒ array
- .on(event, callback)
Logger.options ⇒ OptionsObject
Getter: Return the options of the logger
Example
const loggerOptions = Logger.optionsLogger.version ⇒ string
Returns: string - the version number of the logger
Getter: Version getter
Example
const version = Logger.versionLogger.levels ⇒ LevelsObject
Getter: Levels object getter - All the levels of the logger
Example
const levels = Logger.levelsLogger.setOptions(opts)
| Param | Type | Description |
|---|---|---|
| opts | OptionsObject | logger default values |
Example
Logger.setOptions({filename: 'production.log'})Logger.log(filename, level, message) ↩︎
Chainable
| Param | Type | Description |
|---|---|---|
| filename | string | file where the log is written |
| level | number | string | level of the log |
| message | string | content of the log |
Example
Logger.log('network.log', 'WARN', 'Socket disconnected')
Logger.log('network.log', 2, 'Socket disconnected')
// --> [10-06-2020 06:43:51] - WARN - Socket disconnectedLogger.info(info, filename) ↩︎
Chainable
| Param | Type | Default | Description |
|---|---|---|---|
| info | string | content of the log | |
| filename | string | "options.filename" | filename without path |
Example
Logger.info('Server has started')
Logger.info('Server has started', 'server.log')Logger.debug(debug, filename) ↩︎
Chainable
| Param | Type | Default | Description |
|---|---|---|---|
| debug | string | content of the log | |
| filename | string | "options.filename" | filename without path |
Example
Logger.debug(`Client ID = ${clientID}`)
Logger.debug(`Client ID = ${clientID}`, 'clients.log')Logger.warn(warning, filename) ↩︎
Chainable
| Param | Type | Default | Description |
|---|---|---|---|
| warning | string | content of the log | |
| filename | string | "options.filename" | filename without path |
Example
Logger.warn(`Database disconnected`)
Logger.warn(`Database disconnected`, 'connections.log')Logger.error(error, filename, opts) ↩︎
Chainable
| Param | Type | Default | Description |
|---|---|---|---|
| error | string | content of the log | |
| filename | string | "options.filename" | filename without path |
| opts | object | {} | options |
Example
Logger.error(`Connection to 127.0.0.1:2000 refused`)
Logger.error(`Connection to 127.0.0.1:2000 refused`, 'logs.log')Logger.fatal(error, filename) ↩︎
Chainable
| Param | Type | Default | Description |
|---|---|---|---|
| error | string | content of the log | |
| filename | string | "options.filename" | filename without path |
Example
Logger.fatal(`Division by zero`)
Logger.fatal(`Division by zero`, 'big_errors.log')Logger.clear(...filename) ↩︎
Chainable
| Param | Type | Default | Description |
|---|---|---|---|
| ...filename | string | "options.filename" | The filenames of the files to clear or 'all' if all the files should be cleaned |
Example
Logger.clear() // clear the default file (options.filename)
Logger.clear('client.log')
Logger.clear('client.log', 'connections.log', 'logs.log')
Logger.clear('all')Logger.getLevel(level) ⇒ array
Returns: array - index, label
| Param | Type | Description |
|---|---|---|
| level | string | number | the index or the label of the level |
Example
const testLevel = Logger.getLevel('warn') // --> ["2", "WARN"]
const testLevel = Logger.getLevel(2) // --> ["2", "WARN"]Logger.addLevel(newLevel) ⇒ array
Returns: array - the level array : index, label
| Param | Type | Description |
|---|---|---|
| newLevel | string | The label of the new level |
Example
const [importantLevel, importantLabel] = Logger.addLevel('Important')
Logger.log('logs.log', importantLevel, 'Important message which will be display on top of all other levels')Logger.on(event, callback)
| Param | Type | Description | |
|---|---|---|---|
| event | string | 'log' | 'error' |
| callback | function |
Example
Logger.on('log', log => console.log(log))
Logger.on('error', handleErrorsFunction)LevelsObject : Object
A dictionary of the logger levels indexed by priority
Example
levels = {
0: 'INFO',
1: 'DEBUG',
2: 'WARNING',
3: 'ERROR',
4: 'FATAL'
}OptionsObject : Object
Properties
| Name | Type | Default | Description | ||
|---|---|---|---|---|---|
| filename | string | "'logs.log'" | The name of the default log file | ||
| folder | string | "'./logs/'" | The folder where logs files will be located (sorry for the name, couldn't find more descriptive) | ||
| extension | string | "'.log'" | The extension to use for logs files | ||
| useMoment | boolean | false | Use moment-js to format the dates. Allow timezone options but has a performance cost | ||
| timezone | string | "'Europe/Berlin'" | The moment timezone for the date | Full list available at: https://momentjs.com/timezone | |
| console_logs | boolean | false | Use console.log to displays logs instead of writing it in a log file | ||
| displayLevel | string | number | 0 | The level below a log is not displayed | ||
| showPID | boolean | false | Display the PID of the process in the log | ||
| showHostname | boolean | false | Display the hostname in the log | ||
| align | string | "left" | Where should the level be aligned ('left' | 'center' | 'right') |
2020 © Dorian Beauchesne
3 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago