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