-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(logger): remove util.format usage in the codebase for common code
- Loading branch information
Showing
12 changed files
with
216 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import {Logger} from "../common"; | ||
import {Logger, PatternLayout} from "../common"; | ||
import {LayoutReplacer} from "./layouts/LayoutReplacer"; | ||
|
||
export * from "../common"; | ||
export const $log: Logger = new Logger("default"); | ||
|
||
$log.appenders.set("console", {type: "console", levels: ["info", "debug", "trace", "fatal", "error", "warn"]}); | ||
|
||
PatternLayout.LayoutReplacer = LayoutReplacer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
packages/logger/src/common/layouts/components/MessagePassThroughLayout.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import {BaseLayout} from "../class/BaseLayout"; | ||
import {LogEvent} from "../../core/LogEvent"; | ||
import {Layout} from "../decorators/layout"; | ||
import * as Util from "util"; | ||
import {format} from "../utils/StringUtils"; | ||
|
||
@Layout({name: "messagePassThrough"}) | ||
export class MessagePassThroughLayout extends BaseLayout { | ||
transform(loggingEvent: LogEvent, timezoneOffset?: number): string { | ||
return (Util.format as any)(...[].concat(loggingEvent.data as any)); | ||
// @ts-ignore | ||
return format(...[].concat(loggingEvent.data as any)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/logger/src/common/layouts/utils/timestampLevelAndCategory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
import Os from "os"; | ||
import Util from "util"; | ||
import {LayoutReplacer, Logger} from "../common/index"; | ||
import {Logger, PatternLayout} from "../common/index"; | ||
import "./appenders/StdoutAppender"; | ||
import "./appenders/StderrAppender"; | ||
import {LayoutReplacer} from "./layouts/LayoutReplacer"; | ||
|
||
export const $log: Logger = new Logger("default"); | ||
|
||
$log.appenders | ||
.set("stdout", {type: "stdout", levels: ["info", "debug"]}) | ||
.set("stderr", {type: "stderr", levels: ["trace", "fatal", "error", "warn"]}); | ||
|
||
LayoutReplacer.EOL = Os.EOL || "\n"; | ||
LayoutReplacer.HOSTNAME = Os.hostname().toString(); | ||
LayoutReplacer.formatter = Util.format; | ||
PatternLayout.LayoutReplacer = LayoutReplacer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
// @ts-ignore | ||
import * as dateFormat from "date-format"; | ||
import {LogEvent} from "../../common/core/LogEvent"; | ||
import {LOG_COLORS} from "../../common/layouts/constants/logColors"; | ||
import {colorizeEnd, colorizeStart} from "../../common/layouts/utils/colorizeUtils"; | ||
import {IReplacers} from "../../common/layouts/interfaces/Replacers"; | ||
import {TokensHandlers} from "../../common/layouts/interfaces/BasicLayoutConfiguration"; | ||
import Os from "os"; | ||
import Util from "util"; | ||
|
||
export class LayoutReplacer { | ||
static EOL = Os.EOL || "\n"; | ||
static HOSTNAME = Os.hostname().toString(); | ||
static formatter = Util.format; | ||
|
||
constructor( | ||
private tokens: TokensHandlers, | ||
private timezoneOffset: number | ||
) {} | ||
|
||
/** | ||
* | ||
* @param loggingEvent | ||
* @param specifier | ||
* @returns {any} | ||
*/ | ||
public categoryName = (loggingEvent: LogEvent, specifier: string): string => { | ||
let loggerName = loggingEvent.categoryName; | ||
if (specifier) { | ||
const precision = parseInt(specifier, 10); | ||
const loggerNameBits = loggerName.split("."); | ||
if (precision < loggerNameBits.length) { | ||
loggerName = loggerNameBits.slice(loggerNameBits.length - precision).join("."); | ||
} | ||
} | ||
return loggerName; | ||
}; | ||
/** | ||
* | ||
* @param loggingEvent | ||
* @param specifier | ||
* @returns {any} | ||
*/ | ||
public formatAsDate = (loggingEvent: LogEvent, specifier: string): string => { | ||
let format = dateFormat.ISO8601_FORMAT; | ||
if (specifier) { | ||
format = specifier; | ||
// Pick up special cases | ||
if (format === "ISO8601") { | ||
format = dateFormat.ISO8601_FORMAT; | ||
} else if (format === "ISO8601_WITH_TZ_OFFSET") { | ||
format = dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT; | ||
} else if (format === "ABSOLUTE") { | ||
format = dateFormat.ABSOLUTETIME_FORMAT; | ||
} else if (format === "DATE") { | ||
format = dateFormat.DATETIME_FORMAT; | ||
} | ||
} | ||
// Format the date | ||
return dateFormat.asString(format, loggingEvent.startTime, this.timezoneOffset); | ||
}; | ||
/** | ||
* | ||
* @returns {string} | ||
*/ | ||
public hostname = (): string => { | ||
return LayoutReplacer.HOSTNAME || ""; | ||
}; | ||
/** | ||
* | ||
* @param loggingEvent | ||
* @returns {any} | ||
*/ | ||
public formatMessage = (loggingEvent: LogEvent): string => { | ||
return (LayoutReplacer.formatter as any)(...loggingEvent.data); | ||
}; | ||
|
||
public formatJson = (loggingEvent: LogEvent): string => { | ||
return JSON.stringify(loggingEvent.data); | ||
}; | ||
/** | ||
* | ||
* @returns {string|string} | ||
*/ | ||
public endOfLine = () => { | ||
return LayoutReplacer.EOL; | ||
}; | ||
/** | ||
* | ||
* @param loggingEvent | ||
* @returns {string} | ||
*/ | ||
public logLevel = (loggingEvent: LogEvent): string => { | ||
return loggingEvent.level.toString(); | ||
}; | ||
/** | ||
* | ||
* @param loggingEvent | ||
* @returns {any} | ||
*/ | ||
public startTime = (loggingEvent: LogEvent): string => { | ||
return dateFormat.asString("hh:mm:ss", loggingEvent.startTime, this.timezoneOffset); | ||
}; | ||
/** | ||
* | ||
* @param loggingEvent | ||
* @returns {string} | ||
*/ | ||
public startColour = (loggingEvent: LogEvent): string => { | ||
const index: any = loggingEvent.level.toString(); | ||
return colorizeStart(LOG_COLORS[index as keyof typeof LOG_COLORS]); | ||
}; | ||
/** | ||
* | ||
* @param loggingEvent | ||
* @returns {string} | ||
*/ | ||
public endColour = (loggingEvent: LogEvent): string => { | ||
const index: any = loggingEvent.level.toString(); | ||
return colorizeEnd(LOG_COLORS[index as keyof typeof LOG_COLORS]); | ||
}; | ||
/** | ||
* | ||
* @returns {string} | ||
*/ | ||
public percent = () => { | ||
return "%"; | ||
}; | ||
/** | ||
* | ||
* @param loggingEvent | ||
* @returns {string} | ||
*/ | ||
public pid = (loggingEvent?: LogEvent): string => { | ||
return loggingEvent && loggingEvent.pid ? loggingEvent.pid.toString() : process.pid.toString(); | ||
}; | ||
/** | ||
* | ||
* @param loggingEvent | ||
* @param specifier | ||
* @returns {any} | ||
*/ | ||
public clusterInfo = (loggingEvent: LogEvent, specifier: string) => { | ||
if (loggingEvent.cluster && specifier) { | ||
return specifier | ||
.replace("%m", loggingEvent.cluster.master) | ||
.replace("%w", loggingEvent.cluster.worker) | ||
.replace("%i", loggingEvent.cluster.workerId); | ||
} else if (loggingEvent.cluster) { | ||
return `${loggingEvent.cluster.worker}@${loggingEvent.cluster.master}`; | ||
} | ||
|
||
return this.pid(); | ||
}; | ||
/** | ||
* | ||
* @param loggingEvent | ||
* @param specifier | ||
* @returns {any} | ||
*/ | ||
public userDefined = (loggingEvent: LogEvent, specifier: string) => { | ||
if (typeof this.tokens[specifier] !== "undefined") { | ||
return typeof this.tokens[specifier] === "function" ? this.tokens[specifier](loggingEvent) : this.tokens[specifier]; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
build(): IReplacers { | ||
return { | ||
c: this.categoryName, | ||
d: this.formatAsDate, | ||
h: this.hostname, | ||
m: this.formatMessage, | ||
j: this.formatJson, | ||
n: this.endOfLine, | ||
p: this.logLevel, | ||
r: this.startTime, | ||
"[": this.startColour, | ||
"]": this.endColour, | ||
y: this.clusterInfo, | ||
z: this.pid, | ||
"%": this.percent, | ||
x: this.userDefined | ||
}; | ||
} | ||
} |