npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mongez/logger

v1.0.10

Published

A powerful system for logging messages and errors in nodejs.

Downloads

11

Readme

Mongez Logger

A powerful yet simple logger for Node.js

Features

  • Fully async and non-blocking which doesn't affect the performance of your application.
  • Easy to use and configure.
  • Has multiple channels to log the messages to.
  • You can add your own custom channels for logging.

Installation

yarn add @mongez/logger

Or

npm i @mongez/logger

Usage

At an early point of the application, you need to initialize the logger:

import logger, { FileLog,  } from '@mongez/logger';

logger.configure({
    channels: [
        new FileLog(),
        new ConsoleLog()
    ]
});

Here we declared our logger configurations to use the FileLog and ConsoleLog channels, the file log channel will log all the logs to a file, and the console log channel will log all the logs to the console.

Logging Strategy

To go any value simple use log function, it mainly receives 4 parameters:

  • module: the module name, it's used to group the logs, for example, if you have a module called request, all the logs related to the request module will be grouped under the request module.
  • action: the action name, it's used to group the logs, for example, if you have an action called create, all the logs related to the create action will be grouped under the create action.
  • message: the message to log.
  • level: there are 4 types of logging warn, info, error, debug, the default is info.

Examples

import { log } from '@mongez/logger';

log('request', 'create', 'user created successfully', 'info');

You can also use log.info log.warn log.error log.debug functions to log the message.

import { log } from '@mongez/logger';

log.info('request', 'create', 'user created successfully');

if (somethingWentWrong) {
    log.error('request', 'create', 'something went wrong');
}

database.on('connection', () => {
    log.success('database', 'connection', 'database connected successfully');
})

Console Log Channel

The console log channel will log all the logs to the console, the message appears in the console will be colored based on the log level using chalk.

import logger, { ConsoleLog } from '@mongez/logger';

logger.configure({
    channels: [
        new ConsoleLog()
    ]
});

File Log Channel

The file log channel will log all the logs to a single file, the file will be created in the logs directory in /storage directory with name app.log by default, however, you can change the file name and the directory path.

import logger, { FileLog } from '@mongez/logger';

logger.configure({
    channels: [
        new FileLog({
            storageDirectory: process.cwd() + '/logs',
            fileName: 'app.log'
        })
    ]
});

The message time is stored by default prefixed with current date/time in this format YYYY-MM-DD HH:mm:ss, however, you can change the format by passing the dateFormat option.

import logger, { FileLog } from '@mongez/logger';

logger.configure({
    channels: [
        new FileLog({
            dateFormat: {
                date: 'DD-MM-YYYY',
                time: 'HH:mm:ss'
            }
        })
    ]
});

You can see the available date/time formats in dayjs documentation.

This could be useful with small projects, but it's not recommended to use it if the application is large, because the file will be very large and it will affect the performance of the application, you can use the following channels to solve this problem.

Chunk File Log Channel

This channel will log all the logs to multiple files, it could be based on daily basis, monthly basis, yearly basis or evenhourly basis.

import logger, { ChunkFileLog } from '@mongez/logger';

logger.configure({
    channels: [
        new ChunkFileLog({
            storageDirectory: process.cwd() + '/logs',
            chunk: 'daily', // default is daily
            dateFormat: {
                date: 'DD-MM-YYYY',
                time: 'HH:mm:ss'
                month: 'YYYY MM',
            },
        })
    ]
});

For better performance, the file will be created in the logs directory in /storage, each file will be named based on the date, for example, if the date is 2021-01-01, the file name will be 2021-01-01.log, and the message time is stored by default prefixed with current date/time in this format YYYY-MM-DD HH:mm:ss, however, you can change the format by passing the dateFormat option.

JSON File Log Channel

Works exactly in the same sense of Chunk File Log Channel, but the difference is that the logs will be stored in JSON format.

import logger, { JSONFileLog } from '@mongez/logger';

logger.configure({
    channels: [
        new JSONFileLog({
            storageDirectory: process.cwd() + '/logs',
            chunk: 'daily', // default is daily
            dateFormat: {
                date: 'DD-MM-YYYY',
                time: 'HH:mm:ss'
                month: 'YYYY MM',
            },
        })
    ]
});

Example of output log file

/storage/logs/01-04-2023.json

{
    "date": "01-04-2023",
    "logs": [
        {
            "module": "request",
            "action": "create",
            "message": "user created successfully",
            "level": "info",
            "date": "01-04-2023",
            "time": "12:00:00"            
        }
    ]
}

If the log is an error log, the trace will also be included:

/storage/logs/01-04-2023.json

{
    "date": "01-04-2023",
    "logs": [
        {
            "module": "request",
            "action": "create",
            "message": "user created successfully",
            "level": "error",
            "date": "01-04-2023",
            "time": "12:00:00",
            "trace": "Error: something went wrong...."
        }
    ]
}

JSON File Typed Log Channel

This channel pretty much the same as JSON File Log Channel, but the difference is that the file path is structured as follows:

/$storagePath/json/$level/$module/$action/$date.json

Where $level is the log level, $module is the module name, $action is the action name, and $date is the date format.

This will make files more organized and easier to trace.

import logger, { JSONFileTypedLog } from '@mongez/logger';

logger.configure({
    channels: [
        new JSONFileTypedLog({
            storageDirectory: process.cwd() + '/logs',
            dateFormat: {
                date: 'DD-MM-YYYY',
                time: 'HH:mm:ss'
                month: 'YYYY MM',
            },
        })
    ]
});

Create Custom Log Channel

You can create your own log channel by extending the LogChannel class

import { LogChannel, LogLevel } from '@mongez/logger';

export default class CustomLogChannel extends LogChannel {
    /**
     * Log the message
     * 
     * @param module 
     * @param action 
     * @param message 
     * @param level 
     */
    public async log(module: string, action: string, message: string, level: LogLevel) {
        // log the message
    }
}

If the log channel will output something in the terminal, mark the terminal property as true.

import { LogChannel, LogLevel } from '@mongez/logger';

export default class CustomLogChannel extends LogChannel {
    /**
     * Whether the log channel will output something in the terminal
     */
    public terminal = true;

    /**
     * Log the message
     * 
     * @param module 
     * @param action 
     * @param message 
     * @param level 
     */
    public async log(module: string, action: string, message: string, level: LogLevel) {
        // log the message
    }
}

This will automatically parse and remove the ANSI color codes from the message.

Now you can use the custom log channel in your application.

import logger, { CustomLogChannel } from '@mongez/logger';

logger.configure({
    channels: [
        new CustomLogChannel()
    ]
});

Capture Uncaught Errors

If you want automatically capture any unhandled errors, you can import captureAnyUnhandledRejection function and call it in your application entry point.

import { captureAnyUnhandledRejection } from '@mongez/logger';

captureAnyUnhandledRejection();