From b4a4f8a6b44f2f6b90476f4c61863f5d42c7ccf2 Mon Sep 17 00:00:00 2001 From: Jay McDoniel Date: Thu, 7 Nov 2024 11:32:51 -0800 Subject: [PATCH 1/4] 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 From 5bf006d0db137dafd3495e6247bf5366954db1bd Mon Sep 17 00:00:00 2001 From: Jay McDoniel Date: Thu, 7 Nov 2024 11:34:13 -0800 Subject: [PATCH 2/4] chore: changeset for logger 3.4.0 --- .changeset/long-moose-change.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/long-moose-change.md diff --git a/.changeset/long-moose-change.md b/.changeset/long-moose-change.md new file mode 100644 index 00000000..0020e7e8 --- /dev/null +++ b/.changeset/long-moose-change.md @@ -0,0 +1,5 @@ +--- +'@ogma/logger': minor +--- + +Create a child method to allow for creating new loggers with existing options From ac5137c4ccfbbc49c2f7fd8f3a1be25b434500d0 Mon Sep 17 00:00:00 2001 From: Jay McDoniel Date: Thu, 7 Nov 2024 11:39:29 -0800 Subject: [PATCH 3/4] feat(module): correct type for internal logger call --- packages/nestjs-module/src/ogma.service.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/nestjs-module/src/ogma.service.ts b/packages/nestjs-module/src/ogma.service.ts index 5486f68f..d1097978 100644 --- a/packages/nestjs-module/src/ogma.service.ts +++ b/packages/nestjs-module/src/ogma.service.ts @@ -187,7 +187,10 @@ export class OgmaService implements LoggerService { private printMessage( message: any, - levelString: Exclude, + levelString: Exclude< + keyof Ogma, + 'printMessage' | 'printError' | 'setLogLevel' | 'style' | 'child' + >, meta: OgmaServiceMeta = {}, ): void { meta.context = meta.context ?? this.context; From 360b2b94c2c112a4aa987da803ad2184f51b52e3 Mon Sep 17 00:00:00 2001 From: Jay McDoniel Date: Thu, 7 Nov 2024 11:39:56 -0800 Subject: [PATCH 4/4] chore: update changeset to include module --- .changeset/long-moose-change.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changeset/long-moose-change.md b/.changeset/long-moose-change.md index 0020e7e8..dbb7e947 100644 --- a/.changeset/long-moose-change.md +++ b/.changeset/long-moose-change.md @@ -1,5 +1,6 @@ --- '@ogma/logger': minor +'@ogma/nestjs-module': minor --- Create a child method to allow for creating new loggers with existing options