{"id":12241,"library":"typescript-logging-log4ts-style","title":"TypeScript Logging Log4TS Style","description":"typescript-logging-log4ts-style is a logging library for TypeScript projects, providing a Log4TS-inspired flavor built upon the `typescript-logging` core. It allows developers to create hierarchical loggers based on unique names, often corresponding to namespaces or logical groups (e.g., 'service.Account'). The library emphasizes structured output and dynamic control over logging levels. The current stable version is 2.2.0, with regular updates incorporating new features like file logging and rollover support as seen in version 2.2.0. It differentiates itself by offering a familiar log4j-like API and configuration patterns, making it approachable for developers accustomed to enterprise logging solutions, while integrating seamlessly into modern TypeScript environments. Its modular design as a 'flavor' allows users to choose the logging style that best fits their project's needs.","status":"active","version":"2.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/vauxite-org/typescript-logging","tags":["javascript","typescript-logging","typescript logging","typescript-logging log4ts","typescript logging log4ts","typescript logger","log4ts","log4ts-like","logging","typescript"],"install":[{"cmd":"npm install typescript-logging-log4ts-style","lang":"bash","label":"npm"},{"cmd":"yarn add typescript-logging-log4ts-style","lang":"bash","label":"yarn"},{"cmd":"pnpm add typescript-logging-log4ts-style","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core logging library; this package provides the Log4TS style on top of it.","package":"typescript-logging","optional":false}],"imports":[{"note":"The library primarily uses ESM imports for its public API since version 2.x. CommonJS require() is not supported.","wrong":"const Log4TSProvider = require('typescript-logging-log4ts-style');","symbol":"Log4TSProvider","correct":"import { Log4TSProvider } from 'typescript-logging-log4ts-style';"},{"note":"LogLevel is part of the core `typescript-logging` package, not the style package itself.","wrong":"import { LogLevel } from 'typescript-logging-log4ts-style';","symbol":"LogLevel","correct":"import { LogLevel } from 'typescript-logging';"},{"note":"The `Logger` interface and type definitions are exposed by the core `typescript-logging` package, though you primarily interact with it via `provider.getLogger()`.","symbol":"Logger","correct":"import { Logger } from 'typescript-logging';"}],"quickstart":{"code":"/*--- config/LogConfig.ts ---*/\nimport { LogLevel } from \"typescript-logging\";\nimport { Log4TSProvider } from \"typescript-logging-log4ts-style\";\n\nexport const log4TSProvider = Log4TSProvider.createProvider(\"AwesomeLog4TSProvider\", {\n  level: LogLevel.Debug,\n  groups: [{\n    expression: new RegExp(\".+\")\n  }]\n});\n\n/*--- model/Account.ts ---*/\nimport { log4TSProvider } from \"../config/LogConfig\";\n\nconst log = log4TSProvider.getLogger(\"model.Account\");\n\nexport interface Account {\n  name: string;\n}\n\nexport function createAccount(name: string): Account {\n  log.debug(() => `Creating new account with name '${name}'.`);\n  return {name};\n}\n\n/*--- service/AccountService.ts ---*/\nimport { log4TSProvider } from \"../config/LogConfig\";\nimport { Account } from \"../model/Account\";\n\nconst log = log4TSProvider.getLogger(\"service.AccountService\");\n\nasync function someUpdateHere() {\n  return new Promise(resolve => setTimeout(resolve, 100)); // Simulate async operation\n}\n\nexport async function saveAccount(account: Account) {\n  log.debug(() => `Will save account '${account.name}'.`);\n  try {\n    await someUpdateHere();\n  }\n  catch (e) {\n    log.error(() => `Failed to save account '${account.name}'.`, e);\n    throw e;\n  }\n}\n\n/*--- Main.ts ---*/\nimport { log4TSProvider } from \"../config/LogConfig\";\nimport { createAccount } from \"./model/Account\";\nimport { saveAccount } from \"./service/AccountService\"; // Corrected import path\n\nconst log = log4TSProvider.getLogger(\"main\");\n\n/* Create an account and save it - log on success/error */\nconst account = createAccount(\"My Account\");\nsaveAccount(account)\n  .then(() => {\n    log.debug(\"Successfully created account.\", account.name);\n  })\n  .catch(e => {\n    log.error(\"Ooops...\", e);\n  });","lang":"typescript","description":"This quickstart demonstrates setting up a Log4TSProvider, configuring log levels and groups, creating named loggers, and using them across different modules for debugging and error logging."},"warnings":[{"fix":"Refer to the migration guide available in the `typescript-logging` repository for 1.x releases (e.g., `https://github.com/vauxite-org/typescript-logging/tree/release-1.x`) and rewrite logging configuration and calls for the 2.x API.","message":"Major architectural changes occurred between version 1.x and 2.x of `typescript-logging`. Code written for 1.x is not directly compatible with 2.x and requires a migration.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Always install both packages: `npm install --save typescript-logging typescript-logging-log4ts-style`.","message":"The `typescript-logging-log4ts-style` package is an add-on 'flavor' and requires the core `typescript-logging` package to be installed as a separate dependency.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Review existing code that interacts directly with `LogLevel` values or performs custom comparisons to ensure compatibility with the new `LogLevel.Off` value.","message":"The `LogLevel.Off` enumeration was introduced in version 2.1.0. While minor, there is a small chance this could affect existing code if custom log level handling or comparisons were in place.","severity":"gotcha","affected_versions":">=2.1.0"},{"fix":"Install `npm install --save typescript-logging-node-channel` and consult its documentation for configuration details to enable file output.","message":"To enable file logging and rollover support, the separate `typescript-logging-node-channel` package must be installed and configured in addition to the core and style packages.","severity":"gotcha","affected_versions":">=2.2.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure both `typescript-logging` and `typescript-logging-log4ts-style` are installed: `npm install --save typescript-logging typescript-logging-log4ts-style`.","cause":"The package was not installed or installed incorrectly, or TypeScript cannot locate its types.","error":"Cannot find module 'typescript-logging-log4ts-style' or its corresponding type declarations."},{"fix":"Verify that `Log4TSProvider.createProvider` is called before any `getLogger` calls, and that the provider object is correctly exported and imported across modules.","cause":"The `log4TSProvider` was not initialized or imported correctly, leading to an attempt to call a method on an `undefined` object.","error":"TypeError: Cannot read properties of undefined (reading 'getLogger')"},{"fix":"Adjust the `level` property in your `Log4TSProvider` configuration (e.g., to `LogLevel.Debug` or `LogLevel.Trace`) and ensure group expressions match your logger names.","cause":"The configured `LogLevel` for the provider or a specific group is set too high (e.g., `LogLevel.Error`), effectively filtering out lower-level messages.","error":"Logging statements (e.g., `log.debug()`) are not appearing in the console or output."},{"fix":"Use ES Module `import` syntax (e.g., `import { Log4TSProvider } from 'typescript-logging-log4ts-style';`) and ensure your project is configured for ESM (e.g., `\"type\": \"module\"` in `package.json`).","cause":"Attempting to use CommonJS `require()` syntax in an ESM project, or trying to import an ESM-only library with `require()`.","error":"ReferenceError: require is not defined in ES module scope"}],"ecosystem":"npm"}