UNPKG

solive-winston-logger

Version:
200 lines (173 loc) 5.3 kB
# SOLIVE-WINSTON-LOGGER A winston logger that emit logs ## USAGE ### createLogger - `options` - `env` [String] - Should looks like 'production' or 'development' - `onNewLog` [Function] - The function you want to call on each log - RETURNS `Logger` > It is recommended to not run this operation more than once per instance. > Meaning that you can setup your logger once, exporting it > and using it everywhere without having to set it up again. Example: ```javascript const logger = createLogger({ env: 'production', onNewLog: (log) => http.post('http://my-log-manager.com', { data: log }) }); ``` ## Logger ### Catch all logs You can catch all logs that you want to display in order to send them somewhere else Example: ```javascript const logger = createLogger({ env: process.env.NODE_ENV || 'production', onNewLog: (log) => { // log = a log you've just send to the logger // Here you can emit the log wherever you want // using http, sockets, amqp... // In our case, let's use http since it's // the easiest example. http.post( 'http://my-log-manager.com', { data: log }, ) .then(() => { // Here we've send the log somewhere else }) } }) ``` > This works even on sub-instance of a logger (Logger::create). > You don't have to setup the onNewLog callback on each sub-instance ### Logger::create You can create a new instance of the logger using `create` with pre-filled fields - `fields` [Object] (tag|action|details) Example: ```javascript const logger = createLogger({ env: process.env.NODE_ENV || 'production' }) const myMovieLogger = logger.create({ tag: 'movie' }) const create = (name, year) => { const movieCreationLogger = myMovieLogger.create({ action: 'create' }) // ... // do movie stuff if (err) { movieCreationLogger.error('An error occurred', { details: { stack: err.stack, route: '/movies', name: err.name, status: 500, }, }) // An error occurred { // date: '2018-07-31T16:54:02.668Z', // tag: 'movie', // action: 'create', // details: { // stack: {...}, // route: '/movie', // name: 'Error name', // status: 500 } // } } else { movieCreationLogger.info('Movie saved') // Movie saved { // tag: 'movie', // action: 'create', // date: '2018-07-31T16:54:02.668Z' } } } ``` ### Logger::info Informative message are usually used for reporting significant application progress and stages. - `message` [String] - `more` [Object] - `tag` [String] - `action` [String] Example: ```javascript const logger = createLogger({ env: 'production' }); logger.info('let\'s log an info', { tag: 'a simple tag', action: 'a simple action' }); ``` > You can always provide details, it's just optional in case you are using > on of [trace, info, debug] ### Logger::trace ... - `message` [String] Example: ```javascript const logger = createLogger({ env: 'production' }); logger.trace('let\'s log a trace'); ``` ### Logger::debug Used for debugging messages with extended information about application processing. - `message` [String] - `more` [Object] - `tag` [String] - `action` [String] Example: ```javascript const logger = createLogger({ env: 'production' }); logger.debug('let\'s log a debug', { tag: 'a simple tag', action: 'a simple action' }); ``` ### Logger::warn Such messages are reported when something unusual happened that is not critical to process the current operation, but it would be useful to review this situation to decide if it should be resolved. - `message` [String] - `more` [Object] - `tag` [String] - `action` [String] - `details` [Any] Example: ```javascript const logger = createLogger({ env: 'production' }); logger.warn('let\'s log a warn', { tag: 'a simple tag', action: 'a simple action', details: { route: '/a/simple/route' } }); ``` ### Logger::error A serious problem occurred while processing the current operation. - `message` [String] - `more` [Object] - `tag` [String] - `action` [String] - `details` [Object] Example: ```javascript const logger = createLogger({ env: 'production' }); logger.error('let\'s log an error', { tag: 'a simple tag', action: 'a simple action', details: { route: '/a/simple/route', name: 'my_error_name', status: 404, stack: new Error('Content Not Found').stack, }, }); ``` ### Logger::fatal The application is in a critical state and cannot proceed with the execution of the current operation. In this case, the application usually reports and terminates. - `message` [String] - `more` [String] - `tag` [Object] - `action` [Object] - `details` [Object] Example: ```javascript const logger = createLogger({ env: 'production' }); logger.fatal('let\'s log a fatal error', { tag: 'a simple tag', action: 'a simple action', details: { route: '/a/simple/route', name: 'my_error_name', status: 404, stack: new Error('Content Not Found').stack, } }); ```