1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
18 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
19 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
20 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
21 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
22 | };
|
23 | var __metadata = (this && this.__metadata) || function (k, v) {
|
24 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
25 | };
|
26 | Object.defineProperty(exports, "__esModule", { value: true });
|
27 | exports.Logger = exports.ILogger = exports.LoggerName = exports.LoggerFactory = exports.setRootLogger = exports.unsetRootLogger = exports.logger = exports.rootLoggerName = exports.LogLevel = void 0;
|
28 | const inversify_1 = require("inversify");
|
29 | const logger_watcher_1 = require("./logger-watcher");
|
30 | const logger_protocol_1 = require("./logger-protocol");
|
31 | Object.defineProperty(exports, "LogLevel", { enumerable: true, get: function () { return logger_protocol_1.LogLevel; } });
|
32 | Object.defineProperty(exports, "rootLoggerName", { enumerable: true, get: function () { return logger_protocol_1.rootLoggerName; } });
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function unsetRootLogger() {
|
39 | if (exports.logger !== undefined) {
|
40 | logger_protocol_1.ConsoleLogger.reset();
|
41 | exports.logger = undefined;
|
42 | }
|
43 | }
|
44 | exports.unsetRootLogger = unsetRootLogger;
|
45 | function setRootLogger(aLogger) {
|
46 | exports.logger = aLogger;
|
47 | const log = (logLevel, message, ...optionalParams) => exports.logger.log(logLevel, message, ...optionalParams);
|
48 | console.error = log.bind(undefined, logger_protocol_1.LogLevel.ERROR);
|
49 | console.warn = log.bind(undefined, logger_protocol_1.LogLevel.WARN);
|
50 | console.info = log.bind(undefined, logger_protocol_1.LogLevel.INFO);
|
51 | console.debug = log.bind(undefined, logger_protocol_1.LogLevel.DEBUG);
|
52 | console.trace = log.bind(undefined, logger_protocol_1.LogLevel.TRACE);
|
53 | console.log = log.bind(undefined, logger_protocol_1.LogLevel.INFO);
|
54 | }
|
55 | exports.setRootLogger = setRootLogger;
|
56 | exports.LoggerFactory = Symbol('LoggerFactory');
|
57 | exports.LoggerName = Symbol('LoggerName');
|
58 | exports.ILogger = Symbol('ILogger');
|
59 | let Logger = class Logger {
|
60 | init() {
|
61 | if (this.name !== logger_protocol_1.rootLoggerName) {
|
62 |
|
63 | this.created = this.server.child(this.name);
|
64 | }
|
65 | else {
|
66 |
|
67 | this.created = Promise.resolve();
|
68 | }
|
69 |
|
70 | this._logLevel = this.created.then(_ => this.server.getLogLevel(this.name));
|
71 |
|
72 | this.loggerWatcher.onLogLevelChanged(event => {
|
73 | this.created.then(() => {
|
74 | if (event.loggerName === this.name) {
|
75 | this._logLevel = Promise.resolve(event.newLogLevel);
|
76 | }
|
77 | });
|
78 | });
|
79 |
|
80 | this.loggerWatcher.onLogConfigChanged(() => {
|
81 | this._logLevel = this.created.then(_ => this.server.getLogLevel(this.name));
|
82 | });
|
83 | }
|
84 | setLogLevel(logLevel) {
|
85 | return new Promise(resolve => {
|
86 | this.created.then(() => {
|
87 | this._logLevel.then(oldLevel => {
|
88 | this.server.setLogLevel(this.name, logLevel).then(() => {
|
89 | this._logLevel = Promise.resolve(logLevel);
|
90 | resolve();
|
91 | });
|
92 | });
|
93 | });
|
94 | });
|
95 | }
|
96 | getLogLevel() {
|
97 | return this._logLevel;
|
98 | }
|
99 | isEnabled(logLevel) {
|
100 | return this._logLevel.then(level => logLevel >= level);
|
101 | }
|
102 | ifEnabled(logLevel) {
|
103 | return new Promise(resolve => this.isEnabled(logLevel).then(enabled => {
|
104 | if (enabled) {
|
105 | resolve();
|
106 | }
|
107 | }));
|
108 | }
|
109 | log(logLevel, arg2, ...params) {
|
110 | return this.getLog(logLevel).then(log => {
|
111 | if (typeof arg2 === 'function') {
|
112 | const loggable = arg2;
|
113 | loggable(log);
|
114 | }
|
115 | else if (arg2) {
|
116 | log(arg2, ...params);
|
117 | }
|
118 | });
|
119 | }
|
120 | getLog(logLevel) {
|
121 | return this.ifEnabled(logLevel).then(() => this.created.then(() => (message, ...params) => this.server.log(this.name, logLevel, this.format(message), params.map(p => this.format(p)))));
|
122 | }
|
123 | format(value) {
|
124 | if (value instanceof Error) {
|
125 | return value.stack || value.toString();
|
126 | }
|
127 | return value;
|
128 | }
|
129 | isTrace() {
|
130 | return this.isEnabled(logger_protocol_1.LogLevel.TRACE);
|
131 | }
|
132 | ifTrace() {
|
133 | return this.ifEnabled(logger_protocol_1.LogLevel.TRACE);
|
134 | }
|
135 | trace(arg, ...params) {
|
136 | return this.log(logger_protocol_1.LogLevel.TRACE, arg, ...params);
|
137 | }
|
138 | isDebug() {
|
139 | return this.isEnabled(logger_protocol_1.LogLevel.DEBUG);
|
140 | }
|
141 | ifDebug() {
|
142 | return this.ifEnabled(logger_protocol_1.LogLevel.DEBUG);
|
143 | }
|
144 | debug(arg, ...params) {
|
145 | return this.log(logger_protocol_1.LogLevel.DEBUG, arg, ...params);
|
146 | }
|
147 | isInfo() {
|
148 | return this.isEnabled(logger_protocol_1.LogLevel.INFO);
|
149 | }
|
150 | ifInfo() {
|
151 | return this.ifEnabled(logger_protocol_1.LogLevel.INFO);
|
152 | }
|
153 | info(arg, ...params) {
|
154 | return this.log(logger_protocol_1.LogLevel.INFO, arg, ...params);
|
155 | }
|
156 | isWarn() {
|
157 | return this.isEnabled(logger_protocol_1.LogLevel.WARN);
|
158 | }
|
159 | ifWarn() {
|
160 | return this.ifEnabled(logger_protocol_1.LogLevel.WARN);
|
161 | }
|
162 | warn(arg, ...params) {
|
163 | return this.log(logger_protocol_1.LogLevel.WARN, arg, ...params);
|
164 | }
|
165 | isError() {
|
166 | return this.isEnabled(logger_protocol_1.LogLevel.ERROR);
|
167 | }
|
168 | ifError() {
|
169 | return this.ifEnabled(logger_protocol_1.LogLevel.ERROR);
|
170 | }
|
171 | error(arg, ...params) {
|
172 | return this.log(logger_protocol_1.LogLevel.ERROR, arg, ...params);
|
173 | }
|
174 | isFatal() {
|
175 | return this.isEnabled(logger_protocol_1.LogLevel.FATAL);
|
176 | }
|
177 | ifFatal() {
|
178 | return this.ifEnabled(logger_protocol_1.LogLevel.FATAL);
|
179 | }
|
180 | fatal(arg, ...params) {
|
181 | return this.log(logger_protocol_1.LogLevel.FATAL, arg, ...params);
|
182 | }
|
183 | child(name) {
|
184 | return this.factory(name);
|
185 | }
|
186 | };
|
187 | __decorate([
|
188 | (0, inversify_1.inject)(logger_protocol_1.ILoggerServer),
|
189 | __metadata("design:type", Object)
|
190 | ], Logger.prototype, "server", void 0);
|
191 | __decorate([
|
192 | (0, inversify_1.inject)(logger_watcher_1.LoggerWatcher),
|
193 | __metadata("design:type", logger_watcher_1.LoggerWatcher)
|
194 | ], Logger.prototype, "loggerWatcher", void 0);
|
195 | __decorate([
|
196 | (0, inversify_1.inject)(exports.LoggerFactory),
|
197 | __metadata("design:type", Function)
|
198 | ], Logger.prototype, "factory", void 0);
|
199 | __decorate([
|
200 | (0, inversify_1.inject)(exports.LoggerName),
|
201 | __metadata("design:type", String)
|
202 | ], Logger.prototype, "name", void 0);
|
203 | __decorate([
|
204 | (0, inversify_1.postConstruct)(),
|
205 | __metadata("design:type", Function),
|
206 | __metadata("design:paramtypes", []),
|
207 | __metadata("design:returntype", void 0)
|
208 | ], Logger.prototype, "init", null);
|
209 | Logger = __decorate([
|
210 | (0, inversify_1.injectable)()
|
211 | ], Logger);
|
212 | exports.Logger = Logger;
|
213 |
|
\ | No newline at end of file |