1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | import { LogVerbosity } from './constants';
|
19 |
|
20 | const DEFAULT_LOGGER: Partial<Console> = {
|
21 | error: (message?: any, ...optionalParams: any[]) => {
|
22 | console.error('E ' + message, ...optionalParams);
|
23 | },
|
24 | info: (message?: any, ...optionalParams: any[]) => {
|
25 | console.error('I ' + message, ...optionalParams);
|
26 | },
|
27 | debug: (message?: any, ...optionalParams: any[]) => {
|
28 | console.error('D ' + message, ...optionalParams);
|
29 | },
|
30 | }
|
31 |
|
32 | let _logger: Partial<Console> = DEFAULT_LOGGER;
|
33 | let _logVerbosity: LogVerbosity = LogVerbosity.ERROR;
|
34 |
|
35 | const verbosityString =
|
36 | process.env.GRPC_NODE_VERBOSITY ?? process.env.GRPC_VERBOSITY ?? '';
|
37 |
|
38 | switch (verbosityString.toUpperCase()) {
|
39 | case 'DEBUG':
|
40 | _logVerbosity = LogVerbosity.DEBUG;
|
41 | break;
|
42 | case 'INFO':
|
43 | _logVerbosity = LogVerbosity.INFO;
|
44 | break;
|
45 | case 'ERROR':
|
46 | _logVerbosity = LogVerbosity.ERROR;
|
47 | break;
|
48 | case 'NONE':
|
49 | _logVerbosity = LogVerbosity.NONE;
|
50 | break;
|
51 | default:
|
52 |
|
53 | }
|
54 |
|
55 | export const getLogger = (): Partial<Console> => {
|
56 | return _logger;
|
57 | };
|
58 |
|
59 | export const setLogger = (logger: Partial<Console>): void => {
|
60 | _logger = logger;
|
61 | };
|
62 |
|
63 | export const setLoggerVerbosity = (verbosity: LogVerbosity): void => {
|
64 | _logVerbosity = verbosity;
|
65 | };
|
66 |
|
67 |
|
68 | export const log = (severity: LogVerbosity, ...args: any[]): void => {
|
69 | let logFunction: typeof DEFAULT_LOGGER.error;
|
70 | if (severity >= _logVerbosity) {
|
71 | switch (severity) {
|
72 | case LogVerbosity.DEBUG:
|
73 | logFunction = _logger.debug;
|
74 | break;
|
75 | case LogVerbosity.INFO:
|
76 | logFunction = _logger.info;
|
77 | break;
|
78 | case LogVerbosity.ERROR:
|
79 | logFunction = _logger.error;
|
80 | break;
|
81 | }
|
82 | |
83 |
|
84 | if (!logFunction) {
|
85 | logFunction = _logger.error;
|
86 | }
|
87 | if (logFunction) {
|
88 | logFunction.bind(_logger)(...args);
|
89 | }
|
90 | }
|
91 | };
|
92 |
|
93 | const tracersString =
|
94 | process.env.GRPC_NODE_TRACE ?? process.env.GRPC_TRACE ?? '';
|
95 | const enabledTracers = new Set<string>();
|
96 | const disabledTracers = new Set<string>();
|
97 | for (const tracerName of tracersString.split(',')) {
|
98 | if (tracerName.startsWith('-')) {
|
99 | disabledTracers.add(tracerName.substring(1));
|
100 | } else {
|
101 | enabledTracers.add(tracerName);
|
102 | }
|
103 | }
|
104 | const allEnabled = enabledTracers.has('all');
|
105 |
|
106 | export function trace(
|
107 | severity: LogVerbosity,
|
108 | tracer: string,
|
109 | text: string
|
110 | ): void {
|
111 | if (isTracerEnabled(tracer)) {
|
112 | log(severity, new Date().toISOString() + ' | ' + tracer + ' | ' + text);
|
113 | }
|
114 | }
|
115 |
|
116 | export function isTracerEnabled(tracer: string): boolean {
|
117 | return !disabledTracers.has(tracer) &&
|
118 | (allEnabled || enabledTracers.has(tracer));
|
119 | }
|