diff --git a/README.md b/README.md index 40cc092..a6a17a9 100644 --- a/README.md +++ b/README.md @@ -73,13 +73,15 @@ B.decorate(logger, logLeve, msg); D.decorate(logger, logLeve, msg); ``` -Each _derived_ logger manges its own `MessageDecoration` list. That is, adding a new decoration to the logger returned from `logger.tag()` will NOT affect the origin logger. -When a new logger derived from the origin logger, all the decorations of the origin logger will be copied to the derived one. The decorations of different derived loggers are managed separately. -In the other word, changing the decorations of one logger (even the main logger) will NOT affect the other existed logger, and vice versa. +##### Private MessageDecoration + +Each _derived_ logger manages its own `MessageDecoration` list. That is, adding a new decoration to the logger returned from `logger.tag()` will NOT affect the origin logger. +When a new logger derived from the origin logger, all the private decorations of the origin logger will be copied to the derived one. The decorations of different derived loggers are managed separately. +In the other word, changing the private decorations of one logger (even the main logger) will NOT affect the other existed logger, and vice versa. By default, a `MessageDecoration` for decorating the message with the `tag` and `tagSeparator` will be added as the first decoration with priority 0. We strongly recommend that DON'T call `logger.clearMessageDecorations()` at the main (origin) logger. Instead, always build a derived logger (by calling `logger.tag('', '')`, for example) and then do whatever you like. -Several methods in `logger` are related to the management of the `MessageDecoration`: +Several methods in `logger` are related to the management of the private `MessageDecoration`s: `logger.addMessageDecoration(messageDecoration: MessageDecoration): Logger` Add decoration to logger. @@ -90,6 +92,22 @@ Get a shallow copy of the decoration list of the logger. Remember that modify th `logger.clearMessageDecorations(): Logger` Clear the decorations of the logger. +##### Global MessageDecoration + +All _derived_ loggers share a _global_ `MessageDecoration` list. Changing any global decoration on any `logger` will affect all the other `logger`s' behaviour. +When logging messages, private decorations and global decorations will be merged and ordered by their priorities. When a private decoration and a global decoration share the same priority number, the private one has the higher priority. + +Several methods in `logger` are related to the management of the global `MessageDecoration`s: + +`logger.addGlobalMessageDecoration(messageDecoration: MessageDecoration): Logger` +Add global decoration to all loggers. + +`logger.getGlobalMessageDecorations(): MessageDecoration[]` +Get a shallow copy of the decoration list of the logger. Remember that modify the returned array will NOT affect the global decoration list. + +`logger.clearGlobalMessageDecorations(): Logger` +Clear the global decorations of the logger. + ##### MessageDecoration available outbox Some `MessageDecoration`s are predefined and available outbox: @@ -129,6 +147,9 @@ Here's the methods which will derive a new logger from the current logger when b ### Change logs +##### 0.3.1 +Introduce _global MessageDecoration_ to `logger`, which will be applied to all derived `logger`s. + ##### 0.3.0 `Logger` can contain more than one `MessageDecoration`, and all the decorations will be applied according to their priorities. The tag and tag separator of the logger is handled by a `MessageDecoration` added to the logger at the very first beginning now. diff --git a/package-lock.json b/package-lock.json index 90c627d..8896552 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "node-common-logger", - "version": "0.3.0", + "version": "0.3.1", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "0.3.0", + "version": "0.3.1", "license": "MIT", "devDependencies": { "typescript": "^4.1.2" diff --git a/package.json b/package.json index 35ae91a..e15d508 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-common-logger", - "version": "0.3.0", + "version": "0.3.1", "description": "A node library provides flexible logging ways and simple facade. User could use whatever implementations they like with the uniform api.", "main": "./dist/index.js", "types": "./typings/index.d.ts", diff --git a/src/Logger.ts b/src/Logger.ts index 92b7a26..de24069 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -8,6 +8,7 @@ export default class Logger implements LogStrategy { private strategy: LogStrategy; private readonly messageDecorations: MessageDecoration[]; + private readonly globalMessageDecorations: MessageDecoration[]; private currentTag?: string; private tagSeparator?: string; @@ -26,6 +27,7 @@ export default class Logger implements LogStrategy { return logger.applyArgument(msg); }, }]; + this.globalMessageDecorations = []; msgDecorations.forEach(decoration => this.addMessageDecoration(decoration)); } @@ -44,13 +46,7 @@ export default class Logger implements LogStrategy { } addMessageDecoration(messageDecoration: MessageDecoration): Logger { - for (let [i, decoration] of this.messageDecorations.entries()) { - if ((messageDecoration.priority || 0) < (decoration.priority || 0)) { - this.messageDecorations.splice(i, 0, messageDecoration); - return this; - } - } - this.messageDecorations.push(messageDecoration); + Logger.insertMessageDecoration(this.messageDecorations, messageDecoration); return this; } @@ -63,6 +59,20 @@ export default class Logger implements LogStrategy { return this; } + addGlobalMessageDecoration(messageDecoration: MessageDecoration): Logger { + Logger.insertMessageDecoration(this.globalMessageDecorations, messageDecoration); + return this; + } + + getGlobalMessageDecorations(): MessageDecoration[] { + return [...this.globalMessageDecorations]; + } + + clearGlobalMessageDecorations(): Logger { + this.globalMessageDecorations.length = 0; + return this; + } + /** * Fetch a specific named category logger. The result may be fetched from the cache unless useCache is * set to false. Since the category is relative to the basic LogStrategy, the caches would be rebuilt if @@ -208,6 +218,24 @@ export default class Logger implements LogStrategy { } private decorateMsg(logLevel: LogLevel, msg: string): string { - return this.messageDecorations.reduce((previousMsg, decoration) => decoration.decorate(this, logLevel, previousMsg), msg); + return this.mergeLocalAndGlobalDecorations().reduce((previousMsg, decoration) => decoration.decorate(this, logLevel, previousMsg), msg); + } + + private mergeLocalAndGlobalDecorations(): MessageDecoration[] { + const messageDecorations = this.getMessageDecorations(); + for (let globalMessageDecoration of this.globalMessageDecorations) { + Logger.insertMessageDecoration(messageDecorations, globalMessageDecoration); + } + return messageDecorations; + } + + private static insertMessageDecoration(list: MessageDecoration[], messageDecoration: MessageDecoration): void { + for (let [i, decoration] of list.entries()) { + if ((messageDecoration.priority || 0) < (decoration.priority || 0)) { + list.splice(i, 0, messageDecoration); + return; + } + } + list.push(messageDecoration); } } \ No newline at end of file