Skip to content

Commit

Permalink
feat: log format (#47)
Browse files Browse the repository at this point in the history
* prettier log
  • Loading branch information
meooxx authored Sep 14, 2022
1 parent 50e5a2a commit a98d7b4
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions log.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const winston = require('winston');
const { format } = winston;
const { combine, timestamp, label, printf } = format;
const util = require('util');
const { combine, timestamp, label, printf, colorize } = format;

const myFormat = printf(({ level, message, timestamp }) => {
return `${timestamp}-${level}: ${message}`;
return util.format('%s-%s', timestamp, level, message);
});

const logger = winston.createLogger({
Expand All @@ -13,6 +14,7 @@ const logger = winston.createLogger({
timestamp({
format: 'HH:mm:ss'
}),
colorize(),
myFormat
),
defaultMeta: { service: 'yuye' },
Expand All @@ -22,22 +24,22 @@ const logger = winston.createLogger({
level: 'error'
}),
new winston.transports.File({ filename: './log/combined.log' }),
new winston.transports.Console()
new winston.transports.Console({
level: 'silly'
})
]
});

Object.assign(console, {
log(...arg) {
logger.info(arg.join(''));
logger.info(util.format(...arg));
},
error(...arg) {
logger.error(arg.join(''));
logger.error(util.format(...arg));
},
info(...arg) {
logger.info(arg.join(''));
logger.info(util.format(...arg));
}
});


module.exports = logger;

0 comments on commit a98d7b4

Please sign in to comment.