Skip to content

Commit 2fd94ec

Browse files
committed
More convenience options for child logger filtering
1 parent 4e1b7cb commit 2fd94ec

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ nestedChild1.debug('I am nested one level');
205205
const nestedChild2 = childLogger(nestedChild1, ['Second', 'Third'], { level: 'silent' });
206206
nestedChild2.info('I do not log because of set level and not enabled by filter');
207207

208+
// without LOG_FILTER_ENABLE from above set, the below logging would be silent
209+
// because they inherit the level from their parent
210+
208211
const nestedChild3 = childLogger(nestedChild2, ['Fourth']);
209212
nestedChild3.info('I do log because of filter Third:Fourth');
210213

@@ -215,6 +218,18 @@ const nestedChild5 = childLogger(nestedChild4, ['Bar']);
215218
nestedChild5.info('I do log because of filter Bar');
216219
```
217220

221+
The minimum level a child logger is "enabled" to is ENV `LOG_FILTER_ENABLE_LEVEL` or `trace`, by default. It can also be specified by passing `labelEnableLevel` to childLogger options.
222+
223+
The minimum level a child logger is "disabled" to is ENV `LOG_FILTER_DISABLE_LEVEL` or `silent`, by default. It can also be specified by passing `labelDisableLevel` to childLogger options.
224+
225+
There are also options for passing your own function to determine if a child logger should be enabled or disabled. See [`childLogger`](https://foxxmd.github.io/logging/functions/index.childLogger.html) docs for full a full reference.
226+
227+
A caveat to be aware of: **enable/disable by filter is evaluated once, when `childLogger` is instantiated.** This means:
228+
229+
* Only the labels added by `childLogger` insantiation are applicable. Labels added during logging, IE `logger.info({labels: ['Runtime']}, "a log")`, are not considered.
230+
* Labels that are functions are evaluated **once**, when `childLogger` is instantiated
231+
* Changing the `LOG_FILTER_*` envs after a childLogger is created will have no effect on it
232+
218233
### Serializing Objects and Errors
219234

220235
Passing an object or array as the first argument to the logger will cause the object to be JSONified and pretty printed below the log message

src/loggers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ export const buildLogger = (defaultLevel: LogLevel, streams: LogLevelStreamEntry
9797
export const childLogger = (parent: Logger, labelsVal: any | any[] = [], context: object = {}, options: ChildLoggerOptions = {}): Logger => {
9898
const {
9999
labelEnable = labelsEnableFromEnvSingleton,
100-
labelEnableLevel = parent.level,
100+
labelEnableLevel = process.env.LOG_FILTER_ENABLE_LEVEL ?? 'trace',
101101
labelDisable = labelsDisableFromEnvSingleton,
102-
labelDisableLevel = 'silent',
102+
labelDisableLevel = process.env.LOG_FILTER_DISABLE_LEVEL ?? 'silent',
103103
...rest
104104
} = options;
105105

0 commit comments

Comments
 (0)