Skip to content

Commit d866d8f

Browse files
committed
Clean up logger name output and add delimiter
1 parent b0c4ccb commit d866d8f

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ secondSubLogger.silly("foo bar 2");
298298

299299
Output:
300300
```bash
301-
2022-11-17 10:45:47.705 SILLY [/examples/nodejs/index2.ts:51 MainLogger] foo bar
302-
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:54 MainLogger:FirstSubLogger] foo bar 1
303-
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:57 MainLogger:FirstSubLogger:SecondSubLogger] foo bar 2
301+
2022-11-17 10:45:47.705 SILLY [/examples/nodejs/index2.ts:51] MainLogger foo bar
302+
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:54] MainLogger:FirstSubLogger foo bar 1
303+
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:57] MainLogger:FirstSubLogger:SecondSubLogger foo bar 2
304304
```
305305

306306
#### minLevel
@@ -367,6 +367,7 @@ Following settings are available for styling:
367367
- `{{filePathWithLine}}`: a full path below the project path with a line number
368368
- `{{method}}`: _optional_ name of the invoking method
369369
- `prettyErrorParentNamesSeparator`: separator to be used when joining names ot the parent logger, and the current one (default: `:`)
370+
- `prettyErrorLoggerNameDelimiter`: if a logger name is set this delimiter will be added afterwards
370371
- `prettyInspectOptions`: <a href="https://nodejs.org/api/util.html#utilinspectobject-options" target="_blank">Available options</a>
371372

