How to Use estrella-logger An Amazing Logging Library for Developers

Introduction to estrella-logger

Welcome to the comprehensive guide on using estrella-logger, an advanced and customizable logging library for developers. This tool is essential for managing logs effectively within your applications. In this guide, we will discuss various APIs provided by estrella-logger and demonstrate their usage with code snippets.

Setting Up estrella-logger

First, we’ll start by installing the estrella-logger package:

npm install estrella-logger

APIs and Usage

1. Basic Configuration

To get started, you can create a basic logger with the following configuration:

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

2. Logging Levels

estrella-logger supports multiple levels of logging:

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

3. Custom Formats

You can customize the format of your log messages:

 const customFormat = format.printf(({ level, message, timestamp }) => {
  return `[${timestamp}] ${level.toUpperCase()}: ${message}`;
});
const logger = createLogger({
  format: combine(
    format.timestamp(),
    customFormat
  ),
  transports: [
    new transports.Console()
  ]
}); 

4. HTTP Transport

Send logs to a remote HTTP server:

 const logger = createLogger({
  transports: [
    new transports.Http({
      host: 'logserver.example.com',
      port: 9000,
      path: '/log',
      ssl: true
    })
  ]
}); 

5. Exceptions Handling

Handle and log uncaught exceptions:

 const logger = createLogger({
  exceptionHandlers: [
    new transports.File({ filename: 'exceptions.log' })
  ]
}); 

6. Rejections Handling

Handle and log unhandled promise rejections:

 const logger = createLogger({
  rejectionHandlers: [
    new transports.File({ filename: 'rejections.log' })
  ]
}); 

Application Example

Below is an example of how you might use estrella-logger in an Express application:

 const express = require('express'); const { createLogger, transports, format } = require('estrella-logger');
const app = express(); const logger = createLogger({
  level: 'info',
  format: format.combine(
    format.timestamp(),
    format.simple()
  ),
  transports: [
    new transports.Console(),
    new transports.File({ filename: 'app.log' })
  ]
});
app.use((req, res, next) => {
  logger.info(`Received a ${req.method} request for ${req.url}`);
  next();
});
app.get('/', (req, res) => {
  logger.info('Handling home route');
  res.send('Hello, world!');
});
app.listen(3000, () => {
  logger.info('Server is running on port 3000');
}); 

By using estrella-logger, you can effectively manage logs within your applications, ensuring that you can track and debug issues efficiently.

Hash: 682c7d0b47f5606f6546d74824670e50d2c39c9fc92d855270e680dac76012f3

Leave a Reply

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