Error Logging in Node

From Logic Wiki
Revision as of 12:02, 8 November 2018 by AliIybar (Talk | contribs)

Jump to: navigation, search


Installation

npm i winston

Setup

const winston = require('winston');
...
winston.add(winston.transports.File, {filename:'logfile.log'})

Usage

winston.log('error', err.message);

or

winston.error(err.message, error);
// error
// warn
// info 
// verbose
// debug
// silly

Logging to MongoDb

npm i winston-mongodb@3.0.0
require('winston-mongodb');
winston.add(winston.transports.MongoDB, { db: 'mongodb://localhost/somedb' });

To set only the errors to go to db

winston.add(winston.transports.MongoDB, { db: 'mongodb://localhost/somedb', level:'error' });

if we give the parameter as info error, warn and info will be logged.