Skip to main content

Logging

Why we need Logging

  • Debugging:Logs help you understand what went wrong when an error occurs, providing context and details that can be crucial for identifying the root cause
  • Performance Monitoring:By tracking execution times and resource usage, logs can highlight performance bottlenecks and inefficiencies
  • Security Auditing:Logs can record user actions and system events, helping to detect and investigate suspicious activities
  • Operational Insights:Logs provide a historical record of application behavior, useful for troubleshooting issues and optimizing performance

Logging Levels

  • Info:General informational messages about application progress or state changes
  • Warn:Potentially harmful situations that are not errors but might require attention
  • Error:Error events that might still allow the application to continue running
  • Debug:Detailed information for diagnosing problems, typically used in development
  • Trace:Finer-grained informational events than debug, often including detailed context

Node.js Built-in Logging

  • The console module in Node.js offers several methods for logging messages to the console
console.log() //Used for general logging of information
console.info() //Used for informational messages
console.warn() //Used for logging warning messages
console.error() //Used for logging error messages

Logging with Winston Library

  • Custom Log Levels:Define and use custom log levels to categorize and manage logs effectively
  • Multiple Transports:Log to various destinations such as files, databases, or remote services simultaneously
  • Structured Logging:Format logs in JSON or other structured formats, making them easier to parse and analyze
  • Timestamping:Automatically include timestamps in each log entry for better traceability
$ npm i winston

Log into console

//in index.js file, configure winston to log into console
const winston = require('winston');
winston.add(winston.transports.Console);

Log into a file

//in index.js file, configure winston to log into a file
const winston = require('winston');
winston.add(winston.transports.File,{filename:'logfile.log'});

//then in the errorHandler middleware
const winston = require('winston');
const errorHandler = (err,req,res,next) =>{
winston.log('error',err.message,err);
res.status(500).json({success:false,error:'['+err.code+'] '+err.message});
}
module.exports = errorHandler;

//the first argument is the level, which can be of the following:
//error
//warn
//info
//verbose
//debug
//silly

Log into MongoDB

//install the library
$ npm i winston-mongodb

//in index.js, configure as follows
const winston = require('winston');
require('winston-mongodb');
winston.add(winston.transports.MongoDB,{db:'mongodb://localhost.mydb'});

Log into mySQL

//install the library
$ npm i winston-mysql

//in index.js, configure as follows
const winston = require('winston');
require('winston-mysql');
const options_default = {
host: 'localhost',
user: 'logger',
password: '123456',
database: 'myDB',
table: 'mylogs'
};
winston.add(winston.transports.winstonMysql,options_default);