372373
- **Styling:**
@@ -390,6 +391,7 @@ const logger = new Logger({
390391
prettyErrorTemplate: "\n{{errorName}} {{errorMessage}}\nerror stack:\n{{errorStack}}",
391392
prettyErrorStackTemplate: " • {{fileName}}\t{{method}}\n\t{{filePathWithLine}}",
392393
prettyErrorParentNamesSeparator: ":",
394+
prettyErrorLoggerNameDelimiter: "\t",
393395
stylePrettyLogs: true,
394396
prettyLogStyles: {
395397
logLevelName: {
@@ -404,7 +406,7 @@ const logger = new Logger({
404406
},
405407
dateIsoStr: "white",
406408
filePathWithLine: "white",
407-
name: "white",
409+
name: ["white", "bold"],
408410
errorName: ["bold", "bgRedBright", "whiteBright"],
409411
fileName: ["yellow"],
410412
},

docs/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ secondSubLogger.silly("foo bar 2");
298298

299299
Output:
300300
```bash
301-
2022-11-17 10:45:47.705 SILLY [/examples/nodejs/index2.ts:51 MainLogger] foo bar
302-
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:54 MainLogger:FirstSubLogger] foo bar 1
303-
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:57 MainLogger:FirstSubLogger:SecondSubLogger] foo bar 2
301+
2022-11-17 10:45:47.705 SILLY [/examples/nodejs/index2.ts:51] MainLogger foo bar
302+
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:54] MainLogger:FirstSubLogger foo bar 1
303+
2022-11-17 10:45:47.706 SILLY [/examples/nodejs/index2.ts:57] MainLogger:FirstSubLogger:SecondSubLogger foo bar 2
304304
```
305305

306306
#### minLevel
@@ -367,6 +367,7 @@ Following settings are available for styling:
367367
- `{{filePathWithLine}}`: a full path below the project path with a line number
368368
- `{{method}}`: _optional_ name of the invoking method
369369
- `prettyErrorParentNamesSeparator`: separator to be used when joining names ot the parent logger, and the current one (default: `:`)
370+
- `prettyErrorLoggerNameDelimiter`: if a logger name is set this delimiter will be added afterwards
370371
- `prettyInspectOptions`: <a href="https://nodejs.org/api/util.html#utilinspectobject-options" target="_blank">Available options</a>
371372

372373
- **Styling:**
@@ -390,6 +391,7 @@ const logger = new Logger({
390391
prettyErrorTemplate: "\n{{errorName}} {{errorMessage}}\nerror stack:\n{{errorStack}}",
391392
prettyErrorStackTemplate: " • {{fileName}}\t{{method}}\n\t{{filePathWithLine}}",
392393
prettyErrorParentNamesSeparator: ":",
394+
prettyErrorLoggerNameDelimiter: "\t",
393395
stylePrettyLogs: true,
394396
prettyLogStyles: {
395397
logLevelName: {
@@ -404,7 +406,7 @@ const logger = new Logger({
404406
},
405407
dateIsoStr: "white",
406408
filePathWithLine: "white",
407-
name: "white",
409+
name: ["white", "bold"],
408410
errorName: ["bold", "bgRedBright", "whiteBright"],
409411
fileName: ["yellow"],
410412
},

src/BaseLogger.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ export class BaseLogger<LogObj> {
2424
minLevel: settings?.minLevel ?? 0,
2525
argumentsArrayName: settings?.argumentsArrayName,
2626
prettyLogTemplate:
27-
settings?.prettyLogTemplate ?? "{{yyyy}}.{{mm}}.{{dd}} {{hh}}:{{MM}}:{{ss}}:{{ms}}\t{{logLevelName}}\t[{{filePathWithLine}}{{name}}]\t",
27+
settings?.prettyLogTemplate ?? "{{yyyy}}.{{mm}}.{{dd}} {{hh}}:{{MM}}:{{ss}}:{{ms}}\t{{logLevelName}}\t[{{filePathWithLine}}]\t{{name}}",
2828
prettyErrorTemplate: settings?.prettyErrorTemplate ?? "\n{{errorName}} {{errorMessage}}\nerror stack:\n{{errorStack}}",
2929
prettyErrorStackTemplate: settings?.prettyErrorTemplate ?? " • {{fileName}}\t{{method}}\n\t{{filePathWithLine}}",
3030
prettyErrorParentNamesSeparator: settings?.prettyErrorParentNamesSeparator ?? ":",
31+
prettyErrorLoggerNameDelimiter: settings?.prettyErrorLoggerNameDelimiter ?? "\t",
3132
stylePrettyLogs: settings?.stylePrettyLogs ?? true,
3233
prettyLogStyles: settings?.prettyLogStyles ?? {
3334
logLevelName: {
@@ -42,7 +43,7 @@ export class BaseLogger<LogObj> {
4243
},
4344
dateIsoStr: "white",
4445
filePathWithLine: "white",
45-
name: "white",
46+
name: ["white", "bold"],
4647
errorName: ["bold", "bgRedBright", "whiteBright"],
4748
fileName: ["yellow"],
4849
},
@@ -89,8 +90,8 @@ export class BaseLogger<LogObj> {
8990
this.settings.overwrite?.mask != null
9091
? this.settings.overwrite?.mask(logArgs)
9192
: this.settings.maskValuesOfKeys != null && this.settings.maskValuesOfKeys.length > 0
92-
? this._mask(logArgs)
93-
: logArgs;
93+
? this._mask(logArgs)
94+
: logArgs;
9495
// execute default LogObj functions for every log (e.g. requestId)
9596
const thisLogObj: LogObj | undefined = this.logObj != null ? this._recursiveCloneAndExecuteFunctions(this.logObj) : undefined;
9697
const logObj: LogObj =
@@ -124,8 +125,8 @@ export class BaseLogger<LogObj> {
124125
this.settings.overwrite?.transportJSON != null
125126
? this.settings.overwrite?.transportJSON(logObjWithMeta)
126127
: this.settings.type !== "hidden"
127-
? transportJSON(logObjWithMeta)
128-
: undefined;
128+
? transportJSON(logObjWithMeta)
129+
: undefined;
129130
}
130131

131132
if (this.settings.attachedTransports != null && this.settings.attachedTransports.length > 0) {
@@ -160,8 +161,8 @@ export class BaseLogger<LogObj> {
160161
this.settings?.parentNames != null && this.settings?.name != null
161162
? [...this.settings.parentNames, this.settings.name]
162163
: this.settings?.name != null
163-
? [this.settings.name]
164-
: undefined,
164+
? [this.settings.name]
165+
: undefined,
165166
// merge all prefixes instead of overwriting them
166167
prefix: [...this.settings.prefix, ...(settings?.prefix ?? [])],
167168
};
@@ -215,15 +216,15 @@ export class BaseLogger<LogObj> {
215216
return Array.isArray(source)
216217
? source.map((item) => this._recursiveCloneAndExecuteFunctions(item))
217218
: source instanceof Date
218-
? new Date(source.getTime())
219-
: source && typeof source === "object"
220-
? Object.getOwnPropertyNames(source).reduce((o, prop) => {
221-
Object.defineProperty(o, prop, Object.getOwnPropertyDescriptor(source, prop)!);
222-
// execute functions or clone
223-
o[prop] = typeof source[prop] === "function" ? source[prop]() : this._recursiveCloneAndExecuteFunctions((source as { [key: string]: unknown })[prop]);
224-
return o;
225-
}, Object.create(Object.getPrototypeOf(source)))
226-
: (source as T);
219+
? new Date(source.getTime())
220+
: source && typeof source === "object"
221+
? Object.getOwnPropertyNames(source).reduce((o, prop) => {
222+
Object.defineProperty(o, prop, Object.getOwnPropertyDescriptor(source, prop)!);
223+
// execute functions or clone
224+
o[prop] = typeof source[prop] === "function" ? source[prop]() : this._recursiveCloneAndExecuteFunctions((source as { [key: string]: unknown })[prop]);
225+
return o;
226+
}, Object.create(Object.getPrototypeOf(source)))
227+
: (source as T);
227228
}
228229

229230
private _toLogObj(args: unknown[], clonedLogObj: LogObj = {} as LogObj): LogObj {
@@ -289,7 +290,8 @@ export class BaseLogger<LogObj> {
289290
// name
290291
let parentNamesString = this.settings.parentNames?.join(this.settings.prettyErrorParentNamesSeparator);
291292
parentNamesString = parentNamesString != null && logObjMeta?.name != null ? parentNamesString + this.settings.prettyErrorParentNamesSeparator : undefined;
292-
placeholderValues["name"] = logObjMeta?.name != null || parentNamesString != null ? (parentNamesString ?? "") + logObjMeta?.name ?? "" : "";
293+
const nameStr = logObjMeta?.name != null || parentNamesString != null ? (parentNamesString ?? "") + logObjMeta?.name ?? "" : "";
294+
placeholderValues["name"] = nameStr.length > 0 ? nameStr + this.settings.prettyErrorLoggerNameDelimiter : "";
293295

294296
return formatTemplate(this.settings, template, placeholderValues);
295297
}

src/interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface ISettingsParam<LogObj> {
1818
prettyErrorTemplate?: string;
1919
prettyErrorStackTemplate?: string;
2020
prettyErrorParentNamesSeparator?: string;
21+
prettyErrorLoggerNameDelimiter?: string;
2122
stylePrettyLogs?: boolean;
2223
prettyLogStyles?: {
2324
yyyy?: TStyle;
@@ -64,6 +65,7 @@ export interface ISettings<LogObj> extends ISettingsParam<LogObj> {
6465
prettyErrorTemplate: string;
6566
prettyErrorStackTemplate: string;
6667
prettyErrorParentNamesSeparator: string;
68+
prettyErrorLoggerNameDelimiter: string;
6769
stylePrettyLogs: boolean;
6870
prettyLogStyles: {
6971
yyyy?: TStyle;

0 commit comments

Comments
 (0)