.vscode | ||
dist | ||
test | ||
ts | ||
.gitignore | ||
.gitlab-ci.yml | ||
npmextra.json | ||
package.json | ||
pnpm-lock.yaml | ||
readme.hints.md | ||
readme.md | ||
tsconfig.json |
@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.
License and Legal Information
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.