❀Numbers Probably Matter

    tie-logger
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.2Β β€’Β PublicΒ β€’Β Published

    Tie Logger

    πŸ‘” Fully typed minimal platform-agnostic logger

    Test Status Downloads last commit codecov GitHub tie-logger Known Vulnerabilities Quality npm license MIT Size Codacy Badge

    🍬 Why use this lib?

    1. Small. This library weighs under 3kb (minified + gzip)
    2. Simple. Only 3 exports: Logger, logLevels and filter
    3. Secure. No dependencies. Library written in TypeScript, covered by tests and constantly being scanned by Snyk and Code QL

    πŸ“¦ Installation

    • Using npm
      npm i tie-logger
    • Using Yarn
      yarn add tie-logger
    • Using pnpm
      pnpm add tie-logger

    Usage

    Initialization

    /** @file logger.js */
    import { Logger, logLevels, filter } from "tie-logger";
    
    export const logger = new Logger(
      "app", // Root logger name
      logLevels(), // Define log levels. 
      // By default are: verbose, debug, info, warn, error, fatal
      // You can use custom levels by using
      // logLevels("info", "warn", "error")
    
      {
        // Custom data
        appVersion: "3.1"
        moduleName: "root",
        moduleVersion: "1.0.0"
      }
    );
    
    export const child = logger.child(
      // Child logger name
      "auth",
    
      // Child logger data
      { moduleName: "auth", moduleVersion: "0.3.1" }
    );
    
    const criticalLogs = [];
    
    const unsubscribe = logger.subscribe(
      // Subscribe to all logs, they go to console
      (log) => console.log(...log.message.parts),
    
      // All logs, that level is greater or equal than "warn" will be added to critical logs
    
      // Severity is determined by index of level in levels array
      // Current array is: verbose, debug, info, warn, error, fatal
      //                             [less] <<<  ^^^^   >> [greater]
      filter(">=", "warn", (log) => criticalLogs.push(log))
    )
    
    process.on("SIGINT", () => {
      unsubscribe();
    })

    Logging

    /** @file index.js */
    import { child, logger } from "./logger.js";
    
    const PORT = parseInt(process.env.PORT) || 3000;
    logger.subscribe(log => console.log(log));
    
    child.log.debug`Application initialized(${process.pid}). Port: ${{ port: PORT }}. Environment: ${{process.env}}`;
    // Level: ^^^^^
    
    // Here goes app

    Log format

    ({
      // One of defined levels
      level: "debug",
    
      // Date of creating log
      time: new Date(),
    
      message: {
        // Scalars are inlined in template
        template:
          "Application initialized(83915). Port: {port}. Environment: {SHELL,COLORTERM,PWD}",
        plain:
          'Application initialized(83915). Port: 3000. Environment: {"SHELL":"/bin/bash","COLORTERM":"truecolor","PWD":"/home/alexxgrib/Projects/tie-logger"}',
        parts: [
          "Application initialized(",
          83915,
          "). Port:",
          { port: 3000 },
          ". Environment: ",
          {
            SHELL: "/bin/bash",
            COLORTERM: "truecolor",
            PWD: "/home/alexxgrib/Projects/tie-logger"
          }
        ]
      },
    
      // merge of
      // - logger data
      // - logger parents data
      // - data passed in log message
      data: {
        // pid is scalar and has no name, so it is not added to data
        appVersion: "3.1",
        moduleName: "auth",
        moduleVersion: "0.3.1",
        port: 3000,
        SHELL: "/bin/bash",
        COLORTERM: "truecolor",
        PWD: "/home/alexxgrib/Projects/tie-logger"
      },
    
      context: {
        // name of the logger
        name: "auth",
    
        // list of logger inheritance
        path: ["app", "auth"]
      },
    
      // logger object
      origin: child
    });

    Install

    npm i tie-logger

    DownloadsWeekly Downloads

    300

    Version

    1.0.2

    License

    MIT

    Unpacked Size

    162 kB

    Total Files

    45

    Last publish

    Collaborators

    • alexxgrib