Comprehensive Guide to Level Logger Master Logging with Advanced API Usage

Introduction to Level Logger

The Level Logger is a robust logging library designed for JavaScript applications. It offers a variety of logging levels—such as debug, info, warn, and error—to help developers effectively track and manage log output. In this comprehensive guide, we will dive into the various APIs provided by Level Logger, accompanied by practical code snippets and a complete application example.

APIs in Level Logger

Level Logger offers a myriad of features and methods to facilitate efficient logging. Below are some of the most commonly used APIs:

1. Initialization

To begin using Level Logger, you need to initialize it first:

  const logger = new LevelLogger({
    level: 'info', // Default logging level
    format: 'json', // Log format
  });

2. Basic Logging

Level Logger allows you to log messages at different severity levels:

  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');

3. Customizing Log Levels

You can specify custom log levels if needed:

  logger.addLevel('custom', 50, { color: 'magenta', label: 'CUSTOM' });
  logger.custom('This is a custom log level message');

4. Formatting Logs

Custom formats can be applied to log messages:

  const customLogger = new LevelLogger({
    level: 'debug',
    format: (log) => `[${log.level.toUpperCase()}] ${log.message}`
  });
  customLogger.debug('Custom formatted debug message');

5. Transports

Level Logger supports various transports for log output:

  const fileTransport = new FileTransport({ filename: 'app.log' });
  const loggerWithTransports = new LevelLogger({
    level: 'info',
    transports: [fileTransport]
  });
  loggerWithTransports.info('This log will be written to a file');

6. Log Rotation

Automatic log rotation is also possible:

  const rotatingFileTransport = new RotatingFileTransport({
    filename: 'app-%DATE%.log',
    frequency: 'daily'
  });
  const loggerWithRotation = new LevelLogger({
    level: 'info',
    transports: [rotatingFileTransport]
  });
  loggerWithRotation.info('This log will be rotated daily');

7. Contextual Logging

Adding contexts to your logs can be very useful:

  const loggerWithContext = new LevelLogger({
    level: 'info',
    context: { userId: 12345 }
  });
  loggerWithContext.info('User login event');

Application Example

Let’s see an application example where we integrate Level Logger into a Node.js application:

  const http = require('http');
  const LevelLogger = require('level-logger');

  const logger = new LevelLogger({
    level: 'debug',
    transports: [
      new ConsoleTransport(),
      new FileTransport({ filename: 'server.log' })
    ]
  });

  const server = http.createServer((req, res) => {
    logger.debug(`Received request for ${req.url}`);
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello, world!\n');
    logger.info('Response sent');
  });

  server.listen(3000, () => {
    logger.info('Server running at http://localhost:3000/');
  });

By following this guide, you can effectively utilize Level Logger’s powerful APIs to manage your application’s logging with ease.

Hash: 4c18e6850375c0f35cab509b889ed230e61a0254b8b86f200d3fe333d453e2d6

Leave a Reply

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