This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
LoggerManager.ts
134 lines (116 loc) · 3.98 KB
/
LoggerManager.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
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import { IQueuedMessage } from "./doc/IQueuedMessage";
import { Console } from "../../console";
import { appendFileSync } from "fs";
/**
* LoggerManager is a singleton class used to contain logger information.
*/
export class LoggerManager {
private static readonly DEFAULT_MAX_QUEUE_SIZE = 10000;
private static mInstance: LoggerManager = null;
public static get instance(): LoggerManager {
if (this.mInstance == null) {
this.mInstance = new LoggerManager();
}
return this.mInstance;
}
private mIsLoggerInit: boolean = false;
private mLogInMemory: boolean = false;
private mMaxQueueSize: number;
private console: Console;
private mQueuedMessages: IQueuedMessage[] = [];
constructor() {
this.console = new Console();
this.mMaxQueueSize = LoggerManager.DEFAULT_MAX_QUEUE_SIZE;
}
/**
* The following flag is used to monitor if the Logger.initLogger function
* have been called to set the configuration of log4js.
*/
public get isLoggerInit(): boolean {
return this.mIsLoggerInit;
}
public set isLoggerInit(status: boolean) {
this.mIsLoggerInit = status;
}
/**
* The following flag is used to control if the log message should be store
* in memory while log4js have yet to be configured.
*/
public get logInMemory(): boolean {
return this.mLogInMemory;
}
public set logInMemory(status: boolean) {
this.mLogInMemory = status;
}
/**
* The following value is used to control the max number of messages allowed
* to be stored in memory at all time.
*/
public get maxQueueSize(): number {
return this.mMaxQueueSize;
}
public set maxQueueSize(size: number){
this.mMaxQueueSize = size;
}
/**
* This function returned an array that contain all of the messages.
*/
public get QueuedMessages(): IQueuedMessage[] {
return this.mQueuedMessages;
}
/**
* This function is responsible for gathering all of the input parameters and
* store them in the message queue array.
*
* New messages are to be stored at the top of the array instead of the bottom.
* This allow easy removing message from array while looping the array.
* @param category - logger category
* @param method - log method
* @param message - log message
*/
public queueMessage(category: string, method: string, message: string){
if (this.logInMemory) {
this.QueuedMessages.unshift({
category,
method,
message
});
if (this.QueuedMessages.length > this.maxQueueSize) {
this.QueuedMessages.pop();
}
} else {
this.console.info(message);
}
}
/**
* Dump all of the log messages in memory to the specified file
* @param file log file
*/
public dumpQueuedMessages(file: string) {
if (this.QueuedMessages.length > 0) {
this.console.debug(`Writing all logged messages in memory to ${file}`);
this.QueuedMessages.slice().reverse().forEach((value) => {
(this.console as any)[value.method](value.message);
try {
appendFileSync(file, `${value.message}\n`);
} catch (error) {
/**
* For whatever reason causing logger to unable to append to the log file,
* log the error to console so user see and take appropriate action.
*/
this.console.info(error);
}
});
}
}
}