forked from jupyterlab/jupyterlab
/
logger.ts
438 lines (390 loc) · 9.45 KB
/
logger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { nbformat } from '@jupyterlab/coreutils';
import { IOutputAreaModel, OutputAreaModel } from '@jupyterlab/outputarea';
import {
IRenderMimeRegistry,
IOutputModel,
OutputModel
} from '@jupyterlab/rendermime';
import { ISignal, Signal } from '@phosphor/signaling';
import {
ILogger,
IContentChange,
IStateChange,
ILoggerOutputAreaModel,
ILogPayload,
LogLevel
} from './tokens';
/**
* All severity levels, including an internal one for metadata.
*/
type FullLogLevel = LogLevel | 'metadata';
/**
* Custom Notebook Output with log info.
*/
type ILogOutput = nbformat.IOutput & {
/**
* Date & time when output is logged in integer representation.
*/
timestamp: number;
/**
* Log level
*/
level: FullLogLevel;
};
export interface ILogOutputModel extends IOutputModel {
/**
* Date & time when output is logged.
*/
readonly timestamp: Date;
/**
* Log level
*/
readonly level: FullLogLevel;
}
/**
* Log Output Model with timestamp which provides
* item information for Output Area Model.
*/
export class LogOutputModel extends OutputModel implements ILogOutputModel {
/**
* Construct a LogOutputModel.
*
* @param options - The model initialization options.
*/
constructor(options: LogOutputModel.IOptions) {
super(options);
this.timestamp = new Date(options.value.timestamp);
this.level = options.value.level;
}
/**
* Date & time when output is logged.
*/
readonly timestamp: Date = null;
/**
* Log level
*/
readonly level: FullLogLevel;
}
/**
* Log Output Model namespace that defines initialization options.
*/
namespace LogOutputModel {
export interface IOptions extends IOutputModel.IOptions {
value: ILogOutput;
}
}
/**
* Implementation of `IContentFactory` for Output Area Model
* which creates LogOutputModel instances.
*/
class LogConsoleModelContentFactory extends OutputAreaModel.ContentFactory {
/**
* Create a rendermime output model from notebook output.
*/
createOutputModel(options: LogOutputModel.IOptions): LogOutputModel {
return new LogOutputModel(options);
}
}
/**
* Output Area Model implementation which is able to
* limit number of outputs stored.
*/
export class LoggerOutputAreaModel extends OutputAreaModel
implements ILoggerOutputAreaModel {
constructor({ maxLength, ...options }: LoggerOutputAreaModel.IOptions) {
super(options);
this.maxLength = maxLength;
}
/**
* Add an output, which may be combined with previous output.
*
* @returns The total number of outputs.
*
* #### Notes
* The output bundle is copied. Contiguous stream outputs of the same `name`
* are combined. The oldest outputs are possibly removed to ensure the total
* number of outputs is at most `.maxLength`.
*/
add(output: ILogOutput): number {
super.add(output);
this._applyMaxLength();
return this.length;
}
/**
* Whether an output should combine with the previous output.
*
* We combine if the two outputs are in the same second, which is the
* resolution for our time display.
*/
protected shouldCombine(options: {
value: ILogOutput;
lastModel: ILogOutputModel;
}): boolean {
const { value, lastModel } = options;
let oldSeconds = Math.trunc(lastModel.timestamp.getTime() / 1000);
let newSeconds = Math.trunc(value.timestamp / 1000);
return oldSeconds === newSeconds;
}
/**
* Get an item at the specified index.
*/
get(index: number): ILogOutputModel {
return super.get(index) as ILogOutputModel;
}
/**
* Maximum number of outputs to store in the model.
*/
get maxLength(): number {
return this._maxLength;
}
set maxLength(value: number) {
this._maxLength = value;
this._applyMaxLength();
}
/**
* Manually apply length limit.
*/
private _applyMaxLength() {
if (this.list.length > this._maxLength) {
this.list.removeRange(0, this.list.length - this._maxLength);
}
}
private _maxLength: number;
}
export namespace LoggerOutputAreaModel {
export interface IOptions extends IOutputAreaModel.IOptions {
/**
* The maximum number of messages stored.
*/
maxLength: number;
}
}
/**
* A concrete implementation of ILogger.
*/
export class Logger implements ILogger {
/**
* Construct a Logger.
*
* @param source - The name of the log source.
*/
constructor(options: Logger.IOptions) {
this.source = options.source;
this.outputAreaModel = new LoggerOutputAreaModel({
contentFactory: new LogConsoleModelContentFactory(),
maxLength: options.maxLength
});
}
/**
* The maximum number of outputs stored.
*
* #### Notes
* Oldest entries will be trimmed to ensure the length is at most
* `.maxLength`.
*/
get maxLength() {
return this.outputAreaModel.maxLength;
}
set maxLength(value: number) {
this.outputAreaModel.maxLength = value;
}
/**
* The level of outputs logged
*/
get level(): LogLevel {
return this._level;
}
set level(newValue: LogLevel) {
let oldValue = this._level;
if (oldValue === newValue) {
return;
}
this._level = newValue;
this._log({
output: {
output_type: 'display_data',
data: {
'text/plain': `Log level set to ${newValue}`
}
},
level: 'metadata'
});
this._stateChanged.emit({ name: 'level', oldValue, newValue });
}
/**
* Number of outputs logged.
*/
get length(): number {
return this.outputAreaModel.length;
}
/**
* A signal emitted when the list of log messages changes.
*/
get contentChanged(): ISignal<this, IContentChange> {
return this._contentChanged;
}
/**
* A signal emitted when the log state changes.
*/
get stateChanged(): ISignal<this, IStateChange> {
return this._stateChanged;
}
/**
* Rendermime to use when rendering outputs logged.
*/
get rendermime(): IRenderMimeRegistry | null {
return this._rendermime;
}
set rendermime(value: IRenderMimeRegistry | null) {
if (value !== this._rendermime) {
let oldValue = this._rendermime;
let newValue = (this._rendermime = value);
this._stateChanged.emit({ name: 'rendermime', oldValue, newValue });
}
}
/**
* The number of messages that have ever been stored.
*/
get version(): number {
return this._version;
}
/**
* The source for the logger.
*/
readonly source: string;
/**
* The output area model used for the logger.
*
* #### Notes
* This will usually not be accessed directly. It is a public attribute so
* that the renderer can access it.
*/
readonly outputAreaModel: LoggerOutputAreaModel;
/**
* Log an output to logger.
*
* @param log - The output to be logged.
*/
log(log: ILogPayload) {
// Filter by our current log level
if (
Private.LogLevel[log.level as keyof typeof Private.LogLevel] <
Private.LogLevel[this._level as keyof typeof Private.LogLevel]
) {
return;
}
let output: nbformat.IOutput = null;
switch (log.type) {
case 'text':
output = {
output_type: 'display_data',
data: {
'text/plain': log.data
}
};
break;
case 'html':
output = {
output_type: 'display_data',
data: {
'text/html': log.data
}
};
break;
case 'output':
output = log.data;
break;
default:
break;
}
if (output) {
this._log({
output,
level: log.level
});
}
}
/**
* Clear all outputs logged.
*/
clear() {
this.outputAreaModel.clear(false);
this._contentChanged.emit('clear');
}
/**
* Add a checkpoint to the log.
*/
checkpoint() {
this._log({
output: {
output_type: 'display_data',
data: {
'text/html': '<hr/>'
}
},
level: 'metadata'
});
}
/**
* Whether the logger is disposed.
*/
get isDisposed() {
return this._isDisposed;
}
/**
* Dispose the logger.
*/
dispose() {
if (this.isDisposed) {
return;
}
this._isDisposed = true;
this.clear();
this._rendermime = null;
Signal.clearData(this);
}
private _log(options: { output: nbformat.IOutput; level: FullLogLevel }) {
// First, make sure our version reflects the new message so things
// triggering from the signals below have the correct version.
this._version++;
// Next, trigger any displays of the message
this.outputAreaModel.add({
...options.output,
timestamp: Date.now(),
level: options.level
});
// Finally, tell people that the message was appended (and possibly
// already displayed).
this._contentChanged.emit('append');
}
private _isDisposed = false;
private _contentChanged = new Signal<this, IContentChange>(this);
private _stateChanged = new Signal<this, IStateChange>(this);
private _rendermime: IRenderMimeRegistry | null = null;
private _version = 0;
private _level: LogLevel = 'warning';
}
export namespace Logger {
export interface IOptions {
/**
* The log source identifier.
*/
source: string;
/**
* The maximum number of messages to store.
*/
maxLength: number;
}
}
namespace Private {
export enum LogLevel {
debug,
info,
warning,
error,
critical,
metadata
}
}