-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathlogger.service.ts
158 lines (134 loc) · 6.18 KB
/
logger.service.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
import { Inject, Injectable } from '@angular/core';
import { HttpHeaders, HttpParams } from '@angular/common/http';
import { NgxLoggerLevel } from './types/logger-level.enum';
import { INGXLoggerConfigEngine } from './config/iconfig-engine';
import { INGXLoggerConfig, TOKEN_LOGGER_CONFIG } from './config/iconfig';
import { INGXLoggerMetadataService, TOKEN_LOGGER_METADATA_SERVICE } from './metadata/imetadata.service';
import { INGXLoggerRulesService, TOKEN_LOGGER_RULES_SERVICE } from './rules/irules.service';
import { INGXLoggerMapperService, TOKEN_LOGGER_MAPPER_SERVICE } from './mapper/imapper.service';
import { INGXLoggerMonitor } from './monitor/ilogger-monitor';
import { INGXLoggerWriterService, TOKEN_LOGGER_WRITER_SERVICE } from './writer/iwriter.service';
import { INGXLoggerServerService, TOKEN_LOGGER_SERVER_SERVICE } from './server/iserver.service';
import { take } from 'rxjs/operators';
import { INGXLoggerConfigEngineFactory, TOKEN_LOGGER_CONFIG_ENGINE_FACTORY } from './config/iconfig-engine-factory';
@Injectable({
providedIn: 'root'
})
export class NGXLogger {
private _loggerMonitor: INGXLoggerMonitor;
private configEngine: INGXLoggerConfigEngine;
constructor(
@Inject(TOKEN_LOGGER_CONFIG) config: INGXLoggerConfig,
@Inject(TOKEN_LOGGER_CONFIG_ENGINE_FACTORY) configEngineFactory: INGXLoggerConfigEngineFactory,
@Inject(TOKEN_LOGGER_METADATA_SERVICE) private metadataService: INGXLoggerMetadataService,
@Inject(TOKEN_LOGGER_RULES_SERVICE) private ruleService: INGXLoggerRulesService,
@Inject(TOKEN_LOGGER_MAPPER_SERVICE) private mapperService: INGXLoggerMapperService,
@Inject(TOKEN_LOGGER_WRITER_SERVICE) private writerService: INGXLoggerWriterService,
@Inject(TOKEN_LOGGER_SERVER_SERVICE) private serverService: INGXLoggerServerService,
) {
this.configEngine = configEngineFactory.provideConfigEngine(config);
}
/** Get a readonly access to the level configured for the NGXLogger */
get level(): NgxLoggerLevel {
return this.configEngine.level;
}
/** Get a readonly access to the serverLogLevel configured for the NGXLogger */
get serverLogLevel(): NgxLoggerLevel {
return this.configEngine.serverLogLevel;
}
public trace(message?: any | (() => any), ...additional: any[]): void {
this._log(NgxLoggerLevel.TRACE, message, additional);
}
public debug(message?: any | (() => any), ...additional: any[]): void {
this._log(NgxLoggerLevel.DEBUG, message, additional);
}
public info(message?: any | (() => any), ...additional: any[]): void {
this._log(NgxLoggerLevel.INFO, message, additional);
}
public log(message?: any | (() => any), ...additional: any[]): void {
this._log(NgxLoggerLevel.LOG, message, additional);
}
public warn(message?: any | (() => any), ...additional: any[]): void {
this._log(NgxLoggerLevel.WARN, message, additional);
}
public error(message?: any | (() => any), ...additional: any[]): void {
this._log(NgxLoggerLevel.ERROR, message, additional);
}
public fatal(message?: any | (() => any), ...additional: any[]): void {
this._log(NgxLoggerLevel.FATAL, message, additional);
}
/** @deprecated customHttpHeaders is now part of the config, this should be updated via @see updateConfig */
public setCustomHttpHeaders(headers: HttpHeaders) {
const config = this.getConfigSnapshot();
config.customHttpHeaders = headers;
this.updateConfig(config);
}
/** @deprecated customHttpParams is now part of the config, this should be updated via @see updateConfig */
public setCustomParams(params: HttpParams) {
const config = this.getConfigSnapshot();
config.customHttpParams = params;
this.updateConfig(config);
}
/** @deprecated withCredentials is now part of the config, this should be updated via @see updateConfig */
public setWithCredentialsOptionValue(withCredentials: boolean) {
const config = this.getConfigSnapshot();
config.withCredentials = withCredentials;
this.updateConfig(config);
}
/**
* Register a INGXLoggerMonitor that will be trigger when a log is either written or sent to server
*
* There is only one monitor, registering one will overwrite the last one if there was one
* @param monitor
*/
public registerMonitor(monitor: INGXLoggerMonitor) {
this._loggerMonitor = monitor;
}
/** Set config of logger
*
* Warning : This overwrites all the config, if you want to update only one property, you should use @see getConfigSnapshot before
*/
public updateConfig(config: INGXLoggerConfig) {
this.configEngine.updateConfig(config);
}
public partialUpdateConfig(partialConfig: Partial<INGXLoggerConfig>): void {
this.configEngine.partialUpdateConfig(partialConfig);
}
/** Get config of logger */
public getConfigSnapshot(): INGXLoggerConfig {
return this.configEngine.getConfig();
}
/**
* Flush the serveur queue
*/
public flushServerQueue(): void {
this.serverService.flushQueue(this.getConfigSnapshot());
}
private _log(level: NgxLoggerLevel, message?: any | (() => any), additional: any[] = []): void {
const config = this.configEngine.getConfig();
const shouldCallWriter = this.ruleService.shouldCallWriter(level, config, message, additional);
const shouldCallServer = this.ruleService.shouldCallServer(level, config, message, additional);
const shouldCallMonitor = this.ruleService.shouldCallMonitor(level, config, message, additional);
if (!shouldCallWriter && !shouldCallServer && !shouldCallMonitor) {
// If nothing is to be called we return
return;
}
const metadata = this.metadataService.getMetadata(level, config, message, additional);
this.mapperService.getLogPosition(config, metadata).pipe(take(1)).subscribe(logPosition => {
if (logPosition) {
metadata.fileName = logPosition.fileName;
metadata.lineNumber = logPosition.lineNumber;
metadata.columnNumber = logPosition.columnNumber;
}
if (shouldCallMonitor && this._loggerMonitor) {
this._loggerMonitor.onLog(metadata, config);
}
if (shouldCallWriter) {
this.writerService.writeMessage(metadata, config);
}
if (shouldCallServer) {
this.serverService.sendToServer(metadata, config);
}
});
}
}