@alanszp/logger 12.0.0 → 12.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -5
- package/.gitignore +0 -3
- package/.npmignore +0 -3
- package/node_modules/@types/node/LICENSE +0 -21
- package/node_modules/@types/node/README.md +0 -15
- package/node_modules/@types/node/assert/strict.d.ts +0 -8
- package/node_modules/@types/node/assert.d.ts +0 -996
- package/node_modules/@types/node/async_hooks.d.ts +0 -539
- package/node_modules/@types/node/buffer.d.ts +0 -2362
- package/node_modules/@types/node/child_process.d.ts +0 -1540
- package/node_modules/@types/node/cluster.d.ts +0 -432
- package/node_modules/@types/node/console.d.ts +0 -415
- package/node_modules/@types/node/constants.d.ts +0 -19
- package/node_modules/@types/node/crypto.d.ts +0 -4487
- package/node_modules/@types/node/dgram.d.ts +0 -596
- package/node_modules/@types/node/diagnostics_channel.d.ts +0 -545
- package/node_modules/@types/node/dns/promises.d.ts +0 -425
- package/node_modules/@types/node/dns.d.ts +0 -809
- package/node_modules/@types/node/dom-events.d.ts +0 -122
- package/node_modules/@types/node/domain.d.ts +0 -170
- package/node_modules/@types/node/events.d.ts +0 -879
- package/node_modules/@types/node/fs/promises.d.ts +0 -1239
- package/node_modules/@types/node/fs.d.ts +0 -4311
- package/node_modules/@types/node/globals.d.ts +0 -411
- package/node_modules/@types/node/globals.global.d.ts +0 -1
- package/node_modules/@types/node/http.d.ts +0 -1887
- package/node_modules/@types/node/http2.d.ts +0 -2382
- package/node_modules/@types/node/https.d.ts +0 -550
- package/node_modules/@types/node/index.d.ts +0 -88
- package/node_modules/@types/node/inspector.d.ts +0 -2747
- package/node_modules/@types/node/module.d.ts +0 -315
- package/node_modules/@types/node/net.d.ts +0 -949
- package/node_modules/@types/node/os.d.ts +0 -478
- package/node_modules/@types/node/package.json +0 -229
- package/node_modules/@types/node/path.d.ts +0 -191
- package/node_modules/@types/node/perf_hooks.d.ts +0 -645
- package/node_modules/@types/node/process.d.ts +0 -1561
- package/node_modules/@types/node/punycode.d.ts +0 -117
- package/node_modules/@types/node/querystring.d.ts +0 -141
- package/node_modules/@types/node/readline/promises.d.ts +0 -150
- package/node_modules/@types/node/readline.d.ts +0 -539
- package/node_modules/@types/node/repl.d.ts +0 -430
- package/node_modules/@types/node/stream/consumers.d.ts +0 -12
- package/node_modules/@types/node/stream/promises.d.ts +0 -83
- package/node_modules/@types/node/stream/web.d.ts +0 -366
- package/node_modules/@types/node/stream.d.ts +0 -1701
- package/node_modules/@types/node/string_decoder.d.ts +0 -67
- package/node_modules/@types/node/test.d.ts +0 -1465
- package/node_modules/@types/node/timers/promises.d.ts +0 -93
- package/node_modules/@types/node/timers.d.ts +0 -240
- package/node_modules/@types/node/tls.d.ts +0 -1210
- package/node_modules/@types/node/trace_events.d.ts +0 -182
- package/node_modules/@types/node/ts4.8/assert/strict.d.ts +0 -8
- package/node_modules/@types/node/ts4.8/assert.d.ts +0 -996
- package/node_modules/@types/node/ts4.8/async_hooks.d.ts +0 -539
- package/node_modules/@types/node/ts4.8/buffer.d.ts +0 -2362
- package/node_modules/@types/node/ts4.8/child_process.d.ts +0 -1540
- package/node_modules/@types/node/ts4.8/cluster.d.ts +0 -432
- package/node_modules/@types/node/ts4.8/console.d.ts +0 -415
- package/node_modules/@types/node/ts4.8/constants.d.ts +0 -19
- package/node_modules/@types/node/ts4.8/crypto.d.ts +0 -4487
- package/node_modules/@types/node/ts4.8/dgram.d.ts +0 -596
- package/node_modules/@types/node/ts4.8/diagnostics_channel.d.ts +0 -545
- package/node_modules/@types/node/ts4.8/dns/promises.d.ts +0 -425
- package/node_modules/@types/node/ts4.8/dns.d.ts +0 -809
- package/node_modules/@types/node/ts4.8/dom-events.d.ts +0 -122
- package/node_modules/@types/node/ts4.8/domain.d.ts +0 -170
- package/node_modules/@types/node/ts4.8/events.d.ts +0 -879
- package/node_modules/@types/node/ts4.8/fs/promises.d.ts +0 -1239
- package/node_modules/@types/node/ts4.8/fs.d.ts +0 -4311
- package/node_modules/@types/node/ts4.8/globals.d.ts +0 -411
- package/node_modules/@types/node/ts4.8/globals.global.d.ts +0 -1
- package/node_modules/@types/node/ts4.8/http.d.ts +0 -1887
- package/node_modules/@types/node/ts4.8/http2.d.ts +0 -2382
- package/node_modules/@types/node/ts4.8/https.d.ts +0 -550
- package/node_modules/@types/node/ts4.8/index.d.ts +0 -88
- package/node_modules/@types/node/ts4.8/inspector.d.ts +0 -2747
- package/node_modules/@types/node/ts4.8/module.d.ts +0 -315
- package/node_modules/@types/node/ts4.8/net.d.ts +0 -949
- package/node_modules/@types/node/ts4.8/os.d.ts +0 -478
- package/node_modules/@types/node/ts4.8/path.d.ts +0 -191
- package/node_modules/@types/node/ts4.8/perf_hooks.d.ts +0 -645
- package/node_modules/@types/node/ts4.8/process.d.ts +0 -1561
- package/node_modules/@types/node/ts4.8/punycode.d.ts +0 -117
- package/node_modules/@types/node/ts4.8/querystring.d.ts +0 -141
- package/node_modules/@types/node/ts4.8/readline/promises.d.ts +0 -150
- package/node_modules/@types/node/ts4.8/readline.d.ts +0 -539
- package/node_modules/@types/node/ts4.8/repl.d.ts +0 -430
- package/node_modules/@types/node/ts4.8/stream/consumers.d.ts +0 -12
- package/node_modules/@types/node/ts4.8/stream/promises.d.ts +0 -83
- package/node_modules/@types/node/ts4.8/stream/web.d.ts +0 -366
- package/node_modules/@types/node/ts4.8/stream.d.ts +0 -1701
- package/node_modules/@types/node/ts4.8/string_decoder.d.ts +0 -67
- package/node_modules/@types/node/ts4.8/test.d.ts +0 -1465
- package/node_modules/@types/node/ts4.8/timers/promises.d.ts +0 -93
- package/node_modules/@types/node/ts4.8/timers.d.ts +0 -240
- package/node_modules/@types/node/ts4.8/tls.d.ts +0 -1210
- package/node_modules/@types/node/ts4.8/trace_events.d.ts +0 -182
- package/node_modules/@types/node/ts4.8/tty.d.ts +0 -208
- package/node_modules/@types/node/ts4.8/url.d.ts +0 -927
- package/node_modules/@types/node/ts4.8/util.d.ts +0 -2183
- package/node_modules/@types/node/ts4.8/v8.d.ts +0 -764
- package/node_modules/@types/node/ts4.8/vm.d.ts +0 -903
- package/node_modules/@types/node/ts4.8/wasi.d.ts +0 -179
- package/node_modules/@types/node/ts4.8/worker_threads.d.ts +0 -691
- package/node_modules/@types/node/ts4.8/zlib.d.ts +0 -517
- package/node_modules/@types/node/tty.d.ts +0 -208
- package/node_modules/@types/node/url.d.ts +0 -927
- package/node_modules/@types/node/util.d.ts +0 -2183
- package/node_modules/@types/node/v8.d.ts +0 -764
- package/node_modules/@types/node/vm.d.ts +0 -903
- package/node_modules/@types/node/wasi.d.ts +0 -179
- package/node_modules/@types/node/worker_threads.d.ts +0 -691
- package/node_modules/@types/node/zlib.d.ts +0 -517
- package/src/config.ts +0 -34
- package/src/factory.ts +0 -68
- package/src/index.ts +0 -5
- package/src/interfaces.ts +0 -26
- package/src/logger.ts +0 -54
- package/src/mock.ts +0 -35
- package/src/serializer.ts +0 -65
- package/src/slackBunyan.ts +0 -133
package/src/logger.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import * as BunyanLogger from "bunyan";
|
|
2
|
-
import { Context, ILogger, LogLevel } from "./interfaces";
|
|
3
|
-
import { serialize } from "./serializer";
|
|
4
|
-
|
|
5
|
-
export class Logger implements ILogger {
|
|
6
|
-
public baseLogger: BunyanLogger;
|
|
7
|
-
public serializers?: BunyanLogger.Serializers;
|
|
8
|
-
|
|
9
|
-
constructor(
|
|
10
|
-
baseLogger: BunyanLogger,
|
|
11
|
-
serializers?: BunyanLogger.Serializers
|
|
12
|
-
) {
|
|
13
|
-
this.baseLogger = baseLogger;
|
|
14
|
-
this.serializers = serializers;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public trace(code: string, context?: Context): void {
|
|
18
|
-
this.log(LogLevel.TRACE, code, context);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public debug(code: string, context?: Context) {
|
|
22
|
-
this.log(LogLevel.DEBUG, code, context);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public info(code: string, context?: Context) {
|
|
26
|
-
this.log(LogLevel.INFO, code, context);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public warn(code: string, context?: Context) {
|
|
30
|
-
this.log(LogLevel.WARN, code, context);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
public error(code: string, context?: Context) {
|
|
34
|
-
this.log(LogLevel.ERROR, code, context);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public child(context?: Context): Logger {
|
|
38
|
-
return new Logger(this.baseLogger.child(context || {}));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
private log(level: LogLevel, code: string, context: Context = {}): void {
|
|
42
|
-
if (this.serializers) {
|
|
43
|
-
context = { ...context };
|
|
44
|
-
for (const key in this.serializers) {
|
|
45
|
-
try {
|
|
46
|
-
if (context[key]) {
|
|
47
|
-
context[key] = this.serializers[key](context[key]);
|
|
48
|
-
}
|
|
49
|
-
} catch {}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
this.baseLogger[level](serialize(context), code);
|
|
53
|
-
}
|
|
54
|
-
}
|
package/src/mock.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { Context, ILogger, LogLevel } from "./interfaces";
|
|
2
|
-
|
|
3
|
-
export function createMockLogger(config: any): ILogger {
|
|
4
|
-
return new MockLogger();
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export class MockLogger implements ILogger {
|
|
8
|
-
public log(level: LogLevel, code: string, context: Context = {}) {
|
|
9
|
-
return;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
public trace(code: string, context?: Context) {
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
public debug(code: string, context?: Context) {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
public info(code: string, context?: Context) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public warn(code: string, context?: Context) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
public error(code: string, context?: Context) {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
public child(context?: Context) {
|
|
33
|
-
return this;
|
|
34
|
-
}
|
|
35
|
-
}
|
package/src/serializer.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import JSON5 from "json5";
|
|
2
|
-
import { isString, keys, mapValues } from "lodash";
|
|
3
|
-
import { Context } from "./interfaces";
|
|
4
|
-
|
|
5
|
-
export function stringify(obj: unknown): string {
|
|
6
|
-
if (isString(obj)) {
|
|
7
|
-
return obj;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
if (obj instanceof Error) {
|
|
11
|
-
obj = errorSerializer(obj);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
try {
|
|
15
|
-
return JSON5.stringify(obj);
|
|
16
|
-
} catch (e) {
|
|
17
|
-
// If we cant stringify we will let the bunyan serializer to make the magic.
|
|
18
|
-
// This is just to detect the logs that are having trouble and then fix it
|
|
19
|
-
// in the log statement. (The error this may produce is due to circular
|
|
20
|
-
// reference, so this is a dev issue, who is not logging in a proper way)
|
|
21
|
-
return stringify({
|
|
22
|
-
parsing_error: e.message,
|
|
23
|
-
object_keys: keys(obj),
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function serialize(obj: Context) {
|
|
29
|
-
return mapValues(obj, stringify);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/* Based on Bunyan error serializer */
|
|
33
|
-
function getFullErrorStack(ex): string {
|
|
34
|
-
let ret = ex.stack || ex.toString();
|
|
35
|
-
if (ex.cause && typeof ex.cause === "function") {
|
|
36
|
-
const cex = ex.cause();
|
|
37
|
-
if (cex) {
|
|
38
|
-
ret += `\nCaused by: ${getFullErrorStack(cex)}`;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return ret;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/* Based on Bunyan error serializer */
|
|
45
|
-
function errorSerializer(err) {
|
|
46
|
-
if (!err || !err.stack) {
|
|
47
|
-
return err;
|
|
48
|
-
}
|
|
49
|
-
return {
|
|
50
|
-
name: err.name,
|
|
51
|
-
message: err.message,
|
|
52
|
-
code: functionOrPropValue(err, "code"),
|
|
53
|
-
context: functionOrPropValue(err, "context"),
|
|
54
|
-
response: err.name === "Non200ResponseError" ? err.response : undefined,
|
|
55
|
-
renderMessage: functionOrPropValue(err, "renderMessage"),
|
|
56
|
-
status: err.status,
|
|
57
|
-
signal: err.signal,
|
|
58
|
-
stack: getFullErrorStack(err),
|
|
59
|
-
subError: errorSerializer(err.error),
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function functionOrPropValue(elem, key) {
|
|
64
|
-
return typeof elem[key] === "function" ? elem[key]() : elem[key];
|
|
65
|
-
}
|
package/src/slackBunyan.ts
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import AxiosGlobal from "axios";
|
|
2
|
-
import { isDate, reduce } from "lodash";
|
|
3
|
-
import { serialize, stringify } from "./serializer";
|
|
4
|
-
|
|
5
|
-
const axios = AxiosGlobal.create();
|
|
6
|
-
|
|
7
|
-
const COLOR_FROM_LEVEL = {
|
|
8
|
-
10: "#c4c4c4",
|
|
9
|
-
20: "#00ab00",
|
|
10
|
-
30: "#0066E7",
|
|
11
|
-
40: "#F18009",
|
|
12
|
-
50: "#D4070F",
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const REMOVABLE_FIELDS = {
|
|
16
|
-
v: true,
|
|
17
|
-
name: true,
|
|
18
|
-
level: true,
|
|
19
|
-
msg: true,
|
|
20
|
-
time: true,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export interface SlackStreamOptions {
|
|
24
|
-
webhookUrl: string;
|
|
25
|
-
channel?: string;
|
|
26
|
-
username?: string;
|
|
27
|
-
emojiIcon?: string;
|
|
28
|
-
onError?: (error: Error, record: unknown) => void;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export class SlackStream {
|
|
32
|
-
private webhookUrl: string;
|
|
33
|
-
private onError: (error: Error, record: unknown) => void;
|
|
34
|
-
private channel?: string;
|
|
35
|
-
private username?: string;
|
|
36
|
-
private emojiIcon?: string;
|
|
37
|
-
|
|
38
|
-
constructor(options: SlackStreamOptions) {
|
|
39
|
-
if (!options?.webhookUrl) {
|
|
40
|
-
throw new Error("webhookUrl and channel must be given");
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
this.channel = options.channel;
|
|
44
|
-
this.webhookUrl = options.webhookUrl;
|
|
45
|
-
this.username = options.username || "Logger";
|
|
46
|
-
this.emojiIcon = options.emojiIcon || ":warning:";
|
|
47
|
-
this.onError =
|
|
48
|
-
options.onError ||
|
|
49
|
-
function (error, record) {
|
|
50
|
-
console.log(
|
|
51
|
-
JSON.stringify(
|
|
52
|
-
serialize({
|
|
53
|
-
level: 50,
|
|
54
|
-
msg: "_slack_serializer.error",
|
|
55
|
-
error,
|
|
56
|
-
record,
|
|
57
|
-
})
|
|
58
|
-
)
|
|
59
|
-
);
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public write(record: any): void {
|
|
64
|
-
record = record || {};
|
|
65
|
-
try {
|
|
66
|
-
const color = COLOR_FROM_LEVEL[record.level];
|
|
67
|
-
const log = reduce(
|
|
68
|
-
record,
|
|
69
|
-
(acc, value, key) => {
|
|
70
|
-
if (!REMOVABLE_FIELDS[key]) {
|
|
71
|
-
acc[key] = value;
|
|
72
|
-
}
|
|
73
|
-
return acc;
|
|
74
|
-
},
|
|
75
|
-
{}
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
const body = {
|
|
79
|
-
channel: this.channel,
|
|
80
|
-
username: this.username,
|
|
81
|
-
icon_emoji: this.emojiIcon,
|
|
82
|
-
attachments: [
|
|
83
|
-
{
|
|
84
|
-
color,
|
|
85
|
-
blocks: [
|
|
86
|
-
{
|
|
87
|
-
type: "section",
|
|
88
|
-
fields: [
|
|
89
|
-
{
|
|
90
|
-
type: "mrkdwn",
|
|
91
|
-
text: `*Message*\n${record.msg}`,
|
|
92
|
-
},
|
|
93
|
-
{
|
|
94
|
-
type: "mrkdwn",
|
|
95
|
-
text: `*Application:*\n${record.name}:${process.env.NODE_ENV}`,
|
|
96
|
-
},
|
|
97
|
-
{
|
|
98
|
-
type: "mrkdwn",
|
|
99
|
-
text: `*Lifecycle ID*\n${record.lid || "-"}`,
|
|
100
|
-
},
|
|
101
|
-
{
|
|
102
|
-
type: "mrkdwn",
|
|
103
|
-
text: `*Time:*\n${
|
|
104
|
-
isDate(record.time)
|
|
105
|
-
? record.time.toISOString()
|
|
106
|
-
: record.time
|
|
107
|
-
}`,
|
|
108
|
-
},
|
|
109
|
-
],
|
|
110
|
-
},
|
|
111
|
-
{
|
|
112
|
-
type: "section",
|
|
113
|
-
text: {
|
|
114
|
-
type: "mrkdwn",
|
|
115
|
-
text: "```" + stringify(serialize(log)) + "```",
|
|
116
|
-
},
|
|
117
|
-
},
|
|
118
|
-
],
|
|
119
|
-
},
|
|
120
|
-
],
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
axios.post(this.webhookUrl, body).catch((error) =>
|
|
124
|
-
this.onError(error, {
|
|
125
|
-
slackResponse: error.response ? error.response.data : null,
|
|
126
|
-
record,
|
|
127
|
-
})
|
|
128
|
-
);
|
|
129
|
-
} catch (err) {
|
|
130
|
-
this.onError(err, record);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|