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
/
Console.ts
261 lines (224 loc) · 7.18 KB
/
Console.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
/*
* 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.
*
*/
/**
* Generic logging-use class for writing info to the console if we have log4js
* failures or are unable to initialize otherwise.
*
* This class supports most of the methods / accessors log4js uses.
*/
import { IConsole } from "./doc/IConsole";
import { TextUtils } from "../../utilities";
import * as moment from "moment";
import { format, isNullOrUndefined } from "util";
import { ImperativeError } from "../../error";
export class Console implements IConsole {
public static readonly LEVELS = ["trace", "debug", "info", "warn", "error", "fatal", "off"];
public static readonly LEVEL_DEFAULT = "debug";
public static getConsole(category: string) {
return new Console();
}
public static isValidLevel(level: string) {
return Console.LEVELS.indexOf(level) < 0 ? false : true;
}
public static validateLevel(level: string) {
if (!Console.isValidLevel(level)) {
throw new ImperativeError(
{
msg: "Invalid level specified",
}
);
}
}
private mPrefix: boolean;
private mColor: boolean;
private mIsOn: boolean;
constructor(private mLevel = Console.LEVEL_DEFAULT) {
this.mPrefix = true;
this.mColor = true;
this.mLevel = mLevel.toLocaleLowerCase();
this.mIsOn = true;
Console.validateLevel(this.mLevel);
}
public addContext(key: string, value: any) {
// do nothing
}
public removeContext(key: string) {
// do nothing
}
public clearContext() {
// do nothing
}
public isLevelEnabled() {
return false;
}
public isTraceEnabled() {
return Console.LEVELS.indexOf("trace") >= Console.LEVELS.indexOf(this.level) ? true : false;
}
public isDebugEnabled() {
return Console.LEVELS.indexOf("debug") >= Console.LEVELS.indexOf(this.level) ? true : false;
}
public isInfoEnabled() {
return Console.LEVELS.indexOf("info") >= Console.LEVELS.indexOf(this.level) ? true : false;
}
public isWarnEnabled() {
return Console.LEVELS.indexOf("warn") >= Console.LEVELS.indexOf(this.level) ? true : false;
}
public isErrorEnabled() {
return Console.LEVELS.indexOf("error") >= Console.LEVELS.indexOf(this.level) ? true : false;
}
public isFatalEnabled() {
return Console.LEVELS.indexOf("fatal") >= Console.LEVELS.indexOf(this.level) ? true : false;
}
public isFormatEnabled() {
return true;
}
public info(message: any, ...args: any[]) {
if (!this.isInfoEnabled()) {
return;
}
let adjustedMessage = message;
if (this.prefix) {
adjustedMessage = this.buildPrefix("INFO") + message;
}
if (this.color) {
adjustedMessage = TextUtils.chalk.grey(adjustedMessage);
}
return this.writeStdout(adjustedMessage, args);
}
public trace(message: any, ...args: any[]) {
if (!this.isTraceEnabled()) {
return;
}
let adjustedMessage = message;
if (this.prefix) {
adjustedMessage = this.buildPrefix("TRACE") + message;
}
if (this.color) {
adjustedMessage = TextUtils.chalk.cyan(adjustedMessage);
}
return this.writeStdout(adjustedMessage, args);
}
public debug(message: any, ...args: any[]) {
if (!this.isDebugEnabled()) {
return;
}
let adjustedMessage = message;
if (this.prefix) {
adjustedMessage = this.buildPrefix("DEBUG") + message;
}
if (this.color) {
adjustedMessage = TextUtils.chalk.blue(adjustedMessage);
}
return this.writeStdout(adjustedMessage, args);
}
public warn(message: any, ...args: any[]) {
if (!this.isWarnEnabled()) {
return;
}
let adjustedMessage = message;
if (this.prefix) {
adjustedMessage = this.buildPrefix("WARN") + message;
}
if (this.color) {
adjustedMessage = TextUtils.chalk.yellow(adjustedMessage);
}
return this.writeStderr(adjustedMessage, args);
}
public error(message: any, ...args: any[]) {
if (!this.isErrorEnabled()) {
return;
}
let adjustedMessage = message;
if (this.prefix) {
adjustedMessage = this.buildPrefix("ERROR") + message;
}
if (this.color) {
adjustedMessage = TextUtils.chalk.red(adjustedMessage);
}
return this.writeStderr(adjustedMessage, args);
}
public fatal(message: any, ...args: any[]) {
if (!this.isFatalEnabled()) {
return;
}
let adjustedMessage = message;
if (this.prefix) {
adjustedMessage = this.buildPrefix("FATAL") + message;
}
if (this.color) {
adjustedMessage = TextUtils.chalk.magenta(adjustedMessage);
}
return this.writeStderr(adjustedMessage, args);
}
private writeStderr(message: string, ...args: any[]) {
const data = this.format(message, args);
if (this.on) {
process.stderr.write(this.format(message, args));
}
return data;
}
private writeStdout(message: string, ...args: any[]) {
const data = this.format(message, args);
if (this.on) {
process.stdout.write(data);
}
return data;
}
private format(data: string, ...args: any[]) {
let formatted = data;
// TODO(Kelosky): this is not ideal, but works for simple cases of
// .debug(%s, "sub string").
if (this.isFormatEnabled() && !isNullOrUndefined(args) && args.length > 0) {
let defined = false;
args.forEach((arg) => {
arg.forEach((ntry: string[]) => {
if (ntry.length > 0) {
defined = true;
}
});
});
// if every argument is undefined, dont format it
if (defined) {
formatted = format(data, args);
}
}
return formatted + "\n";
}
private buildPrefix(type: string) {
return "[" + moment().format("YYYY/MM/DD HH:MM:SS") + "]" + " " + "[" + type + "]" + " ";
}
set level(level: string) {
level = level.toLowerCase();
Console.validateLevel(level);
this.mLevel = level;
}
get level() {
return this.mLevel;
}
set prefix(isEnabled: boolean) {
this.mPrefix = isEnabled;
}
get prefix(): boolean {
return this.mPrefix;
}
set color(isEnabled: boolean) {
this.mColor = isEnabled;
}
get color(): boolean {
return this.mColor;
}
set on(isOn: boolean) {
this.mIsOn = isOn;
}
get on() {
return this.mIsOn;
}
}