-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlogger.ts
More file actions
168 lines (154 loc) · 4.34 KB
/
logger.ts
File metadata and controls
168 lines (154 loc) · 4.34 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
/*!
* Contentstack Export
* Copyright (c) 2024 Contentstack LLC
* MIT Licensed
*/
import * as winston from 'winston';
import * as path from 'path';
import mkdirp from 'mkdirp';
import { ExportConfig } from '../types';
import { sanitizePath, redactObject } from '@contentstack/cli-utilities';
const slice = Array.prototype.slice;
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('|');
function returnString(args: unknown[]) {
let returnStr = '';
if (args && args.length) {
returnStr = args
.map(function (item) {
if (item && typeof item === 'object') {
try {
const redactedObject = redactObject(item);
if(redactedObject && typeof redactedObject === 'object') {
return JSON.stringify(redactedObject);
}
} catch (error) {}
return item;
}
return item;
})
.join(' ')
.trim();
}
returnStr = returnStr.replace(new RegExp(ansiRegexPattern, 'g'), '').trim();
return returnStr;
}
const myCustomLevels = {
levels: {
warn: 1,
info: 2,
debug: 3,
},
colors: {
//colors aren't being used anywhere as of now, we're using chalk to add colors while logging
info: 'blue',
debug: 'green',
warn: 'yellow',
error: 'red',
},
};
let logger: winston.Logger;
let errorLogger: winston.Logger;
let successTransport;
let errorTransport;
function init(_logPath: string) {
if (!logger || !errorLogger) {
const logsDir = path.resolve(sanitizePath(_logPath), 'logs', 'export');
// Create dir if doesn't already exist
mkdirp.sync(logsDir);
successTransport = {
filename: path.join(sanitizePath(logsDir), 'success.log'),
maxFiles: 20,
maxsize: 1000000,
tailable: true,
level: 'info',
};
errorTransport = {
filename: path.join(sanitizePath(logsDir), 'error.log'),
maxFiles: 20,
maxsize: 1000000,
tailable: true,
level: 'error',
};
logger = winston.createLogger({
transports: [
new winston.transports.File(successTransport),
new winston.transports.Console({ format: winston.format.simple() }),
],
levels: myCustomLevels.levels,
});
errorLogger = winston.createLogger({
transports: [
new winston.transports.File(errorTransport),
new winston.transports.Console({
level: 'error',
format: winston.format.combine(
winston.format.colorize({ all: true, colors: { error: 'red' } }),
winston.format.simple(),
),
}),
],
levels: { error: 0 },
});
}
return {
log: function (message: any) {
const args = slice.call(arguments);
const logString = returnString(args);
if (logString) {
logger.log('info', logString);
}
},
warn: function () {
const args = slice.call(arguments);
const logString = returnString(args);
if (logString) {
logger.log('warn', logString);
}
},
error: function (message: any) {
const args = slice.call(arguments);
const logString = returnString(args);
if (logString) {
errorLogger.log('error', logString);
}
},
debug: function () {
const args = slice.call(arguments);
const logString = returnString(args);
if (logString) {
logger.log('debug', logString);
}
},
};
}
export const log = async (config: ExportConfig, message: any, type: string) => {
const logsPath = config.cliLogsPath || config.data;
// ignoring the type argument, as we are not using it to create a logfile anymore
if (type !== 'error') {
// removed type argument from init method
init(logsPath).log(message);
} else {
init(logsPath).error(message);
}
};
export const unlinkFileLogger = () => {
if (logger) {
const transports = logger.transports;
transports.forEach((transport: any) => {
if (transport.name === 'file') {
logger.remove(transport);
}
});
}
if (errorLogger) {
const transports = errorLogger.transports;
transports.forEach((transport: any) => {
if (transport.name === 'file') {
errorLogger.remove(transport);
}
});
}
};