UNPKG

3.63 kBPlain TextView Raw
1/*
2 * Copyright 2019 gRPC authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18import { LogVerbosity } from './constants';
19import { pid } from 'process';
20
21const clientVersion = require('../../package.json').version;
22
23const 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
35let _logger: Partial<Console> = DEFAULT_LOGGER;
36let _logVerbosity: LogVerbosity = LogVerbosity.ERROR;
37
38const verbosityString =
39 process.env.GRPC_NODE_VERBOSITY ?? process.env.GRPC_VERBOSITY ?? '';
40
41switch (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 // Ignore any other values
56}
57
58export const getLogger = (): Partial<Console> => {
59 return _logger;
60};
61
62export const setLogger = (logger: Partial<Console>): void => {
63 _logger = logger;
64};
65
66export const setLoggerVerbosity = (verbosity: LogVerbosity): void => {
67 _logVerbosity = verbosity;
68};
69
70// eslint-disable-next-line @typescript-eslint/no-explicit-any
71export 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 /* Fall back to _logger.error when other methods are not available for
86 * compatiblity with older behavior that always logged to _logger.error */
87 if (!logFunction) {
88 logFunction = _logger.error;
89 }
90 if (logFunction) {
91 logFunction.bind(_logger)(...args);
92 }
93 }
94};
95
96const tracersString =
97 process.env.GRPC_NODE_TRACE ?? process.env.GRPC_TRACE ?? '';
98const enabledTracers = new Set<string>();
99const disabledTracers = new Set<string>();
100for (const tracerName of tracersString.split(',')) {
101 if (tracerName.startsWith('-')) {
102 disabledTracers.add(tracerName.substring(1));
103 } else {
104 enabledTracers.add(tracerName);
105 }
106}
107const allEnabled = enabledTracers.has('all');
108
109export 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
130export function isTracerEnabled(tracer: string): boolean {
131 return (
132 !disabledTracers.has(tracer) && (allEnabled || enabledTracers.has(tracer))
133 );
134}