UNPKG

3.41 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';
19
20const 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
32let _logger: Partial<Console> = DEFAULT_LOGGER;
33let _logVerbosity: LogVerbosity = LogVerbosity.ERROR;
34
35const verbosityString =
36 process.env.GRPC_NODE_VERBOSITY ?? process.env.GRPC_VERBOSITY ?? '';
37
38switch (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 // Ignore any other values
53}
54
55export const getLogger = (): Partial<Console> => {
56 return _logger;
57};
58
59export const setLogger = (logger: Partial<Console>): void => {
60 _logger = logger;
61};
62
63export const setLoggerVerbosity = (verbosity: LogVerbosity): void => {
64 _logVerbosity = verbosity;
65};
66
67// eslint-disable-next-line @typescript-eslint/no-explicit-any
68export 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 /* Fall back to _logger.error when other methods are not available for
83 * compatiblity with older behavior that always logged to _logger.error */
84 if (!logFunction) {
85 logFunction = _logger.error;
86 }
87 if (logFunction) {
88 logFunction.bind(_logger)(...args);
89 }
90 }
91};
92
93const tracersString =
94 process.env.GRPC_NODE_TRACE ?? process.env.GRPC_TRACE ?? '';
95const enabledTracers = new Set<string>();
96const disabledTracers = new Set<string>();
97for (const tracerName of tracersString.split(',')) {
98 if (tracerName.startsWith('-')) {
99 disabledTracers.add(tracerName.substring(1));
100 } else {
101 enabledTracers.add(tracerName);
102 }
103}
104const allEnabled = enabledTracers.has('all');
105
106export 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
116export function isTracerEnabled(tracer: string): boolean {
117 return !disabledTracers.has(tracer) &&
118 (allEnabled || enabledTracers.has(tracer));
119}