Hack4Impact Logger

website·Indexed 30 days ago

https://hack4impact.github.io/logger/

A lightweight and easy-to-use logging library. It features a Logger class for managing log messages and output files, an Output class for synchronous flushing of outputs, and a comprehensive set of type aliases including Log, LogOptions, and ConsoleLevel.

Tokens
3.3K
Snippets
29
Records
37
Agent score
75%

What's inside Hack4Impact Logger

  1. Overview of Hack4Impact's Logger features

    Hack4Impact's Logger is a lightweight, dependency-free logging utility designed for fast installation and minimal overhead. Key characteristics include:

    • Zero Dependencies: The package has no external dependencies, resulting in a zipped size of less than 1.1kB.
    • Type Safety: The library is thoroughly typed and tested using Jest.
    • Quality Assurance: All changes are validated via automated GitHub Workflows to ensure stability and functionality.
  2. Use the Output class to manage synchronous log buffers

    The Output class allows developers to store a series of messages or nested output instances in an array and flush them all at once synchronously.
    const output = new Output();
    output.log("Hello World");
    output.flush();
  3. Install Hack4Impact Logger

    Install the @hack4impact/logger package using npm or yarn.
    # Using npm
    npm install @hack4impact/logger
    
    # Using yarn
    yarn add @hack4impact/logger
  4. Manage log content with Output methods

    The Output class provides several methods to manipulate the log buffer before flushing:

    • log(value): Adds a message to the output array.
    • line(): Adds a line break to the output array.
    • nested(): Creates and adds a nested Output instance to the current array.
    • flush(): Logs all accumulated messages in the output array.
    • output (accessor): Gets or sets the array of messages/nested outputs.
    const mainOutput = new Output();
    mainOutput.log("Main log");
    
    const nested = mainOutput.nested();
    nested.log("Nested log 1");
    nested.log("Nested log 2");
    
    mainOutput.line();
    mainOutput.log("Back to main");
    mainOutput.flush();
  5. Log error messages with the Logger class

    The error() method logs error messages to the console. By default, it writes the log to the configured output file. Use logOptions to disable file writing or provide extra metadata.
    // Basic usage: logs to console and writes to file
    await logger.error("Hello");
    
    // Log to console ONLY (do not write to file)
    await logger.error("Hello", { writeToFile: false });
    
    // Explicitly log to console and write to file
    await logger.error("Hello", { writeToFile: true });
  6. Log info messages with the Logger class

    The info() method logs messages to the console. By default, it also writes the log to the configured output file. You can override this behavior using logOptions.
    // Basic usage: logs to console and writes to file
    await logger.info("Hello");
    await logger.info(2);
    await logger.info(["hi!", 4, ["nested string"]]);
    
    // Log to console ONLY (do not write to file)
    await logger.info("Hello", { writeToFile: false });
    
    // Explicitly log to console and write to file
    await logger.info("Hello", { writeToFile: true });
  7. Persist logs to an output file using writeLogs

    The writeLogs instance method writes all logs currently stored in the this.logs array to the configured output file. It returns a Promise that resolves when the write operation is complete.
    await logger.writeLogs();
  8. Manage and retrieve log history and output paths

    The Logger class provides accessors to retrieve all logs generated by the instance and to get or set the destination file path for logs.
    // Get all logs output by this instance
    const logs = logger.logs;
    
    // Get the current output file path
    const logsPath = logger.logsPath;
    
    // Set a new output file path
    logger.logsPath = __dirname + "/logs.json";
  9. Log colored messages using Output.coloredLog

    The coloredLog method adds a message to the output array with a specified ANSI color. It also supports an optional afterColored string that is appended to the same line without the color formatting.
    const output = new Output();
    // Simple colored log
    output.coloredLog("BgBlue", "hi");
    
    // Colored log with trailing plain text
    output.coloredLog("FgYellow", "hi", " this string will not be colored");