Everything You Need to Know About Vault Logger – APIs Explained with Code Snippets

Introduction to Vault Logger

Vault Logger is a powerful logging library designed for flexibility and scalability in various applications. Whether you’re developing web applications, microservices, or desktop apps, Vault Logger provides a seamless way to maintain and manage logs effectively.

API Overview

Below, you’ll find a comprehensive overview of the most useful APIs that Vault Logger offers, complete with practical code snippets to illustrate their use.

1. Initializing the Logger

  const vaultLogger = require('vault-logger');
  const logger = vaultLogger.createLogger({
    level: 'info',
    transports: [
      new vaultLogger.transports.Console(),
      new vaultLogger.transports.File({ filename: 'combined.log' })
    ]
  });

2. Logging Messages

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

3. Custom Log Levels

  const customLevels = {
    levels: {
      error: 0,
      warn: 1,
      success: 2,
      info: 3,
      debug: 4
    },
    colors: {
      error: 'red',
      warn: 'yellow',
      success: 'green',
      info: 'blue',
      debug: 'magenta'
    }
  };
  
  const customLogger = vaultLogger.createLogger({
    levels: customLevels.levels,
    transports: [
      new vaultLogger.transports.Console({
        format: vaultLogger.format.combine(
          vaultLogger.format.colorize(),
          vaultLogger.format.simple()
        )
      })
    ]
  });
  
  customLogger.success('This is a success message');

4. Using Middleware in Express

  const express = require('express');
  const app = express();
  
  app.use(vaultLogger.middleware);
  
  app.get('/', (req, res) => {
    res.send('Hello, World!');
  });
  
  app.listen(3000, () => {
    logger.info('Server is running on port 3000');
  });

5. Handling Exceptions

  const customLogger = vaultLogger.createLogger({
    exceptionHandlers: [
      new vaultLogger.transports.File({ filename: 'exceptions.log' })
    ]
  });
  
  process.on('uncaughtException', (err) => {
    customLogger.error('Uncaught Exception:', err);
  });
  
  process.on('unhandledRejection', (reason, p) => {
    customLogger.error('Unhandled Rejection at:', p, 'reason:', reason);
  });

Application Example Using Vault Logger

Now, let’s put everything together in a sample application that uses Vault Logger to manage logs.

  const express = require('express');
  const vaultLogger = require('vault-logger');
  
  const appLogger = vaultLogger.createLogger({
    level: 'info',
    transports: [
      new vaultLogger.transports.Console(),
      new vaultLogger.transports.File({ filename: 'app.log' })
    ]
  });
  
  const app = express();
  
  app.use(vaultLogger.middleware);
  
  app.get('/', (req, res) => {
    appLogger.info('Home page visited');
    res.send('Welcome to Home Page');
  });
  
  app.get('/error', (req, res) => {
    appLogger.error('Error page visited');
    throw new Error('This is an intentional error');
  });
  
  app.use((err, req, res, next) => {
    appLogger.error('Unhandled error:', err);
    res.status(500).send('Something went wrong!');
  });
  
  app.listen(3000, () => {
    appLogger.info('Server is running on http://localhost:3000');
  });

By using Vault Logger, you can have a well-structured and efficient logging system for your applications. The flexibility in customizations and integrations makes it a perfect choice for developers.

Hash: e7b612e9b9f52b510b28e2ec643efa0f5131605f5b75498f69ab361089cf52ec

Leave a Reply

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