2.0.0 • Published 6 years ago

solive-winston-logger v2.0.0

Weekly downloads
3
License
MIT
Repository
-
Last release
6 years ago

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:

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:

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

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.

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

...

const logger = createLogger({ env: 'production' });

logger.trace('let\'s log a trace');

Logger::debug

Used for debugging messages with extended information about application processing.

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.

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.

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.

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,
    }
});
2.0.0

6 years ago

1.6.1

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.1

6 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago