1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.DiagAPI = void 0;
|
19 | const ComponentLogger_1 = require("../diag/ComponentLogger");
|
20 | const logLevelLogger_1 = require("../diag/internal/logLevelLogger");
|
21 | const types_1 = require("../diag/types");
|
22 | const global_utils_1 = require("../internal/global-utils");
|
23 | const API_NAME = 'diag';
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | class DiagAPI {
|
29 | |
30 |
|
31 |
|
32 |
|
33 | constructor() {
|
34 | function _logProxy(funcName) {
|
35 | return function (...args) {
|
36 | const logger = (0, global_utils_1.getGlobal)('diag');
|
37 |
|
38 | if (!logger)
|
39 | return;
|
40 | return logger[funcName](...args);
|
41 | };
|
42 | }
|
43 |
|
44 | const self = this;
|
45 |
|
46 | const setLogger = (logger, optionsOrLogLevel = { logLevel: types_1.DiagLogLevel.INFO }) => {
|
47 | var _a, _b, _c;
|
48 | if (logger === self) {
|
49 |
|
50 |
|
51 |
|
52 | const err = new Error('Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation');
|
53 | self.error((_a = err.stack) !== null && _a !== void 0 ? _a : err.message);
|
54 | return false;
|
55 | }
|
56 | if (typeof optionsOrLogLevel === 'number') {
|
57 | optionsOrLogLevel = {
|
58 | logLevel: optionsOrLogLevel,
|
59 | };
|
60 | }
|
61 | const oldLogger = (0, global_utils_1.getGlobal)('diag');
|
62 | const newLogger = (0, logLevelLogger_1.createLogLevelDiagLogger)((_b = optionsOrLogLevel.logLevel) !== null && _b !== void 0 ? _b : types_1.DiagLogLevel.INFO, logger);
|
63 |
|
64 | if (oldLogger && !optionsOrLogLevel.suppressOverrideMessage) {
|
65 | const stack = (_c = new Error().stack) !== null && _c !== void 0 ? _c : '<failed to generate stacktrace>';
|
66 | oldLogger.warn(`Current logger will be overwritten from ${stack}`);
|
67 | newLogger.warn(`Current logger will overwrite one already registered from ${stack}`);
|
68 | }
|
69 | return (0, global_utils_1.registerGlobal)('diag', newLogger, self, true);
|
70 | };
|
71 | self.setLogger = setLogger;
|
72 | self.disable = () => {
|
73 | (0, global_utils_1.unregisterGlobal)(API_NAME, self);
|
74 | };
|
75 | self.createComponentLogger = (options) => {
|
76 | return new ComponentLogger_1.DiagComponentLogger(options);
|
77 | };
|
78 | self.verbose = _logProxy('verbose');
|
79 | self.debug = _logProxy('debug');
|
80 | self.info = _logProxy('info');
|
81 | self.warn = _logProxy('warn');
|
82 | self.error = _logProxy('error');
|
83 | }
|
84 |
|
85 | static instance() {
|
86 | if (!this._instance) {
|
87 | this._instance = new DiagAPI();
|
88 | }
|
89 | return this._instance;
|
90 | }
|
91 | }
|
92 | exports.DiagAPI = DiagAPI;
|
93 |
|
\ | No newline at end of file |