Bonjour,
J'ai deux services dont l'un est utilisé par l'autre
App.module
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 providers: [ LogPublishersService, LogService, CrudService,
App.component page test
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 setOptionsTest(): void { // Get current logging level let oldLevel = this.logger.level; console.log('old Level: ' + oldLevel), this.logger.level = 4; console.log('Level: ' + this.logger.level); // Attempt some logging this.logger.log("This one should not show up"); this.logger.warn("This one should not show up either"); this.logger.error("This is an ERROR"); this.logger.fatal("This is a FATAL ERROR"); // Reset level back to old value this.logger.level = oldLevel; }
log.service
log-publisher.service
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 @Injectable({ providedIn: 'root' }) export class LogService { // Public Properties public publishers: LogPublisher[]; public level: LogLevel = LogLevel.All; public logWithDate: boolean = true; constructor(private publishersService: LogPublishersService) { // Set publishers this.publishers = this.publishersService.publishers; } // ************************* // Public methods // ************************* debug(msg: string, ...optionalParams: any[]) { console.log('debug: ' + msg); this.writeToLog(msg, LogLevel.Debug, optionalParams); } info(msg: string, ...optionalParams: any[]) { console.log('info: ' + msg); this.writeToLog(msg, LogLevel.Info, optionalParams); } warn(msg: string, ...optionalParams: any[]) { console.log('warn: ' + msg); this.writeToLog(msg, LogLevel.Warn, optionalParams); } error(msg: string, ...optionalParams: any[]) { console.log('error: ' + msg); this.writeToLog(msg, LogLevel.Error, optionalParams); } fatal(msg: string, ...optionalParams: any[]) { console.log('fatal: ' + msg); this.writeToLog(msg, LogLevel.Fatal, optionalParams); } log(msg: string, ...optionalParams: any[]) { console.log('log: ' + msg); this.writeToLog(msg, LogLevel.All, optionalParams); } //log1(msg: any) { // console.log(new Date() + ": " + JSON.stringify(msg)); //} clear(): void { for (let logger of this.publishers) { logger.clear() .subscribe(response => console.log(response)); } } // ************************* // Private methods // ************************* private shouldLog(level: LogLevel): boolean { let ret: boolean = false; //console.log('level: ' + level); //console.log('this.level: ' + this.level); if ((level >= this.level && level !== LogLevel.Off) || this.level === LogLevel.All) { ret = true; console.log('OK'); } return ret; } private writeToLog(msg: string, level: LogLevel, params: any[]) { if (this.shouldLog(level)) { // Declare variables let entry: LogEntry = new LogEntry(); // Build Log Entry entry.message = msg; entry.level = level; entry.extraInfo = params; entry.logWithDate = this.logWithDate; if(!this.publishers.length){ console.log('this publixhers is empty'); }else{ console.log('this publixhers is not empty'); } for (let logger of this.publishers) { console.log(msg); logger.log(entry).subscribe(response => console.log(response)); } } } }
Console
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 @Injectable({ providedIn: 'root' }) export class LogPublishersService { // Public properties publishers: LogPublisher[] = []; constructor(private http: HttpClient) { console.log('LogPublishersService') // Build publishers arrays this.buildPublishers(); } // ************************* // Public methods // ************************* // Build publishers array buildPublishers(): void { console.log('Build publishers arrays') let logPub: LogPublisher; this.getLoggers().subscribe(response => { for (let pub of response.filter(p => p.isActive)) { switch (pub.loggerName.toLowerCase()) { case "console": logPub = new LogConsole(); break; case "localstorage": logPub = new LogLocalStorage(); break; case "webapi": logPub = new LogWebApi(this.http); break; } // Set location of logging logPub.location = pub.loggerLocation; // Add publisher to array this.publishers.push(logPub); console.log('publishers arrays push [ location: ' + logPub.location + ' log :' + logPub.log + ' ]') } }); } }
a l'execution , il commence par initailiser le log-publicher.service mais n'initalise pas la proprieté publishers: LogPublisher[] = []; hors j'ai besoin qu'elle soit initialé avant que le second service s'execute. La non initialisation entraine this publixhers is empty ce qui fait que les messages ne s'affichent pas.
LogPublishersService
:4200/main.js:11763 Build publishers arrays
:4200/main.js:15006 ƒ showTasks() {
return this.http.get(`${this.apiUrl}`);
}
/ ici log.service
:4200/main.js:15058 old Level: 0
:4200/main.js:15060 Level: 4
:4200/main.js:7319 log: This one should not show up
:4200/main.js:7307 warn: This one should not show up either
:4200/main.js:7311 error: This is an ERROR
:4200/main.js:7340 OK
:4200/main.js:7354 this publixhers is empty
:4200/main.js:7315 fatal: This is a FATAL ERROR
:4200/main.js:7340 OK
:4200/main.js:7354 this publixhers is empty
core.js:291
suite log-publisher
publishers arrays push [ location: log :log(entry) {
// Log to console
console.log(entry.buildLogString());
//return Observable.of(true);
return Object(rxjs__WEBPACK_IMPORTED_MODULE_0__["of"])(true);
} ]
:4200/main.js:11782 publishers arrays push [ location: logging log :log(entry) {
let ret = false;
let values;
try {
// Retrieve previous values from local storage
values = JSON.parse(localStorage.getItem(this.location)) || [];
// Add new log entry to array
values.push(entry);
// Store array into local storage
localStorage.setItem(this.location, JSON.stringify(values));
// Set return value
ret = true;
}
catch (ex) {
// Display error in console
console.log(ex);
}
//return Observable.of(ret);
return Object(rxjs__WEBPACK_IMPORTED_MODULE_0__["of"])(ret);
} ]
:4200/main.js:11782 publishers arrays push [ location: /api/log log :log(entry) {
/*let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(this.location, entry, options)
.map(response => response.json())
.catch(this.handleErrors);
*/
const headers = new _angular_common_http__WEBPACK_IMPORTED_MODULE_0__["HttpHeaders"]();
headers.set('Content-Type', 'application/json');
//headers.set('Content-Type', 'application/json; charset=utf-8');
const options = ({
headers: headers
});
return this.http.post(this.location, entry, options).pipe(Object(rxjs_operators__WEBPACK_IMPORTED_MODULE_2__["catchError"])(e => this.handleErrors(e)));
} ]
Le souhait est que le système initailse le log-publisher.service (initaise toutes les proprités du service) avant de lancer l'autre service
Partager