Scrutinizer Logger The Ultimate Logging Solution for Modern Applications

Scrutinizer Logger: The Ultimate Logging Solution for Modern Applications

Introducing scrutinizer-logger, the comprehensive logging library designed to meet the needs of modern applications. With support for configurable logging levels, multiple handlers, and easy integration, scrutinizer-logger stands out as the go-to solution for developers looking to implement robust logging mechanisms.

Getting Started

To get started with scrutinizer-logger, first, you need to install it via npm:


  npm install scrutinizer-logger

Then, you can create a basic logger instance:


  const { createLogger, transports, format } = require('scrutinizer-logger');

  const logger = createLogger({
    level: 'info',
    format: format.combine(
      format.timestamp(),
      format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
    ),
    transports: [
      new transports.Console(),
      new transports.File({ filename: 'app.log' })
    ]
  });

Configurable Logging Levels

scrutinizer-logger allows you to set different logging levels, helping you filter logs based on the importance of the messages. Here are some commonly used logging levels:

  • error: Logs only error messages.
  • warn: Logs warnings and errors.
  • info: Logs informational messages, warnings, and errors.
  • debug: Logs everything, including debug messages.

Example:


  const logger = createLogger({
    level: 'debug',
    transports: [
      new transports.Console()
    ]
  });

  logger.debug('This is a debug message');
  logger.info('This is an info message');
  logger.warn('This is a warning message');
  logger.error('This is an error message');

Custom Transports

scrutinizer-logger supports multiple transports, allowing you to log messages to different destinations like files, consoles, or even remote servers. Here is an example of custom transports:


  const { createLogger, transports } = require('scrutinizer-logger');

  const logger = createLogger({
    transports: [
      new transports.Console(),
      new transports.File({ filename: 'combined.log' })
    ]
  });

  logger.info('This message will be logged to both the console and combined.log file');

Additionally, you can create custom transports:


  class CustomTransport {
    log(info, callback) {
      setImmediate(() => this.emit('logged', info));

      // Custom logic to handle log messages
      console.log(info);
      
      callback();
    }
  }

  const logger = createLogger({
    transports: [
      new CustomTransport()
    ]
  });

  logger.info('This is a custom transport log message');

Application Example

Here is a simple application demonstrating the use of scrutinizer-logger:


  const express = require('express');
  const { createLogger, transports, format } = require('scrutinizer-logger');

  const app = express();
  const logger = createLogger({
    level: 'info',
    format: format.combine(
      format.timestamp(),
      format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
    ),
    transports: [
      new transports.Console(),
      new transports.File({ filename: 'app.log' })
    ]
  });

  app.use((req, res, next) => {
    logger.info(`Request method: ${req.method}, URL: ${req.url}`);
    next();
  });

  app.get('/', (req, res) => {
    res.send('Hello World!');
    logger.debug('Response sent: Hello World!');
  });

  app.listen(3000, () => {
    logger.info('Server started on port 3000');
  });

In this example, the scrutinizer-logger is used to log details about incoming requests as well as server start-up messages. You can add more logging details as per your application’s requirement.

Adopt scrutinizer-logger in your projects today and enhance your application’s logging capabilities with minimal effort!

Hash: 2c873c39fc37fed81cfe94ef887ed5838c92c4548ef823d95b73019023d31cd2

Leave a Reply

Your email address will not be published. Required fields are marked *