-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathindex.ts
181 lines (164 loc) · 5.97 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { hostname } from "os";
import { normalize as fileNormalize } from "path";
import { types, formatWithOptions, InspectOptions } from "util";
import { IRuntime, ILogObjMeta, ISettings, IStackFrame } from "../../interfaces.js";
import { formatTemplate } from "../../formatTemplate.js";
export { InspectOptions };
export default {
getCallerStackFrame,
getErrorTrace,
getMeta,
transportJSON,
transportFormatted,
isBuffer,
isError,
prettyFormatLogObj,
prettyFormatErrorObj,
} as IRuntime;
export interface IMetaStatic {
name?: string;
parentNames?: string[];
runtime: string;
runtimeVersion: string;
hostname?: string;
}
export interface IMeta extends IMetaStatic {
date: Date;
logLevelId: number;
logLevelName: string;
path?: IStackFrame;
}
const meta: IMetaStatic = {
runtime: "Nodejs",
runtimeVersion: process?.version,
hostname: hostname ? hostname() : undefined,
};
export function getMeta(
logLevelId: number,
logLevelName: string,
stackDepthLevel: number,
hideLogPositionForPerformance: boolean,
name?: string,
parentNames?: string[]
): IMeta {
// faster than spread operator
return Object.assign({}, meta, {
name,
parentNames,
date: new Date(),
logLevelId,
logLevelName,
path: !hideLogPositionForPerformance ? getCallerStackFrame(stackDepthLevel) : undefined,
}) as IMeta;
}
export function getCallerStackFrame(stackDepthLevel: number, error: Error = Error()): IStackFrame {
return stackLineToStackFrame((error as Error | undefined)?.stack?.split("\n")?.filter((thisLine: string) => thisLine.includes(" at "))?.[stackDepthLevel]);
}
export function getErrorTrace(error: Error): IStackFrame[] {
return (error as Error)?.stack?.split("\n")?.reduce((result: IStackFrame[], line: string) => {
if (line.includes(" at ")) {
result.push(stackLineToStackFrame(line));
}
return result;
}, []) as IStackFrame[];
}
function stackLineToStackFrame(line?: string): IStackFrame {
const pathResult: IStackFrame = {
fullFilePath: undefined,
fileName: undefined,
fileNameWithLine: undefined,
fileColumn: undefined,
fileLine: undefined,
filePath: undefined,
filePathWithLine: undefined,
method: undefined,
};
if (line != null && line.includes(" at ")) {
line = line.replace(/^\s+at\s+/gm, "");
const errorStackLine = line.split(" (");
const fullFilePath = line?.slice(-1) === ")" ? line?.match(/\(([^)]+)\)/)?.[1] : line;
const pathArray = fullFilePath?.includes(":") ? fullFilePath?.replace("file://", "")?.replace(process.cwd(), "")?.split(":") : undefined;
// order plays a role, runs from the back: column, line, path
const fileColumn = pathArray?.pop();
const fileLine = pathArray?.pop();
const filePath = pathArray?.pop();
const filePathWithLine = fileNormalize(`${filePath}:${fileLine}`);
const fileName = filePath?.split("/")?.pop();
const fileNameWithLine = `${fileName}:${fileLine}`;
if (filePath != null && filePath.length > 0) {
pathResult.fullFilePath = fullFilePath;
pathResult.fileName = fileName;
pathResult.fileNameWithLine = fileNameWithLine;
pathResult.fileColumn = fileColumn;
pathResult.fileLine = fileLine;
pathResult.filePath = filePath;
pathResult.filePathWithLine = filePathWithLine;
pathResult.method = errorStackLine?.[1] != null ? errorStackLine?.[0] : undefined;
}
}
return pathResult;
}
export function isError(e: Error | unknown): boolean {
// An error could be an instance of Error while not being a native error
// or could be from a different realm and not be instance of Error but still
// be a native error.
return types?.isNativeError != null ? types.isNativeError(e) : e instanceof Error;
}
export function prettyFormatLogObj<LogObj>(maskedArgs: unknown[], settings: ISettings<LogObj>): { args: unknown[]; errors: string[] } {
return maskedArgs.reduce(
(result: { args: unknown[]; errors: string[] }, arg) => {
isError(arg) ? result.errors.push(prettyFormatErrorObj(arg as Error, settings)) : result.args.push(arg);
return result;
},
{ args: [], errors: [] }
);
}
export function prettyFormatErrorObj<LogObj>(error: Error, settings: ISettings<LogObj>): string {
const errorStackStr = getErrorTrace(error as Error).map((stackFrame) => {
return formatTemplate(settings, settings.prettyErrorStackTemplate, { ...stackFrame }, true);
});
const placeholderValuesError = {
errorName: ` ${error.name} `,
errorMessage: Object.getOwnPropertyNames(error)
.reduce((result: string[], key) => {
if (key !== "stack") {
result.push((error as any)[key]);
}
return result;
}, [])
.join(", "),
errorStack: errorStackStr.join("\n"),
};
return formatTemplate(settings, settings.prettyErrorTemplate, placeholderValuesError);
}
export function transportFormatted<LogObj>(logMetaMarkup: string, logArgs: unknown[], logErrors: string[], settings: ISettings<LogObj>): void {
const logErrorsStr = (logErrors.length > 0 && logArgs.length > 0 ? "\n" : "") + logErrors.join("\n");
settings.prettyInspectOptions.colors = settings.stylePrettyLogs;
console.log(logMetaMarkup + formatWithOptions(settings.prettyInspectOptions, ...logArgs) + logErrorsStr);
}
export function transportJSON<LogObj>(json: LogObj & ILogObjMeta): void {
console.log(jsonStringifyRecursive(json));
function jsonStringifyRecursive(obj: unknown) {
const cache = new Set();
return JSON.stringify(obj, (key, value) => {
if (typeof value === "object" && value !== null) {
if (cache.has(value)) {
// Circular reference found, discard key
return "[Circular]";
}
// Store value in our collection
cache.add(value);
}
if (typeof value === "bigint") {
return `${value}`;
}
if (typeof value === "undefined") {
return "[undefined]";
}
return value;
});
}
}
export function isBuffer(arg: unknown) {
return Buffer.isBuffer(arg);
}