From b4a4f8a6b44f2f6b90476f4c61863f5d42c7ccf2 Mon Sep 17 00:00:00 2001 From: Jay McDoniel Date: Thu, 7 Nov 2024 11:32:51 -0800 Subject: [PATCH] feat(logger): create a child method --- apps/docs/src/pages/en/logger.md | 5 +++++ packages/logger/src/logger/ogma.ts | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/apps/docs/src/pages/en/logger.md b/apps/docs/src/pages/en/logger.md index 28a05794..c53a3012 100644 --- a/apps/docs/src/pages/en/logger.md +++ b/apps/docs/src/pages/en/logger.md @@ -88,6 +88,7 @@ This option is available in `@ogma/logger@^2.5.0` | context | string optional | a context for the Ogma class to work with. | | application | string optional | an application name for Ogma to print | | levelMap | an object with the above levels as the keys and strings as the vales | a way to provide custom log levels in the event that there are mappings the developer wants to support | +| levelKey | string optional | A specific key to print the level as when in JSON mode. Useful for things like GCP which use `severity` instead of `level` | | masks | string[] | An array of words that should be replaced while logging. useful for sensitive information like passwords. | | logPid | boolean | An optional property you can set if you don't want to log the PID. | | logApplication | boolean | An optional property you can set if you don't want to the the application name. | @@ -137,6 +138,10 @@ Using the non-JSON mode, color is attempted to be applied by default. This is de There may be times, like with [OpenTelemetry](https://opentelemetry.io) or with adding a correlation id, that you need to add dynamic values to each log. Rather than making a new wrapper around the Ogma instance, Ogma provides an optional `mixin` property that can be used to add extra data to each log without being in `verbose` mode. This property is a function, that takes in the current log level and the Ogma instance, and should return an object of any shape. +### Child Loggers + +There may be times where you have a default logging configuration that you want to use across multiple a project without having to save the configuration to a variable. If that ever becomes the case, you can call `ogma.child()` and pass in a partial object of new options which will merge with the original options passed to the first `new Ogma()` call. This will return a new logger instance that will have the combined options, so you can change things like the log level or the context easily, without having to create new `levelMap` or `masks`. + ## Example of what the logs look like I said the logs were beautiful, and to me they absolutely are. Each log is matched with a timestamp in ISO format, the log level, a pipe character, and then the message, for easier searching if needed. If a JSON is passed to Ogma, a new line will separate the JSON and the original log line, but the timestamp and level will not be duplicated. Ogma also will print `'[Function]'` if a function is found or `'[Circular]'` is a circular reference is found. diff --git a/packages/logger/src/logger/ogma.ts b/packages/logger/src/logger/ogma.ts index 390db720..b5855c7c 100644 --- a/packages/logger/src/logger/ogma.ts +++ b/packages/logger/src/logger/ogma.ts @@ -374,6 +374,15 @@ export class Ogma { return new Date().toISOString(); } + /** + * Create a new instance of ogma using pre-existing options while being able to pass in new options to override specific values. Useful for when there's a default configuration you'd like to make use of with slightly different modifications elsewhere + * @param newOptions overriding options for the new logger instance + * @returns a new ogma instance using the original logger's options, merged with the new options passed + */ + child(newOptions: Partial): Ogma { + return new Ogma({ ...this.options, ...newOptions }); + } + /** * Change the set log level for the Ogma logger * @param the new log level