Mini Logger The Ultimate Guide to Efficient and Lightweight Logging in Application Development

Welcome to Mini Logger

Meet Mini Logger, a powerful yet simple and lightweight logging library designed to enhance your application development with minimal overhead. This article provides an in-depth guide on Mini Logger’s various features and APIs, allowing you to incorporate efficient logging into your applications seamlessly.

Installation


$ npm install mini-logger

Basic Usage

Start by creating a logger instance:


const Logger = require('mini-logger');
const logger = new Logger();

Log messages at different levels:


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

More API Examples

Mini Logger provides dozens of useful APIs for various logging needs. Here are some of the key methods:

Set Log Level


logger.setLevel('debug');

Log with Metadata


logger.info('User logged in', { userId: 123 });

Timestamped Logs


logger.timestamp(true);

Asynchronous Logging


logger.async(true);

Custom Log Writer


const writer = (message) => {
    console.log(`[Custom Writer] ${message}`);
};
logger.setWriter(writer);

Error Capturing


try {
  throw new Error('Something went wrong');
} catch (error) {
  logger.error('Caught an error', error);
}

App Example

Here is an example of a simple application using Mini Logger to handle various logging scenarios:


const express = require('express');
const Logger = require('mini-logger');

const app = express();
const logger = new Logger();

logger.setLevel('info');
logger.timestamp(true);

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

app.get('/', (req, res) => {
    logger.info('Handling root route');
    res.send('Hello, Mini Logger!');
});

app.use((err, req, res, next) => {
    logger.error('Server error', { error: err.message });
    res.status(500).send('Internal Server Error');
});

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

With Mini Logger, you can improve the readability and maintenance of your application logs, making debugging and monitoring a breeze. So, integrate Mini Logger into your project today and enjoy efficient logging!

Hash: 1dfb6a25f5cd1fb2ed89eb93b556fa8b4ced6bf628100676945c2fecf37ea6b9

Leave a Reply

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