@alterior/logging 3.13.3 → 3.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +4 -4
- package/dist/index.js +7 -7
- package/dist/inspect.d.ts +28 -28
- package/dist/inspect.js +344 -345
- package/dist/inspect.js.map +1 -1
- package/dist/inspect.test.d.ts +1 -1
- package/dist/logger.d.ts +145 -145
- package/dist/logger.js +331 -331
- package/dist/logger.js.map +1 -1
- package/dist/logger.test.d.ts +1 -1
- package/dist/logger.test.js.map +1 -1
- package/dist/logging.module.d.ts +17 -17
- package/dist/logging.module.js +28 -28
- package/dist/logging.module.js.map +1 -1
- package/dist/test.d.ts +1 -1
- package/dist/trace.d.ts +60 -60
- package/dist/trace.js +214 -215
- package/dist/trace.js.map +1 -1
- package/dist/trace.test.d.ts +1 -1
- package/dist/trace.test.js.map +1 -1
- package/dist.esm/index.d.ts +4 -4
- package/dist.esm/index.js +4 -4
- package/dist.esm/inspect.d.ts +28 -28
- package/dist.esm/inspect.js +339 -339
- package/dist.esm/inspect.js.map +1 -1
- package/dist.esm/inspect.test.d.ts +1 -1
- package/dist.esm/logger.d.ts +145 -145
- package/dist.esm/logger.js +323 -323
- package/dist.esm/logger.js.map +1 -1
- package/dist.esm/logger.test.d.ts +1 -1
- package/dist.esm/logger.test.js.map +1 -1
- package/dist.esm/logging.module.d.ts +17 -17
- package/dist.esm/logging.module.js +25 -25
- package/dist.esm/logging.module.js.map +1 -1
- package/dist.esm/test.d.ts +1 -1
- package/dist.esm/trace.d.ts +60 -60
- package/dist.esm/trace.js +208 -208
- package/dist.esm/trace.js.map +1 -1
- package/dist.esm/trace.test.d.ts +1 -1
- package/dist.esm/trace.test.js.map +1 -1
- package/package.json +10 -10
- package/tsconfig.esm.tsbuildinfo +1 -0
- package/tsconfig.json +0 -2
- package/tsconfig.tsbuildinfo +1 -5735
package/dist/logger.js
CHANGED
|
@@ -1,332 +1,332 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var Logger_1;
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.DEFAULT_LISTENERS = exports.DEFAULT_FORMAT = exports.Logger = exports.ZonedLogger = exports.FileLogger = exports.ConsoleLogger = exports.LogFormatter = exports.LoggingOptionsRef = void 0;
|
|
5
|
-
const tslib_1 = require("tslib");
|
|
6
|
-
const di_1 = require("@alterior/di");
|
|
7
|
-
const runtime_1 = require("@alterior/runtime");
|
|
8
|
-
const fs = tslib_1.__importStar(require("fs"));
|
|
9
|
-
const inspect_1 = require("./inspect");
|
|
10
|
-
class LoggingOptionsRef {
|
|
11
|
-
constructor(options) {
|
|
12
|
-
this.options = options;
|
|
13
|
-
}
|
|
14
|
-
static get currentRef() {
|
|
15
|
-
if (!runtime_1.ExecutionContext.current || !runtime_1.ExecutionContext.current.application)
|
|
16
|
-
return null;
|
|
17
|
-
return runtime_1.ExecutionContext.current.application.inject(LoggingOptionsRef, null);
|
|
18
|
-
}
|
|
19
|
-
static get current() {
|
|
20
|
-
let ref = this.currentRef;
|
|
21
|
-
return ref ? ref.options : {};
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
exports.LoggingOptionsRef = LoggingOptionsRef;
|
|
25
|
-
class LogFormatter {
|
|
26
|
-
constructor(logFormat) {
|
|
27
|
-
this.logFormat = logFormat;
|
|
28
|
-
this.segments = [];
|
|
29
|
-
this.compile();
|
|
30
|
-
}
|
|
31
|
-
compile() {
|
|
32
|
-
if (typeof this.logFormat === 'function')
|
|
33
|
-
return;
|
|
34
|
-
let segment = '';
|
|
35
|
-
let segments = [];
|
|
36
|
-
for (let i = 0, max = this.logFormat.length; i < max; ++i) {
|
|
37
|
-
let char = this.logFormat[i];
|
|
38
|
-
if (char == '%') {
|
|
39
|
-
let lookAhead = this.logFormat.substr(i + 1);
|
|
40
|
-
if (lookAhead.includes('%')) {
|
|
41
|
-
if (segment != '') {
|
|
42
|
-
segments.push({ type: 'raw', value: segment });
|
|
43
|
-
segment = '';
|
|
44
|
-
}
|
|
45
|
-
let parameterName = lookAhead.replace(/%.*$/, '');
|
|
46
|
-
i += parameterName.length + 1;
|
|
47
|
-
segments.push({ type: 'parameter', value: parameterName });
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
segment += char;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
segment += char;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
if (segment !== '') {
|
|
58
|
-
segments.push({ type: 'raw', value: segment });
|
|
59
|
-
}
|
|
60
|
-
this.segments = segments;
|
|
61
|
-
}
|
|
62
|
-
formatParameter(name, value) {
|
|
63
|
-
if (value === true || value === false)
|
|
64
|
-
return value ? '«true»' : '«false»';
|
|
65
|
-
if (value === null)
|
|
66
|
-
return '«null»';
|
|
67
|
-
if (value === undefined)
|
|
68
|
-
return '«undefined»';
|
|
69
|
-
if (value instanceof Date)
|
|
70
|
-
return value.toISOString();
|
|
71
|
-
return value.toString();
|
|
72
|
-
}
|
|
73
|
-
format(message) {
|
|
74
|
-
if (typeof this.logFormat === 'function')
|
|
75
|
-
return this.logFormat(message);
|
|
76
|
-
return this.segments.map(x => x.type == 'parameter' ? this.formatParameter(x.value, message[x.value]) : x.value).join('');
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
exports.LogFormatter = LogFormatter;
|
|
80
|
-
class ConsoleLogger {
|
|
81
|
-
constructor(format) {
|
|
82
|
-
this.format = format;
|
|
83
|
-
this.formatter = new LogFormatter(format);
|
|
84
|
-
}
|
|
85
|
-
log(message) {
|
|
86
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
87
|
-
let messageText = message.message;
|
|
88
|
-
if (message.type === 'inspect') {
|
|
89
|
-
// On the web, take advantage of the interactive
|
|
90
|
-
// inspection facilities
|
|
91
|
-
if (typeof document !== 'undefined') {
|
|
92
|
-
console.dir(message.subject);
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
// Inspect
|
|
96
|
-
messageText = (0, inspect_1.inspect)(message.subject, {
|
|
97
|
-
stylize: inspect_1.stylizeWithConsoleColors
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
let finalMessage = Object.assign({}, message, { message: messageText });
|
|
101
|
-
let finalMessageStr = this.formatter.format(finalMessage);
|
|
102
|
-
console.log(finalMessageStr);
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
exports.ConsoleLogger = ConsoleLogger;
|
|
107
|
-
class FileLogger {
|
|
108
|
-
constructor(format, filename) {
|
|
109
|
-
this.format = format;
|
|
110
|
-
this.filename = filename;
|
|
111
|
-
this.formatter = new LogFormatter(format);
|
|
112
|
-
}
|
|
113
|
-
get ready() {
|
|
114
|
-
return this._fdReady;
|
|
115
|
-
}
|
|
116
|
-
open() {
|
|
117
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
118
|
-
if (this._fdReady)
|
|
119
|
-
return yield this._fdReady;
|
|
120
|
-
return yield (this._fdReady = new Promise((res, rej) => {
|
|
121
|
-
fs.open(this.filename, 'a', (err, fd) => {
|
|
122
|
-
if (err)
|
|
123
|
-
rej(err);
|
|
124
|
-
else
|
|
125
|
-
res(fd);
|
|
126
|
-
});
|
|
127
|
-
}));
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
write(str) {
|
|
131
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
132
|
-
let fd = yield this.open();
|
|
133
|
-
yield new Promise((res, rej) => {
|
|
134
|
-
fs.write(fd, Buffer.from(str), (err, written, buffer) => {
|
|
135
|
-
if (err) {
|
|
136
|
-
rej(err);
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
// sync to flush this to disk right away
|
|
140
|
-
fs.fdatasync(fd, (err) => err ? rej(err) : res());
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
log(message) {
|
|
146
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
147
|
-
let formattedMessage = this.formatter.format(message);
|
|
148
|
-
yield this.write(`${formattedMessage}\n`);
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
exports.FileLogger = FileLogger;
|
|
153
|
-
let ZonedLogger = class ZonedLogger {
|
|
154
|
-
constructor(optionsRef, app, sourceLabel) {
|
|
155
|
-
this.optionsRef = optionsRef;
|
|
156
|
-
this.app = app;
|
|
157
|
-
this._sourceLabel = undefined;
|
|
158
|
-
this._sourceLabel = sourceLabel;
|
|
159
|
-
}
|
|
160
|
-
clone() {
|
|
161
|
-
let logger = new
|
|
162
|
-
Object.keys(this).filter(x => typeof this[x] !== 'function').forEach(key => logger[key] = this[key]);
|
|
163
|
-
return logger;
|
|
164
|
-
}
|
|
165
|
-
get sourceLabel() {
|
|
166
|
-
return this._sourceLabel;
|
|
167
|
-
}
|
|
168
|
-
static get current() {
|
|
169
|
-
var _a, _b, _c, _d;
|
|
170
|
-
return (_d = (_a = Zone.current.get(Logger.ZONE_LOCAL_NAME)) !== null && _a !== void 0 ? _a : (_c = (_b = runtime_1.ExecutionContext.current) === null || _b === void 0 ? void 0 : _b.application) === null || _c === void 0 ? void 0 : _c.inject(Logger)) !== null && _d !== void 0 ? _d : new
|
|
171
|
-
}
|
|
172
|
-
static log(message, options) { this.current.log(message, options); }
|
|
173
|
-
static info(message, options) { this.current.log(message, options); }
|
|
174
|
-
static fatal(message, options) { this.current.fatal(message, options); }
|
|
175
|
-
static debug(message, options) { this.current.log(message, options); }
|
|
176
|
-
static warning(message, options) { this.current.log(message, options); }
|
|
177
|
-
static error(message, options) { this.current.log(message, options); }
|
|
178
|
-
/**
|
|
179
|
-
* Get the listeners for this logger. Usually this is the global listeners, but if the property
|
|
180
|
-
* was previously assigned it will return those listeners.
|
|
181
|
-
*/
|
|
182
|
-
get listeners() {
|
|
183
|
-
var _a, _b, _c, _d;
|
|
184
|
-
if (this.customListeners !== undefined)
|
|
185
|
-
return this.customListeners;
|
|
186
|
-
if ((_b = (_a = this.optionsRef) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.listeners) {
|
|
187
|
-
return this.optionsRef.options.listeners;
|
|
188
|
-
}
|
|
189
|
-
if ((_d = (_c = this.app) === null || _c === void 0 ? void 0 : _c.options) === null || _d === void 0 ? void 0 : _d.silent)
|
|
190
|
-
return [];
|
|
191
|
-
return exports.DEFAULT_LISTENERS;
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Set the log listeners for this specific logger. Set listeners to undefined to use the
|
|
195
|
-
* global listener configuration.
|
|
196
|
-
*/
|
|
197
|
-
set listeners(value) {
|
|
198
|
-
this.customListeners = value;
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Run the given function with this logger as the current logger.
|
|
202
|
-
* Calls to Logger.current will yield the logger for this execution
|
|
203
|
-
* context.
|
|
204
|
-
*/
|
|
205
|
-
run(func) {
|
|
206
|
-
return Zone.current.fork({
|
|
207
|
-
name: `LoggerContext`,
|
|
208
|
-
properties: {
|
|
209
|
-
[Logger.ZONE_LOCAL_NAME]: this
|
|
210
|
-
}
|
|
211
|
-
}).run(func);
|
|
212
|
-
}
|
|
213
|
-
createMessage(message) {
|
|
214
|
-
let contextSummary = this.contextLabel || '';
|
|
215
|
-
if (this._sourceLabel)
|
|
216
|
-
contextSummary = `${contextSummary} » ${this._sourceLabel}`;
|
|
217
|
-
return Object.assign({
|
|
218
|
-
type: 'message',
|
|
219
|
-
context: this.context,
|
|
220
|
-
date: new Date(),
|
|
221
|
-
message,
|
|
222
|
-
contextLabel: this.contextLabel,
|
|
223
|
-
sourceLabel: this._sourceLabel,
|
|
224
|
-
contextSummary: contextSummary
|
|
225
|
-
}, message);
|
|
226
|
-
}
|
|
227
|
-
log(message, options) {
|
|
228
|
-
this.emitLog(this.createMessage({
|
|
229
|
-
message,
|
|
230
|
-
severity: (options ? options.severity : undefined) || 'info'
|
|
231
|
-
}));
|
|
232
|
-
}
|
|
233
|
-
info(message, options) {
|
|
234
|
-
this.log(message, Object.assign({}, options, { severity: 'info' }));
|
|
235
|
-
}
|
|
236
|
-
fatal(message, options) {
|
|
237
|
-
this.log(message, Object.assign({}, options, { severity: 'fatal' }));
|
|
238
|
-
}
|
|
239
|
-
debug(message, options) {
|
|
240
|
-
this.log(message, Object.assign({}, options, { severity: 'debug' }));
|
|
241
|
-
}
|
|
242
|
-
warning(message, options) {
|
|
243
|
-
this.log(message, Object.assign({}, options, { severity: 'warning' }));
|
|
244
|
-
}
|
|
245
|
-
error(message, options) {
|
|
246
|
-
this.log(message, Object.assign({}, options, { severity: 'error' }));
|
|
247
|
-
}
|
|
248
|
-
inspect(object, options) {
|
|
249
|
-
this.emitLog(this.createMessage({
|
|
250
|
-
type: 'inspect',
|
|
251
|
-
severity: (options ? options.severity : undefined) || 'info',
|
|
252
|
-
subject: object,
|
|
253
|
-
message: '' + object
|
|
254
|
-
}));
|
|
255
|
-
}
|
|
256
|
-
emitLog(message) {
|
|
257
|
-
this.listeners.forEach(listener => listener.log(message));
|
|
258
|
-
}
|
|
259
|
-
get context() {
|
|
260
|
-
return Zone.current.get('logContext');
|
|
261
|
-
}
|
|
262
|
-
get contextLabel() {
|
|
263
|
-
return Zone.current.get('logContextLabel');
|
|
264
|
-
}
|
|
265
|
-
withContext(context, label, callback) {
|
|
266
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
267
|
-
let zone = Zone.current.fork({
|
|
268
|
-
name: `LogContextZone: ${label}`,
|
|
269
|
-
properties: {
|
|
270
|
-
logContext: context,
|
|
271
|
-
logContextLabel: label
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
|
-
return yield zone.run(() => callback());
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
|
-
ZonedLogger
|
|
279
|
-
ZonedLogger =
|
|
280
|
-
|
|
281
|
-
tslib_1.
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
let Logger = Logger_1 = class Logger extends ZonedLogger {
|
|
286
|
-
constructor(optionsRef, app) {
|
|
287
|
-
super(optionsRef, app);
|
|
288
|
-
}
|
|
289
|
-
clone() {
|
|
290
|
-
let logger = new Logger_1(this.optionsRef);
|
|
291
|
-
Object.keys(this).filter(x => typeof this[x] !== 'function').forEach(key => logger[key] = this[key]);
|
|
292
|
-
return logger;
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* Create a new logger based on this one with the given listeners. You can also assign the `listeners` array
|
|
296
|
-
* on the instance directly after using clone() or withSource().
|
|
297
|
-
**/
|
|
298
|
-
withListeners(listeners) {
|
|
299
|
-
let logger = this.clone();
|
|
300
|
-
logger.listeners = listeners;
|
|
301
|
-
return logger;
|
|
302
|
-
}
|
|
303
|
-
withSource(sourceLabel) {
|
|
304
|
-
let logger = this.clone();
|
|
305
|
-
if (typeof sourceLabel === 'string') {
|
|
306
|
-
logger._sourceLabel = sourceLabel;
|
|
307
|
-
}
|
|
308
|
-
else if (typeof sourceLabel === 'object') {
|
|
309
|
-
logger._sourceLabel = sourceLabel.constructor.name;
|
|
310
|
-
}
|
|
311
|
-
else {
|
|
312
|
-
logger._sourceLabel = `${sourceLabel}`;
|
|
313
|
-
}
|
|
314
|
-
return logger;
|
|
315
|
-
}
|
|
316
|
-
};
|
|
317
|
-
Logger =
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
tslib_1.
|
|
321
|
-
|
|
322
|
-
]
|
|
323
|
-
|
|
324
|
-
const DEFAULT_FORMAT = (event) => {
|
|
325
|
-
if (event.contextSummary)
|
|
326
|
-
return `${event.date.toISOString()} [${event.contextSummary}] ${event.severity}: ${event.message}`;
|
|
327
|
-
else
|
|
328
|
-
return `${event.date.toISOString()} ${event.severity}: ${event.message}`;
|
|
329
|
-
};
|
|
330
|
-
exports.DEFAULT_FORMAT = DEFAULT_FORMAT;
|
|
331
|
-
exports.DEFAULT_LISTENERS = [new ConsoleLogger(exports.DEFAULT_FORMAT)];
|
|
1
|
+
"use strict";
|
|
2
|
+
var ZonedLogger_1, Logger_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.DEFAULT_LISTENERS = exports.DEFAULT_FORMAT = exports.Logger = exports.ZonedLogger = exports.FileLogger = exports.ConsoleLogger = exports.LogFormatter = exports.LoggingOptionsRef = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const di_1 = require("@alterior/di");
|
|
7
|
+
const runtime_1 = require("@alterior/runtime");
|
|
8
|
+
const fs = tslib_1.__importStar(require("fs"));
|
|
9
|
+
const inspect_1 = require("./inspect");
|
|
10
|
+
class LoggingOptionsRef {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.options = options;
|
|
13
|
+
}
|
|
14
|
+
static get currentRef() {
|
|
15
|
+
if (!runtime_1.ExecutionContext.current || !runtime_1.ExecutionContext.current.application)
|
|
16
|
+
return null;
|
|
17
|
+
return runtime_1.ExecutionContext.current.application.inject(LoggingOptionsRef, null);
|
|
18
|
+
}
|
|
19
|
+
static get current() {
|
|
20
|
+
let ref = this.currentRef;
|
|
21
|
+
return ref ? ref.options : {};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.LoggingOptionsRef = LoggingOptionsRef;
|
|
25
|
+
class LogFormatter {
|
|
26
|
+
constructor(logFormat) {
|
|
27
|
+
this.logFormat = logFormat;
|
|
28
|
+
this.segments = [];
|
|
29
|
+
this.compile();
|
|
30
|
+
}
|
|
31
|
+
compile() {
|
|
32
|
+
if (typeof this.logFormat === 'function')
|
|
33
|
+
return;
|
|
34
|
+
let segment = '';
|
|
35
|
+
let segments = [];
|
|
36
|
+
for (let i = 0, max = this.logFormat.length; i < max; ++i) {
|
|
37
|
+
let char = this.logFormat[i];
|
|
38
|
+
if (char == '%') {
|
|
39
|
+
let lookAhead = this.logFormat.substr(i + 1);
|
|
40
|
+
if (lookAhead.includes('%')) {
|
|
41
|
+
if (segment != '') {
|
|
42
|
+
segments.push({ type: 'raw', value: segment });
|
|
43
|
+
segment = '';
|
|
44
|
+
}
|
|
45
|
+
let parameterName = lookAhead.replace(/%.*$/, '');
|
|
46
|
+
i += parameterName.length + 1;
|
|
47
|
+
segments.push({ type: 'parameter', value: parameterName });
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
segment += char;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
segment += char;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (segment !== '') {
|
|
58
|
+
segments.push({ type: 'raw', value: segment });
|
|
59
|
+
}
|
|
60
|
+
this.segments = segments;
|
|
61
|
+
}
|
|
62
|
+
formatParameter(name, value) {
|
|
63
|
+
if (value === true || value === false)
|
|
64
|
+
return value ? '«true»' : '«false»';
|
|
65
|
+
if (value === null)
|
|
66
|
+
return '«null»';
|
|
67
|
+
if (value === undefined)
|
|
68
|
+
return '«undefined»';
|
|
69
|
+
if (value instanceof Date)
|
|
70
|
+
return value.toISOString();
|
|
71
|
+
return value.toString();
|
|
72
|
+
}
|
|
73
|
+
format(message) {
|
|
74
|
+
if (typeof this.logFormat === 'function')
|
|
75
|
+
return this.logFormat(message);
|
|
76
|
+
return this.segments.map(x => x.type == 'parameter' ? this.formatParameter(x.value, message[x.value]) : x.value).join('');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.LogFormatter = LogFormatter;
|
|
80
|
+
class ConsoleLogger {
|
|
81
|
+
constructor(format) {
|
|
82
|
+
this.format = format;
|
|
83
|
+
this.formatter = new LogFormatter(format);
|
|
84
|
+
}
|
|
85
|
+
log(message) {
|
|
86
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
let messageText = message.message;
|
|
88
|
+
if (message.type === 'inspect') {
|
|
89
|
+
// On the web, take advantage of the interactive
|
|
90
|
+
// inspection facilities
|
|
91
|
+
if (typeof document !== 'undefined') {
|
|
92
|
+
console.dir(message.subject);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
// Inspect
|
|
96
|
+
messageText = (0, inspect_1.inspect)(message.subject, {
|
|
97
|
+
stylize: inspect_1.stylizeWithConsoleColors
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
let finalMessage = Object.assign({}, message, { message: messageText });
|
|
101
|
+
let finalMessageStr = this.formatter.format(finalMessage);
|
|
102
|
+
console.log(finalMessageStr);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.ConsoleLogger = ConsoleLogger;
|
|
107
|
+
class FileLogger {
|
|
108
|
+
constructor(format, filename) {
|
|
109
|
+
this.format = format;
|
|
110
|
+
this.filename = filename;
|
|
111
|
+
this.formatter = new LogFormatter(format);
|
|
112
|
+
}
|
|
113
|
+
get ready() {
|
|
114
|
+
return this._fdReady;
|
|
115
|
+
}
|
|
116
|
+
open() {
|
|
117
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
118
|
+
if (this._fdReady)
|
|
119
|
+
return yield this._fdReady;
|
|
120
|
+
return yield (this._fdReady = new Promise((res, rej) => {
|
|
121
|
+
fs.open(this.filename, 'a', (err, fd) => {
|
|
122
|
+
if (err)
|
|
123
|
+
rej(err);
|
|
124
|
+
else
|
|
125
|
+
res(fd);
|
|
126
|
+
});
|
|
127
|
+
}));
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
write(str) {
|
|
131
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
let fd = yield this.open();
|
|
133
|
+
yield new Promise((res, rej) => {
|
|
134
|
+
fs.write(fd, Buffer.from(str), (err, written, buffer) => {
|
|
135
|
+
if (err) {
|
|
136
|
+
rej(err);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
// sync to flush this to disk right away
|
|
140
|
+
fs.fdatasync(fd, (err) => err ? rej(err) : res());
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
log(message) {
|
|
146
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
147
|
+
let formattedMessage = this.formatter.format(message);
|
|
148
|
+
yield this.write(`${formattedMessage}\n`);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
exports.FileLogger = FileLogger;
|
|
153
|
+
let ZonedLogger = ZonedLogger_1 = class ZonedLogger {
|
|
154
|
+
constructor(optionsRef, app, sourceLabel) {
|
|
155
|
+
this.optionsRef = optionsRef;
|
|
156
|
+
this.app = app;
|
|
157
|
+
this._sourceLabel = undefined;
|
|
158
|
+
this._sourceLabel = sourceLabel;
|
|
159
|
+
}
|
|
160
|
+
clone() {
|
|
161
|
+
let logger = new ZonedLogger_1(this.optionsRef, this.app, this._sourceLabel);
|
|
162
|
+
Object.keys(this).filter(x => typeof this[x] !== 'function').forEach(key => logger[key] = this[key]);
|
|
163
|
+
return logger;
|
|
164
|
+
}
|
|
165
|
+
get sourceLabel() {
|
|
166
|
+
return this._sourceLabel;
|
|
167
|
+
}
|
|
168
|
+
static get current() {
|
|
169
|
+
var _a, _b, _c, _d;
|
|
170
|
+
return (_d = (_a = Zone.current.get(Logger.ZONE_LOCAL_NAME)) !== null && _a !== void 0 ? _a : (_c = (_b = runtime_1.ExecutionContext.current) === null || _b === void 0 ? void 0 : _b.application) === null || _c === void 0 ? void 0 : _c.inject(Logger)) !== null && _d !== void 0 ? _d : new ZonedLogger_1(null, null);
|
|
171
|
+
}
|
|
172
|
+
static log(message, options) { this.current.log(message, options); }
|
|
173
|
+
static info(message, options) { this.current.log(message, options); }
|
|
174
|
+
static fatal(message, options) { this.current.fatal(message, options); }
|
|
175
|
+
static debug(message, options) { this.current.log(message, options); }
|
|
176
|
+
static warning(message, options) { this.current.log(message, options); }
|
|
177
|
+
static error(message, options) { this.current.log(message, options); }
|
|
178
|
+
/**
|
|
179
|
+
* Get the listeners for this logger. Usually this is the global listeners, but if the property
|
|
180
|
+
* was previously assigned it will return those listeners.
|
|
181
|
+
*/
|
|
182
|
+
get listeners() {
|
|
183
|
+
var _a, _b, _c, _d;
|
|
184
|
+
if (this.customListeners !== undefined)
|
|
185
|
+
return this.customListeners;
|
|
186
|
+
if ((_b = (_a = this.optionsRef) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.listeners) {
|
|
187
|
+
return this.optionsRef.options.listeners;
|
|
188
|
+
}
|
|
189
|
+
if ((_d = (_c = this.app) === null || _c === void 0 ? void 0 : _c.options) === null || _d === void 0 ? void 0 : _d.silent)
|
|
190
|
+
return [];
|
|
191
|
+
return exports.DEFAULT_LISTENERS;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Set the log listeners for this specific logger. Set listeners to undefined to use the
|
|
195
|
+
* global listener configuration.
|
|
196
|
+
*/
|
|
197
|
+
set listeners(value) {
|
|
198
|
+
this.customListeners = value;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Run the given function with this logger as the current logger.
|
|
202
|
+
* Calls to Logger.current will yield the logger for this execution
|
|
203
|
+
* context.
|
|
204
|
+
*/
|
|
205
|
+
run(func) {
|
|
206
|
+
return Zone.current.fork({
|
|
207
|
+
name: `LoggerContext`,
|
|
208
|
+
properties: {
|
|
209
|
+
[Logger.ZONE_LOCAL_NAME]: this
|
|
210
|
+
}
|
|
211
|
+
}).run(func);
|
|
212
|
+
}
|
|
213
|
+
createMessage(message) {
|
|
214
|
+
let contextSummary = this.contextLabel || '';
|
|
215
|
+
if (this._sourceLabel)
|
|
216
|
+
contextSummary = `${contextSummary} » ${this._sourceLabel}`;
|
|
217
|
+
return Object.assign({
|
|
218
|
+
type: 'message',
|
|
219
|
+
context: this.context,
|
|
220
|
+
date: new Date(),
|
|
221
|
+
message,
|
|
222
|
+
contextLabel: this.contextLabel,
|
|
223
|
+
sourceLabel: this._sourceLabel,
|
|
224
|
+
contextSummary: contextSummary
|
|
225
|
+
}, message);
|
|
226
|
+
}
|
|
227
|
+
log(message, options) {
|
|
228
|
+
this.emitLog(this.createMessage({
|
|
229
|
+
message,
|
|
230
|
+
severity: (options ? options.severity : undefined) || 'info'
|
|
231
|
+
}));
|
|
232
|
+
}
|
|
233
|
+
info(message, options) {
|
|
234
|
+
this.log(message, Object.assign({}, options, { severity: 'info' }));
|
|
235
|
+
}
|
|
236
|
+
fatal(message, options) {
|
|
237
|
+
this.log(message, Object.assign({}, options, { severity: 'fatal' }));
|
|
238
|
+
}
|
|
239
|
+
debug(message, options) {
|
|
240
|
+
this.log(message, Object.assign({}, options, { severity: 'debug' }));
|
|
241
|
+
}
|
|
242
|
+
warning(message, options) {
|
|
243
|
+
this.log(message, Object.assign({}, options, { severity: 'warning' }));
|
|
244
|
+
}
|
|
245
|
+
error(message, options) {
|
|
246
|
+
this.log(message, Object.assign({}, options, { severity: 'error' }));
|
|
247
|
+
}
|
|
248
|
+
inspect(object, options) {
|
|
249
|
+
this.emitLog(this.createMessage({
|
|
250
|
+
type: 'inspect',
|
|
251
|
+
severity: (options ? options.severity : undefined) || 'info',
|
|
252
|
+
subject: object,
|
|
253
|
+
message: '' + object
|
|
254
|
+
}));
|
|
255
|
+
}
|
|
256
|
+
emitLog(message) {
|
|
257
|
+
this.listeners.forEach(listener => listener.log(message));
|
|
258
|
+
}
|
|
259
|
+
get context() {
|
|
260
|
+
return Zone.current.get('logContext');
|
|
261
|
+
}
|
|
262
|
+
get contextLabel() {
|
|
263
|
+
return Zone.current.get('logContextLabel');
|
|
264
|
+
}
|
|
265
|
+
withContext(context, label, callback) {
|
|
266
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
267
|
+
let zone = Zone.current.fork({
|
|
268
|
+
name: `LogContextZone: ${label}`,
|
|
269
|
+
properties: {
|
|
270
|
+
logContext: context,
|
|
271
|
+
logContextLabel: label
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
return yield zone.run(() => callback());
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
exports.ZonedLogger = ZonedLogger;
|
|
279
|
+
ZonedLogger.ZONE_LOCAL_NAME = '@alterior/logger:Logger.current';
|
|
280
|
+
exports.ZonedLogger = ZonedLogger = ZonedLogger_1 = tslib_1.__decorate([
|
|
281
|
+
tslib_1.__param(0, (0, di_1.Optional)()),
|
|
282
|
+
tslib_1.__metadata("design:paramtypes", [LoggingOptionsRef,
|
|
283
|
+
runtime_1.Application, String])
|
|
284
|
+
], ZonedLogger);
|
|
285
|
+
let Logger = Logger_1 = class Logger extends ZonedLogger {
|
|
286
|
+
constructor(optionsRef, app) {
|
|
287
|
+
super(optionsRef, app);
|
|
288
|
+
}
|
|
289
|
+
clone() {
|
|
290
|
+
let logger = new Logger_1(this.optionsRef);
|
|
291
|
+
Object.keys(this).filter(x => typeof this[x] !== 'function').forEach(key => logger[key] = this[key]);
|
|
292
|
+
return logger;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Create a new logger based on this one with the given listeners. You can also assign the `listeners` array
|
|
296
|
+
* on the instance directly after using clone() or withSource().
|
|
297
|
+
**/
|
|
298
|
+
withListeners(listeners) {
|
|
299
|
+
let logger = this.clone();
|
|
300
|
+
logger.listeners = listeners;
|
|
301
|
+
return logger;
|
|
302
|
+
}
|
|
303
|
+
withSource(sourceLabel) {
|
|
304
|
+
let logger = this.clone();
|
|
305
|
+
if (typeof sourceLabel === 'string') {
|
|
306
|
+
logger._sourceLabel = sourceLabel;
|
|
307
|
+
}
|
|
308
|
+
else if (typeof sourceLabel === 'object') {
|
|
309
|
+
logger._sourceLabel = sourceLabel.constructor.name;
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
logger._sourceLabel = `${sourceLabel}`;
|
|
313
|
+
}
|
|
314
|
+
return logger;
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
exports.Logger = Logger;
|
|
318
|
+
exports.Logger = Logger = Logger_1 = tslib_1.__decorate([
|
|
319
|
+
(0, di_1.Injectable)(),
|
|
320
|
+
tslib_1.__param(0, (0, di_1.Optional)()),
|
|
321
|
+
tslib_1.__metadata("design:paramtypes", [LoggingOptionsRef,
|
|
322
|
+
runtime_1.Application])
|
|
323
|
+
], Logger);
|
|
324
|
+
const DEFAULT_FORMAT = (event) => {
|
|
325
|
+
if (event.contextSummary)
|
|
326
|
+
return `${event.date.toISOString()} [${event.contextSummary}] ${event.severity}: ${event.message}`;
|
|
327
|
+
else
|
|
328
|
+
return `${event.date.toISOString()} ${event.severity}: ${event.message}`;
|
|
329
|
+
};
|
|
330
|
+
exports.DEFAULT_FORMAT = DEFAULT_FORMAT;
|
|
331
|
+
exports.DEFAULT_LISTENERS = [new ConsoleLogger(exports.DEFAULT_FORMAT)];
|
|
332
332
|
//# sourceMappingURL=logger.js.map
|