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