TypeScript Logging Category Style

2.2.0 · active · verified Sun Apr 19

This package provides the "category style" flavor for the `typescript-logging` library, enabling hierarchical and topological logging structures. It allows applications to create a tree of loggers, where each `Category` can have child categories, facilitating fine-grained control over logging levels and output for specific application modules or concerns. For instance, a "Performance" category can have subcategories, and logging can be enabled/disabled at any level of this hierarchy. The current stable version is 2.2.0, with frequent minor and patch releases, typically driven by feature additions (like file logging support in 2.2.0 via `typescript-logging-node-channel`) or bug fixes. It differentiates itself by offering this unique tree-based categorization over other flat logging approaches, built specifically for TypeScript projects to leverage its type system.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates configuring a `CategoryProvider`, creating root and child categories, and logging messages at different levels, including error handling, to build a hierarchical logging structure.

import {LogLevel} from "typescript-logging";
import {CategoryProvider, Category} from "typescript-logging-category-style";

// --- Configuration (LogConfig.ts logic) ---
const provider = CategoryProvider.createProvider("ExampleProvider", {
  level: LogLevel.Debug,
  // Optional: Add a simple appender for console output if not already configured globally
  // appenders: { default: { type: 'console' } }
});

// Create some root categories for this example
const rootModel = provider.getCategory("model");
const rootService = provider.getCategory("service");
const rootMain = provider.getCategory("main");

// --- Model (Account.ts logic) ---
const modelLog = rootModel.getChildCategory("Account");

interface Account {
  name: string;
}

function createAccount(name: string): Account {
  modelLog.debug(() => `Creating new account with name '${name}'.`);
  return {name};
}

// --- Service (AccountService.ts logic) ---
const serviceLog = rootService.getChildCategory("AccountService");

// Mock function for demonstration
async function someExternalApiCall(): Promise<void> {
    return new Promise(resolve => setTimeout(resolve, 50)); // Simulate async work
}

async function saveAccount(account: Account) {
  serviceLog.debug(() => `Will save account '${account.name}'.`);
  try {
    await someExternalApiCall();
    serviceLog.info(() => `Account '${account.name}' saved successfully.`);
  }
  catch (e) {
    serviceLog.error(() => `Failed to save account '${account.name}'.`, e);
    throw e;
  }
}

// --- Main Application Logic (Main.ts logic) ---
(async () => {
  rootMain.info(() => "Application starting...");
  const newAccount = createAccount("JaneDoe");
  try {
    await saveAccount(newAccount);
  } catch (error) {
    rootMain.error(() => "Error during account operations.", error);
  }
  rootMain.info(() => "Application finished.");
})();

view raw JSON →