Skip to content

Commit

Permalink
Improve performance: make date formatting optional
Browse files Browse the repository at this point in the history
  • Loading branch information
terehov committed Dec 23, 2020
1 parent 8c7c4ac commit 6f32f8b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ common/temp/

# Don't commit tsdoc meta
/src/tsdoc-metadata.json

# For now, ignore banchmarks (WIP)
/benchmarks/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
![tslog pretty output](https://raw.githubusercontent.com/fullstack-build/tslog/master/docs/assets/tslog_pretty_output.png "tslog pretty output")

### Highlights
**Small footprint, blazing performance (native V8 integration)**<br>
**Batteries included, native V8 integration**<br>
👮‍️ **Fully typed with TypeScript support (exact code position)**<br>
🗃 **_Pretty_ or `JSON` output**<br>
⭕️ **Supports _circular_ structures**<br>
Expand Down
66 changes: 41 additions & 25 deletions src/LoggerWithoutCallSite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ export class LoggerWithoutCallSite {
depth: Infinity,
},
delimiter: " ",
dateTimePattern: "year-month-day hour:minute:second.millisecond",
dateTimePattern: undefined,
// local timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
dateTimeTimezone: "utc",
dateTimeTimezone: undefined,

prefix: [],
maskValuesOfKeys: ["password"],
Expand Down Expand Up @@ -366,13 +366,15 @@ export class LoggerWithoutCallSite {
exposeStack: boolean = true
): ILogObject {
const callSites: NodeJS.CallSite[] = LoggerHelper.getCallSites();

const relevantCallSites: NodeJS.CallSite[] = callSites.splice(
this.settings.ignoreStackLevels
);
const stackFrame: NodeJS.CallSite | undefined =
relevantCallSites[0] != null
? this._callSiteWrapper(relevantCallSites[0])
: undefined;

const stackFrameObject: IStackFrame | undefined =
stackFrame != null
? LoggerHelper.toStackFrameObject(stackFrame)
Expand Down Expand Up @@ -497,29 +499,43 @@ export class LoggerWithoutCallSite {
**/
public printPrettyLog(std: IStd, logObject: ILogObject): void {
if (this.settings.displayDateTime === true) {
const dateTimeParts: IFullDateTimeFormatPart[] = [
...(new Intl.DateTimeFormat("en", {
weekday: undefined,
year: "numeric",
month: "2-digit",
day: "2-digit",
hour12: false,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
timeZone: this.settings.dateTimeTimezone,
}).formatToParts(logObject.date) as IFullDateTimeFormatPart[]),
{
type: "millisecond",
value: ("00" + logObject.date.getMilliseconds()).slice(-3),
} as IFullDateTimeFormatPart,
];
let nowStr: string = "";
if (
this.settings.dateTimePattern != null ||
this.settings.dateTimeTimezone != null
) {
const dateTimePattern =
this.settings.dateTimePattern ??
"year-month-day hour:minute:second.millisecond";
const dateTimeTimezone = this.settings.dateTimeTimezone ?? "utc";

const dateTimeParts: IFullDateTimeFormatPart[] = [
...(new Intl.DateTimeFormat("en", {
weekday: undefined,
year: "numeric",
month: "2-digit",
day: "2-digit",
hour12: false,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
timeZone: dateTimeTimezone,
}).formatToParts(logObject.date) as IFullDateTimeFormatPart[]),
{
type: "millisecond",
value: ("00" + logObject.date.getMilliseconds()).slice(-3),
} as IFullDateTimeFormatPart,
];

nowStr = dateTimeParts.reduce(
(prevStr: string, thisStr: IFullDateTimeFormatPart) =>
prevStr.replace(thisStr.type, thisStr.value),
dateTimePattern
);
} else {
nowStr = new Date().toISOString().replace("T", " ").replace("Z", " ");
}

const nowStr: string = dateTimeParts.reduce(
(prevStr: string, thisStr: IFullDateTimeFormatPart) =>
prevStr.replace(thisStr.type, thisStr.value),
this.settings.dateTimePattern
);
std.write(
LoggerHelper.styleString(
["gray"],
Expand Down Expand Up @@ -587,7 +603,7 @@ export class LoggerWithoutCallSite {
fileLocation = `${logObject.filePath}:${logObject.lineNumber}`;
}
const concatenatedMetaLine: string = [name, fileLocation, functionName]
.join("")
.join(" ")
.trim();

if (concatenatedMetaLine.length > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ export interface ISettings extends ISettingsParam {
prettyInspectOptions: InspectOptions;
jsonInspectOptions: InspectOptions;
delimiter: string;
dateTimePattern: string;
dateTimeTimezone: string;
dateTimePattern?: string;
dateTimeTimezone?: string;
prefix: unknown[];
maskValuesOfKeys: (number | string)[];
maskAnyRegEx: (string | number)[];
Expand Down

0 comments on commit 6f32f8b

Please sign in to comment.