Code Monkey home page Code Monkey logo

tlog's Introduction

tlog

A simple implementation for logging library.

Usage

Basic usage

const logger = new Logger();

logger.trace('Trace message');
logger.debug('Debug message');
logger.info('Info message');
logger.warn('Warn message');
logger.error('Error message');

logger.info('Log the object', {
    foo: 'baz'
});

Log Appender

  • Appenders serialise log events to some form of output. They can write to files, outputs to console.

  • The library provides 2 log appenders:

    • Console Appender : This appender outputs log messages to the console.

    • File Appender : This appedner outputs log message to a file (as requirement).

Logger config parameters

  • name (string): The name of the logger.

  • timestamps (Boolean): Whether to include timestamps in logs (default: true)

  • appenders: Array of appenders to process and log the data. (default: [ConsoleAppender])

Log Appender Usage

// Initialize the console logger
const consoleAppender = new ConsoleAppender();

/*
  - The file appender will create 'logs/app.log' if it doesn't exist.
  - If 'logs/app.log' exists, it will append new logs to the file.
  - Log rotation options can be provided to the file appender to automatically
    rotate the log file based on date.
*/
const fileAppender = new FileAppender({ path: 'logs/app.log' });

// Set up the logger with both console and file appenders
const logger = new Logger({
  appenders: [consoleAppender, fileAppender]
});

/*
  The logger will use both appenders:
  - The console appender outputs log messages to the console.
  - The file appender writes log messages to 'logs/app.log'.
*/
logger.info('Information log message');

Create custom log appender

Can add multiple appenders to a single logger, so that log messages can be sent to multiple outputs.

You can also create custom log appenders by extending the Appender class as shown below:

export class CustomAppender extends Appender {
  constructor() {
    super();
  }

  handle(config: AppenderConfig) {
    console.log([`[CustomAppender] - ${config.message}`]);
  }
}

Note: You can also create custom log formatters by implementing the Formatter interface.

tlog's People

Contributors

ntananh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.