-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
132 lines (114 loc) · 2.59 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
import path from "node:path";
import url from "node:url";
import util from "node:util";
import { isNativeError } from "node:util/types";
import type log4js from "log4js";
interface Output {
/**
* ISO-8601 format
*/
time: Date;
/**
* Category name of log4js instance.
*/
category: string;
/**
* log4js.Level.levelStr
*/
level: string;
/**
* Data passed to log argument, formatted using `util.format`.
*/
msg?: string;
/**
* The name of the file where the log message originated
*/
file_name?: string;
/**
* The name of the function where the log message originated
*/
function_name?: string;
}
/**
* Parses the file name from a logging event
*/
function parseFileName(loggingEvent: log4js.LoggingEvent): string | undefined {
let filename = loggingEvent.fileName || "";
if (filename.startsWith("file://")) {
filename = url.fileURLToPath(filename);
}
return filename.split(path.sep).at(-1);
}
/**
* Formats a log event into the desired output format
*/
export function format(event: log4js.LoggingEvent, config?: Config): Output {
const output: Output = {
time: event.startTime,
category: event.categoryName,
level: event.level.levelStr,
};
if (config?.includeContext) {
Object.assign(output, event.context);
}
if (config?.includeFunctionName && event.functionName) {
output.function_name = event.functionName;
}
if (config?.includeFileName) {
const filename = parseFileName(event);
if (filename) {
output.file_name = filename;
}
}
let msgs: Array<any> | undefined;
if (Array.isArray(event.data)) {
msgs = event.data;
} else {
msgs = [event.data];
}
msgs = msgs
.filter((m) => isNativeError(m) || typeof m !== "object")
.filter(Boolean);
output.msg = util.format(...msgs);
if (output.msg === undefined) {
delete output.msg;
}
return output;
}
export interface Config {
/**
* Include context added using `log.addContext()`
*
* @default true
*/
includeContext?: boolean;
/**
* Include file name in json output.
*
* @default false
*/
includeFileName?: boolean;
/**
* Include function name in json output.
*
* @requires log4js>=6.7
* @default false
*/
includeFunctionName?: boolean;
}
/**
* Creates a JSON layout function for log4js.
*/
export function layout(config?: Config): log4js.LayoutFunction {
const defaults = {
includeContext: true,
includeFileName: false,
includeFunctionName: false,
};
const _config = Object.assign({}, defaults, config);
return (event: log4js.LoggingEvent): string => {
const formated = format(event, _config);
const output = JSON.stringify(formated);
return output;
};
}