UNPKG

3.88 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2019 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18var _a, _b, _c, _d;
19Object.defineProperty(exports, "__esModule", { value: true });
20exports.isTracerEnabled = exports.trace = exports.log = exports.setLoggerVerbosity = exports.setLogger = exports.getLogger = void 0;
21const constants_1 = require("./constants");
22const DEFAULT_LOGGER = {
23 error: (message, ...optionalParams) => {
24 console.error('E ' + message, ...optionalParams);
25 },
26 info: (message, ...optionalParams) => {
27 console.error('I ' + message, ...optionalParams);
28 },
29 debug: (message, ...optionalParams) => {
30 console.error('D ' + message, ...optionalParams);
31 },
32};
33let _logger = DEFAULT_LOGGER;
34let _logVerbosity = constants_1.LogVerbosity.ERROR;
35const verbosityString = (_b = (_a = process.env.GRPC_NODE_VERBOSITY) !== null && _a !== void 0 ? _a : process.env.GRPC_VERBOSITY) !== null && _b !== void 0 ? _b : '';
36switch (verbosityString.toUpperCase()) {
37 case 'DEBUG':
38 _logVerbosity = constants_1.LogVerbosity.DEBUG;
39 break;
40 case 'INFO':
41 _logVerbosity = constants_1.LogVerbosity.INFO;
42 break;
43 case 'ERROR':
44 _logVerbosity = constants_1.LogVerbosity.ERROR;
45 break;
46 case 'NONE':
47 _logVerbosity = constants_1.LogVerbosity.NONE;
48 break;
49 default:
50 // Ignore any other values
51}
52exports.getLogger = () => {
53 return _logger;
54};
55exports.setLogger = (logger) => {
56 _logger = logger;
57};
58exports.setLoggerVerbosity = (verbosity) => {
59 _logVerbosity = verbosity;
60};
61// eslint-disable-next-line @typescript-eslint/no-explicit-any
62exports.log = (severity, ...args) => {
63 let logFunction;
64 if (severity >= _logVerbosity) {
65 switch (severity) {
66 case constants_1.LogVerbosity.DEBUG:
67 logFunction = _logger.debug;
68 break;
69 case constants_1.LogVerbosity.INFO:
70 logFunction = _logger.info;
71 break;
72 case constants_1.LogVerbosity.ERROR:
73 logFunction = _logger.error;
74 break;
75 }
76 /* Fall back to _logger.error when other methods are not available for
77 * compatiblity with older behavior that always logged to _logger.error */
78 if (!logFunction) {
79 logFunction = _logger.error;
80 }
81 if (logFunction) {
82 logFunction.bind(_logger)(...args);
83 }
84 }
85};
86const tracersString = (_d = (_c = process.env.GRPC_NODE_TRACE) !== null && _c !== void 0 ? _c : process.env.GRPC_TRACE) !== null && _d !== void 0 ? _d : '';
87const enabledTracers = new Set();
88const disabledTracers = new Set();
89for (const tracerName of tracersString.split(',')) {
90 if (tracerName.startsWith('-')) {
91 disabledTracers.add(tracerName.substring(1));
92 }
93 else {
94 enabledTracers.add(tracerName);
95 }
96}
97const allEnabled = enabledTracers.has('all');
98function trace(severity, tracer, text) {
99 if (isTracerEnabled(tracer)) {
100 exports.log(severity, new Date().toISOString() + ' | ' + tracer + ' | ' + text);
101 }
102}
103exports.trace = trace;
104function isTracerEnabled(tracer) {
105 return !disabledTracers.has(tracer) &&
106 (allEnabled || enabledTracers.has(tracer));
107}
108exports.isTracerEnabled = isTracerEnabled;
109//# sourceMappingURL=logging.js.map
\No newline at end of file