-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.ts
More file actions
executable file
·183 lines (158 loc) · 5.7 KB
/
log.ts
File metadata and controls
executable file
·183 lines (158 loc) · 5.7 KB
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
import map from 'lodash/map';
import winston from 'winston';
import { getChalk, ChalkInstance } from '@contentstack/cli-utilities';
import replace from 'lodash/replace';
import isObject from 'lodash/isObject';
import { normalize, resolve } from 'path';
import { PrintOptions, cliux as ux } from '@contentstack/cli-utilities';
import { LoggerType, PrintType } from '../types';
const ansiRegexPattern = [
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))',
].join('|');
const customLevels = {
levels: {
warn: 1,
info: 2,
debug: 3,
},
};
/* The Logger class is a TypeScript class that provides logging functionality with different log levels
and options. */
export default class Logger {
private infoLogger!: winston.Logger;
private errorLogger!: winston.Logger;
private config!: Record<string, any>;
private hiddenInfoLogger!: winston.Logger;
get loggerOptions(): winston.transports.FileTransportOptions {
return {
filename: '',
maxFiles: 20,
tailable: true,
maxsize: 1000000,
};
}
constructor(config: Record<string, any>) {
this.config = config;
this.infoLogger = this.getLoggerInstance('info');
this.errorLogger = this.getLoggerInstance('error');
this.hiddenInfoLogger = this.getLoggerInstance('hidden');
}
/**
* The function getLoggerInstance creates and returns a winston logger instance based on the provided
* log type.
* @param {LoggerType} logType - The `logType` parameter is a string that represents the type of log.
* It can have one of the following values: "error", "info", "debug", "warn", or any other custom log
* type.
* @returns an instance of the winston.Logger class.
*/
getLoggerInstance(logType: LoggerType): winston.Logger {
const consoleOptions: winston.transports.ConsoleTransportOptions = {
format: winston.format.combine(winston.format.simple(), winston.format.colorize({ all: true })),
};
const isHidden = logType === 'hidden';
logType = logType === 'hidden' ? 'info' : logType;
if (logType === 'error') {
consoleOptions.level = logType;
}
const filename = normalize(
resolve(this.config.basePath, 'Audit logs', `${logType}.log`),
).replace(/^(\.\.(\/|\\|$))+/, '');
const transports: winston.transport[] = [
new winston.transports.File({
...this.loggerOptions,
level: logType,
filename,
}),
];
if (!isHidden) {
transports.push(new winston.transports.Console(consoleOptions));
}
const loggerOptions: winston.LoggerOptions = {
transports,
levels: customLevels.levels,
};
if (logType === 'error') {
loggerOptions.levels = { error: 0 };
}
return winston.createLogger(loggerOptions);
}
/**
* The function `log` takes a message and an optional log type, and logs the message using different
* loggers based on the log type.
* @param {string | any} message - The `message` parameter is a string or any type of value that you
* want to log. It represents the content of the log message that you want to display.
* @param {LoggerType | PrintOptions | undefined} [logType] - The `logType` parameter is an optional
* parameter that specifies the type of log. It can be one of the following values:
*/
log(
message: string | any,
logType?: LoggerType | PrintOptions | undefined,
skipCredentialCheck: boolean = false,
): void {
const logString = skipCredentialCheck ? message : this.returnString(message);
switch (logType) {
case 'info':
case 'debug':
case 'warn':
this.infoLogger.log(logType, logString);
break;
case 'error':
this.errorLogger.error(logString);
break;
case 'hidden':
this.hiddenInfoLogger.log('info', logString);
break;
default:
ux.print(logString, logType || {});
break;
}
}
/**
* The function `returnString` takes a message as input and returns a modified version of the message
* with sensitive credentials replaced and any ANSI escape codes removed.
* @param {any} message - The `message` parameter can be of any type. It can be a string, an object, or
* an array.
* @returns a string.
*/
returnString(message: any): string {
let returnStr = '';
const replaceCredentials = (item: any) => {
try {
return JSON.stringify(item).replace(/"authtoken":\s*".*?"/, '"authtoken": "..."');
} catch (error) {}
return item;
};
if (Array.isArray(message) && message.length) {
returnStr = map(message, (item: any) => {
if (item && typeof item === 'object') {
return replaceCredentials(item);
}
return item;
})
.join(' ')
.trim();
} else if (isObject(message)) {
return replaceCredentials(message);
} else {
returnStr = message;
}
returnStr = replace(returnStr, new RegExp(ansiRegexPattern, 'g'), '').trim();
return returnStr;
}
}
/**
* The `print` function takes an array of `PrintType` objects, formats the messages using the `chalk`
* library for styling, and prints the formatted messages using the `ux.print` function.
* @param printInput - An array of objects with the following properties:
*/
export function print(printInput: Array<PrintType>): void {
const chalk = getChalk();
const str = map(printInput, ({ message, bold, color }: PrintType) => {
let chalkFn: ChalkInstance = chalk;
if (color) chalkFn = chalkFn[color] as ChalkInstance;
if (bold) chalkFn = chalkFn.bold as ChalkInstance;
return chalkFn(message);
}).join(' ');
ux.print(str);
}