Difference between revisions of "Error Logging in Node"

From Logic Wiki
Jump to: navigation, search
(Created page with "Category:Node.js == Installation == npm i winston == Setup == const winston = require('winston'); ... winston.add(winston.transports.File, {filename:'logfile.log'}) =...")
(No difference)

Revision as of 12:00, 8 November 2018


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.