TypeScript Logging Core

2.2.0 · active · verified Sun Apr 19

TypeScript Logging is a core library designed for adding robust logging capabilities to both web and Node.js projects. It is currently at version 2.2.0 and is actively maintained, with minor releases adding new features such as file logging and rollover support via the `typescript-logging-node-channel` package. This package serves as the foundation; users must also install a specific 'flavor' package (e.g., `typescript-logging-category-style` or `typescript-logging-log4ts-style`) to gain concrete logging implementations and APIs. Version 2.x, a complete rewrite, is not compatible with version 1.x. Key differentiators include offering two distinct API styles: a hierarchical, category-based approach and a style mimicking Log4j/log4ts for familiarity, providing flexibility for different development preferences.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to configure the Category-style logging provider and obtain hierarchical loggers for different modules, logging messages at various levels.

/*--- LogConfig.ts ---*/
import {CategoryProvider, Category} from "typescript-logging-category-style";

const provider = CategoryProvider.createProvider("ExampleProvider");

export function getLogger(name: string): Category {
  return provider.getCategory(name);
}

/*--- Person.ts ---*/
import {getLogger} from "./LogConfig";

/* Root categories can and probably will be defined elsewhere, this is just an example */
const logModel = getLogger("model");

/* Create child categories based on a parent category, effectively allowing you to create a tree of loggers when needed */
const logPerson = logModel.getChildCategory("Person");

function example(value: string) {
  logPerson.debug(() => `Example function called with value ${value}`);
  try {
    // Awesome code here...
    logPerson.getChildCategory("example()").debug(() => "Child category again");
  }
  catch (e: any) {
    logPerson.error(() => "Awesome code failed unexpectedly", e);
  }
  finally {
    logPerson.debug(() => "Example function completed");
  }
}

// Example usage call
example("test-value");

view raw JSON →