TypeScript Logging Core
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
-
TypeError: Cannot read properties of undefined (reading 'createProvider')
cause Attempting to instantiate a logging provider (e.g., CategoryProvider or Log4TSProvider) directly from the `typescript-logging` core package, which only exports interfaces and not concrete implementations.fixEnsure you are importing the provider from the correct 'flavor' package, e.g., `import {CategoryProvider} from 'typescript-logging-category-style';` and that the flavor package is installed. -
Module not found: Can't resolve 'typescript-logging-category-style'
cause The specific logging flavor package (e.g., `typescript-logging-category-style` or `typescript-logging-log4ts-style`) has not been installed, or the import path is incorrect.fixInstall the required flavor package using npm: `npm install --save typescript-logging-category-style` (or your chosen flavor) and verify the import statement correctly references the package name and export.
Warnings
- breaking Version 2.x of `typescript-logging` is a complete rewrite and is not backward compatible with version 1.x APIs. Existing code using v1 will break.
- gotcha The `typescript-logging` core package provides only interfaces and basic types. To get functional logging, you must install and configure a specific 'flavor' package (e.g., `typescript-logging-category-style` or `typescript-logging-log4ts-style`) alongside the core library.
- deprecated The `LogLevel.Off` value was introduced in version 2.1.0. While it provides an explicit way to disable logging for a category, it might subtly alter behavior or require adjustments in existing configurations that implicitly handled disabled logging.
Install
-
npm install typescript-logging -
yarn add typescript-logging -
pnpm add typescript-logging
Imports
- LogLevel
const { LogLevel } = require('typescript-logging');import { LogLevel } from 'typescript-logging'; - CategoryProvider, Category
import CategoryProvider from 'typescript-logging-category-style'; const { Category } = require('typescript-logging-category-style');import { CategoryProvider, Category } from 'typescript-logging-category-style'; - Log4TSProvider, Logger
import Logger from 'typescript-logging-log4ts-style'; const { Log4TSProvider } = require('typescript-logging-log4ts-style');import { Log4TSProvider, Logger } from 'typescript-logging-log4ts-style';
Quickstart
/*--- 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");