A module to enrich logs with context, featuring async log contexts and scope management.
Go to file
2024-05-29 14:11:16 +02:00
.vscode fix(core): update 2021-09-17 19:21:34 +02:00
dist dependencies, first working version 2018-03-03 14:11:27 +01:00
test fix(core): update 2023-01-12 11:58:34 +01:00
ts BREAKING CHANGE(switch to esm): update 2023-01-12 11:59:41 +01:00
.gitignore fix(core): update 2020-07-20 11:57:20 +00:00
.gitlab-ci.yml fix(core): update 2023-01-12 11:58:34 +01:00
npmextra.json update tsconfig 2024-04-14 13:39:41 +02:00
package.json update description 2024-05-29 14:11:16 +02:00
pnpm-lock.yaml switch to new org scheme 2023-07-11 00:01:09 +02:00
readme.hints.md update tsconfig 2024-04-14 13:39:41 +02:00
readme.md update tsconfig 2024-04-14 13:39:41 +02:00
tsconfig.json update npmextra.json: githost 2024-04-01 21:33:20 +02:00

@push.rocks/logcontext

enrich logs with context

Install

To incorporate @push.rocks/logcontext into your project, you will need Node.js and npm (Node Package Manager) installed. Once you have those prerequisites, you can install @push.rocks/logcontext by running the following command in your project's root directory:

npm install @push.rocks/logcontext --save

This command will download and install @push.rocks/logcontext and its dependencies into your project's node_modules folder and save it as a dependency in your project's package.json file.

Usage

@push.rocks/logcontext is a TypeScript-focused module designed to enrich logging operations with contextual information, making it easier to trace logs through asynchronous operations in Node.js applications. It leverages modern JavaScript features such as async/await and provides a structured way to append and manage log-specific context.

Getting Started

First, ensure that you import the Logger class from @push.rocks/logcontext at the beginning of your TypeScript file:

import { Logger } from "@push.rocks/logcontext";

Creating a Logger Instance

To start logging with context, you'll first need to create an instance of a logger. Optionally, you can provide a namespace string during instantiation to differentiate logs from different parts of your application.

const appLogger = new Logger("appNamespace");

If no namespace is provided, a unique ID will be generated to serve as the namespace.

Adding Context and Logging

One of the core features of @push.rocks/logcontext is the ability to associate contextual data with logs. This is very useful in asynchronous operations where tracing the flow of execution through different contexts is necessary.

Scoping Contexts

To scope a context, use the scope function of the Logger instance. This function takes an async function as an argument, and any logs emitted within this function will include the provided scoped context.

appLogger.scope(async () => {
  appLogger.addData("userId", 123);
  appLogger.log("User authenticated");
  // Further async operations where logs should include { userId: 123 }
});

Asynchronous Context Propagation

The context you define will propagate through asynchronous calls, ensuring that logs emitted after asynchronous operations still contain the context that was active when the operation started.

Available Logging Methods

@push.rocks/logcontext exposes several logging methods, including log, error, warn, info, and debug. These methods are intended to mirror the conventional logging levels, making it easier to integrate into existing logging strategies.

appLogger.log("A standard log message.");
appLogger.error("An error message.");
appLogger.warn("A warning message.");
appLogger.info("An informational message.");
appLogger.debug("A debug message.");

Integrating with Third-Party Loggers

While @push.rocks/logcontext works well on its own for adding context to logs, it also supports integration with third-party logging solutions. You can associate a third-party logger using the addThirdPartyLogger method. This allows you to leverage the structured logging features of @push.rocks/logcontext while utilizing the logging infrastructure provided by another package.

Conclusion

@push.rocks/logcontext offers a powerful and flexible solution for enriched logging in Node.js applications. By enabling contextual logging and integrating seamlessly with asynchronous operations, it facilitates better traceability and debugging of applications. Whether used standalone or alongside other logging libraries, @push.rocks/logcontext helps maintain clear and useful logs in complex applications.

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.