| 31 | export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'fatal'; |
| 32 | |
| 33 | export class Logger extends Observable<LogEntry> implements LoggerApi { |
| 34 | protected readonly _subject: Subject<LogEntry> = new Subject<LogEntry>(); |
| 35 | protected _metadata: LoggerMetadata; |
| 36 | |
| 37 | private _obs: Observable<LogEntry> = EMPTY; |
| 38 | private _subscription: Subscription | null = null; |
| 39 | |
| 40 | protected get _observable() { |
| 41 | return this._obs; |
| 42 | } |
| 43 | protected set _observable(v: Observable<LogEntry>) { |
| 44 | if (this._subscription) { |
| 45 | this._subscription.unsubscribe(); |
| 46 | } |
| 47 | this._obs = v; |
| 48 | if (this.parent) { |
| 49 | this._subscription = this.subscribe( |
| 50 | (value: LogEntry) => { |
| 51 | if (this.parent) { |
| 52 | this.parent._subject.next(value); |
| 53 | } |
| 54 | }, |
| 55 | (error: Error) => { |
| 56 | if (this.parent) { |
| 57 | this.parent._subject.error(error); |
| 58 | } |
| 59 | }, |
| 60 | () => { |
| 61 | if (this._subscription) { |
| 62 | this._subscription.unsubscribe(); |
| 63 | } |
| 64 | this._subscription = null; |
| 65 | }, |
| 66 | ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | constructor( |
| 71 | public readonly name: string, |
| 72 | public readonly parent: Logger | null = null, |
| 73 | ) { |
| 74 | super(); |
| 75 | |
| 76 | const path: string[] = []; |
| 77 | let p = parent; |
| 78 | while (p) { |
| 79 | path.push(p.name); |
| 80 | p = p.parent; |
| 81 | } |
| 82 | this._metadata = { name, path }; |
| 83 | this._observable = this._subject.asObservable(); |
| 84 | if (this.parent && this.parent._subject) { |
| 85 | // When the parent completes, complete us as well. |
| 86 | this.parent._subject.subscribe(undefined, undefined, () => this.complete()); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | asApi(): LoggerApi { |
nothing calls this directly
no outgoing calls
no test coverage detected