-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathlogger.ts
More file actions
129 lines (109 loc) · 3.71 KB
/
Copy pathlogger.ts
File metadata and controls
129 lines (109 loc) · 3.71 KB
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
import { APP, LOG_CLIENT_INIT_TIMEOUT_MS } from '../../constants'
import { cleanError } from '../../utils/error'
import { cleanLog } from '../../utils/log'
import { Types } from '@vtex/diagnostics-nodejs';
import { LoggerContext, LogLevel, TracingState } from './loggerTypes'
import { getLogClient } from './client'
const app = APP.ID
const EMPTY_MESSAGE = 'Logger.log was called with null or undefined message'
export class Logger {
private account: string
private workspace: string
private operationId: string
private requestId: string
private production: boolean
private tracingState?: TracingState
private logClient: Types.LogClient | undefined = undefined
private clientInitPromise: Promise<Types.LogClient | undefined> | undefined = undefined
constructor(ctx: LoggerContext) {
this.account = ctx.account
this.workspace = ctx.workspace
this.requestId = ctx.requestId
this.operationId = ctx.operationId
this.production = ctx.production
if(ctx.tracer) {
this.tracingState = {
isTraceSampled: ctx.tracer.isTraceSampled,
traceId: ctx.tracer.traceId,
}
}
// this.initLogClient();
}
private initLogClient(): Promise<Types.LogClient | undefined> {
if (this.clientInitPromise) {
return this.clientInitPromise;
}
this.clientInitPromise = (async () => {
try {
const timeoutPromise = new Promise<never>((_, reject) => {
setTimeout(() => reject(new Error('Log client initialization timeout')), LOG_CLIENT_INIT_TIMEOUT_MS);
});
this.logClient = await Promise.race([
getLogClient(),
timeoutPromise
]);
return this.logClient;
} catch (error) {
console.error('Failed to initialize log client:', error);
return undefined;
}
})();
return this.clientInitPromise;
}
public debug = (message: any) =>
this.log(message, LogLevel.Debug)
public info = (message: any) =>
this.log(message, LogLevel.Info)
public warn = (warning: any) =>
this.log(warning, LogLevel.Warn)
public error = (error: any) =>
this.log(error, LogLevel.Error)
public log = (message: any, level: LogLevel): void => {
const data = message ? cleanError(message) : EMPTY_MESSAGE
cleanLog(data)
/* tslint:disable:object-literal-sort-keys */
const inflatedLog = {
__VTEX_IO_LOG: true,
level,
app,
account: this.account,
workspace: this.workspace,
production: this.production,
data,
operationId: this.operationId,
requestId: this.requestId,
... (this.tracingState?.isTraceSampled ? { traceId: this.tracingState.traceId } : null),
}
// Mark third-party apps logs to send to skidder
if (APP.IS_THIRD_PARTY()) {
Object.assign(inflatedLog, {
'__SKIDDER_TOPIC_1': `skidder.vendor.${APP.VENDOR}`,
'__SKIDDER_TOPIC_2': `skidder.app.${APP.VENDOR}.${APP.NAME}`,
});
}
if (this.logClient) {
try {
let logMessage = typeof data === 'string' ? data : JSON.stringify(data)
switch (level) {
case LogLevel.Debug:
this.logClient.debug(logMessage, inflatedLog);
break;
case LogLevel.Info:
this.logClient.info(logMessage, inflatedLog);
break;
case LogLevel.Warn:
this.logClient.warn(logMessage, inflatedLog);
break;
case LogLevel.Error:
this.logClient.error(logMessage, inflatedLog);
break;
default:
this.logClient.info(logMessage, inflatedLog);
}
} catch (e) {
console.error('Error using diagnostics client for logging:', e);
}
}
console.log(JSON.stringify(inflatedLog))
}
}