@zwa73/utils 1.0.27 → 1.0.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/UtilLogger.js +26 -10
- package/package.json +1 -1
- package/src/UtilLogger.ts +29 -11
- package/test.js +3 -2
package/dist/UtilLogger.js
CHANGED
|
@@ -46,7 +46,17 @@ class SLogger {
|
|
|
46
46
|
static createLogger(name = "default", consoleLevel = "info", outFloder, fileLevel = "info") {
|
|
47
47
|
const transports = [];
|
|
48
48
|
if (outFloder != null) {
|
|
49
|
-
const fileFormat = winston.format.combine(winston.format.timestamp({ format: '
|
|
49
|
+
const fileFormat = winston.format.combine(winston.format.timestamp({ format: 'YY-MM-DD HH:mm:ss' }), winston.format.printf((info) => {
|
|
50
|
+
const level = info.level.toUpperCase();
|
|
51
|
+
const message = info.message;
|
|
52
|
+
//格式化
|
|
53
|
+
let format = `[${info.timestamp}] [${level}]: `;
|
|
54
|
+
let space = "";
|
|
55
|
+
for (let cha of format)
|
|
56
|
+
space += " ";
|
|
57
|
+
let messageList = message.split("\n");
|
|
58
|
+
return `[${info.timestamp}] [${level.toUpperCase()}]: ${messageList.join("\n" + space)}`;
|
|
59
|
+
}));
|
|
50
60
|
transports.push(new DailyRotateFile({
|
|
51
61
|
filename: path.join(outFloder, 'log-%DATE%.txt'),
|
|
52
62
|
datePattern: 'YYYY-MM-DD',
|
|
@@ -54,12 +64,20 @@ class SLogger {
|
|
|
54
64
|
format: fileFormat,
|
|
55
65
|
}));
|
|
56
66
|
}
|
|
57
|
-
const consoleFormat = winston.format.combine(winston.format.timestamp({ format: '
|
|
67
|
+
const consoleFormat = winston.format.combine(winston.format.timestamp({ format: 'YY-MM-DD HH:mm:ss' }), winston.format.printf((info) => {
|
|
58
68
|
const level = info.level.toUpperCase();
|
|
59
|
-
const message = info.message
|
|
69
|
+
const message = info.message;
|
|
60
70
|
const colorizedLevel = colorizer.colorize(info.level, level);
|
|
61
|
-
|
|
62
|
-
|
|
71
|
+
//格式化
|
|
72
|
+
let format = `[${info.timestamp}] [${level}]: `;
|
|
73
|
+
let space = "";
|
|
74
|
+
for (let cha of format)
|
|
75
|
+
space += " ";
|
|
76
|
+
let messageList = message.split("\n");
|
|
77
|
+
messageList[0] = colorizer.colorize(info.level, messageList[0]);
|
|
78
|
+
for (let i = 1; i < messageList.length; i++)
|
|
79
|
+
messageList[i] = colorizer.colorize(info.level, messageList[i]);
|
|
80
|
+
return `[${info.timestamp}] [${colorizedLevel}]: ${messageList.join("\n" + space)}`;
|
|
63
81
|
}));
|
|
64
82
|
transports.push(new winston.transports.Console({
|
|
65
83
|
level: consoleLevel,
|
|
@@ -97,16 +115,14 @@ class SLogger {
|
|
|
97
115
|
for (let message of messages) {
|
|
98
116
|
let out;
|
|
99
117
|
if (message == null)
|
|
100
|
-
out =
|
|
118
|
+
out = `<${typeof message}> ${typeof message}`;
|
|
101
119
|
else if (typeof message !== "string")
|
|
102
|
-
out =
|
|
120
|
+
out = `<${typeof message}> ${message.toString()}`;
|
|
103
121
|
else
|
|
104
122
|
out = message;
|
|
105
123
|
strMessages.push(out);
|
|
106
124
|
}
|
|
107
|
-
|
|
108
|
-
strMessages.unshift("");
|
|
109
|
-
let outMessage = strMessages.join("\n - ");
|
|
125
|
+
let outMessage = strMessages.join("\n");
|
|
110
126
|
this._logger.log(level, outMessage);
|
|
111
127
|
return this;
|
|
112
128
|
}
|
package/package.json
CHANGED
package/src/UtilLogger.ts
CHANGED
|
@@ -24,6 +24,7 @@ colorizer.addColors({
|
|
|
24
24
|
silly: 'bold magenta'
|
|
25
25
|
});
|
|
26
26
|
|
|
27
|
+
|
|
27
28
|
export class SLogger{
|
|
28
29
|
/**获取一个Logger,如不存在则用默认参数创建
|
|
29
30
|
* @param {string} name - logger的名称 默认default
|
|
@@ -48,8 +49,18 @@ export class SLogger{
|
|
|
48
49
|
const transports:winston.transport[]=[];
|
|
49
50
|
if(outFloder!=null){
|
|
50
51
|
const fileFormat = winston.format.combine(
|
|
51
|
-
winston.format.timestamp({ format: '
|
|
52
|
-
winston.format.printf(info =>
|
|
52
|
+
winston.format.timestamp({ format: 'YY-MM-DD HH:mm:ss' }),
|
|
53
|
+
winston.format.printf((info) => {
|
|
54
|
+
const level = info.level.toUpperCase();
|
|
55
|
+
const message = info.message as string;
|
|
56
|
+
//格式化
|
|
57
|
+
let format = `[${info.timestamp}] [${level}]: `
|
|
58
|
+
let space = "";
|
|
59
|
+
for(let cha of format)
|
|
60
|
+
space+=" ";
|
|
61
|
+
let messageList = message.split("\n");
|
|
62
|
+
return `[${info.timestamp}] [${level.toUpperCase()}]: ${messageList.join("\n"+space)}`
|
|
63
|
+
}),
|
|
53
64
|
);
|
|
54
65
|
transports.push(new DailyRotateFile({
|
|
55
66
|
filename: path.join(outFloder,'log-%DATE%.txt'),
|
|
@@ -59,13 +70,21 @@ export class SLogger{
|
|
|
59
70
|
}));
|
|
60
71
|
}
|
|
61
72
|
const consoleFormat = winston.format.combine(
|
|
62
|
-
winston.format.timestamp({ format: '
|
|
63
|
-
winston.format.printf(info => {
|
|
73
|
+
winston.format.timestamp({ format: 'YY-MM-DD HH:mm:ss' }),
|
|
74
|
+
winston.format.printf((info) => {
|
|
64
75
|
const level = info.level.toUpperCase();
|
|
65
|
-
const message = info.message
|
|
76
|
+
const message = info.message as string;
|
|
66
77
|
const colorizedLevel = colorizer.colorize(info.level, level);
|
|
67
|
-
|
|
68
|
-
|
|
78
|
+
//格式化
|
|
79
|
+
let format = `[${info.timestamp}] [${level}]: `
|
|
80
|
+
let space = "";
|
|
81
|
+
for(let cha of format)
|
|
82
|
+
space+=" ";
|
|
83
|
+
let messageList = message.split("\n");
|
|
84
|
+
messageList[0]=colorizer.colorize(info.level, messageList[0])
|
|
85
|
+
for(let i=1;i<messageList.length;i++)
|
|
86
|
+
messageList[i]=colorizer.colorize(info.level, messageList[i]);
|
|
87
|
+
return `[${info.timestamp}] [${colorizedLevel}]: ${messageList.join("\n"+space)}`;
|
|
69
88
|
}),
|
|
70
89
|
);
|
|
71
90
|
transports.push(new winston.transports.Console({
|
|
@@ -109,14 +128,13 @@ export class SLogger{
|
|
|
109
128
|
for(let message of messages){
|
|
110
129
|
let out:string;
|
|
111
130
|
if(message == null)
|
|
112
|
-
out =
|
|
131
|
+
out = `<${typeof message}> ${typeof message}`;
|
|
113
132
|
else if(typeof message!=="string")
|
|
114
|
-
out
|
|
133
|
+
out=`<${typeof message}> ${message.toString()}`;
|
|
115
134
|
else out = message;
|
|
116
135
|
strMessages.push(out);
|
|
117
136
|
}
|
|
118
|
-
|
|
119
|
-
let outMessage = strMessages.join("\n - ");
|
|
137
|
+
let outMessage = strMessages.join("\n");
|
|
120
138
|
this._logger.log(level,outMessage);
|
|
121
139
|
return this;
|
|
122
140
|
}
|
package/test.js
CHANGED
|
@@ -31,7 +31,8 @@ async function main(){
|
|
|
31
31
|
|
|
32
32
|
main();
|
|
33
33
|
let a= {a:1};
|
|
34
|
-
SLogger.createLogger("default","silly")
|
|
34
|
+
//SLogger.createLogger("default","silly","./testlog","silly")
|
|
35
|
+
SLogger.createLogger("default","silly");
|
|
35
36
|
SLogger.fatal('This is an fatal message');
|
|
36
37
|
SLogger.error('This is an error message');
|
|
37
38
|
SLogger.warn('This is a warning message');
|
|
@@ -41,6 +42,6 @@ SLogger.verbose('This is a verbose message');
|
|
|
41
42
|
SLogger.debug('This is a debug message');
|
|
42
43
|
SLogger.silly('This is a silly message');
|
|
43
44
|
|
|
44
|
-
SLogger.
|
|
45
|
+
SLogger.fatal('This is an info message',123,true);
|
|
45
46
|
SLogger.info([1,2,3,4]);
|
|
46
47
|
|