共计 3076 个字符,预计需要花费 8 分钟才能阅读完成。
winston 被设计为一个简略且通用的日志库,反对多种传输。传输实质上是日志的存储设备。每个 winston 记录器都能够在不同级别配置多个存储渠道。例如,人们可能心愿将谬误日志存储在长久的近程地位(如数据库),但所有调试日志都输入到控制台或本地文件。
应用 winston 的举荐办法是创立您本人的记录器。最简略的办法是应用 winston.createLogger:
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: {service: 'user-service'},
transports: [
//
// - Write all logs with level `error` and below to `error.log`
// - Write all logs with level `info` and below to `combined.log`
//
new winston.transports.File({filename: 'error.log', level: 'error'}),
new winston.transports.File({filename: 'combined.log'}),
],
});
//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({...rest}) `
//
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({format: winston.format.simple(),
}));
}
winston 的日志等级
const levels = {
error: 0,
warn: 1,
info: 2,
http: 3,
verbose: 4,
debug: 5,
silly: 6
};
如何创立 logger
const logger = winston.createLogger({
transports: [new winston.transports.Console(),
new winston.transports.File({filename: 'combined.log'})
]
});
即便 logger 实例创立之后,也能容易地删除或者削减新的 transport:
const files = new winston.transports.File({filename: 'combined.log'});
const console = new winston.transports.Console();
logger
.clear() // Remove all transports
.add(console) // Add console transport
.add(files) // Add file transport
.remove(console); // Remove console transport
也能应用 logger 的 configure 办法,重新配置新的 transport:
const logger = winston.createLogger({
level: 'info',
transports: [new winston.transports.Console(),
new winston.transports.File({filename: 'combined.log'})
]
});
//
// Replaces the previous transports with those in the
// new configuration wholesale.
//
const DailyRotateFile = require('winston-daily-rotate-file');
logger.configure({
level: 'verbose',
transports: [new DailyRotateFile(opts)
]
});
在 winston 中,Logger 和 Transport 实例都被视为承受 info 对象的 objectMode 流。
提供给给定格局的 info 参数示意单个日志音讯。对象自身是可变的。每个信息必须至多具备 level 和 message 属性:
const info = {
level: 'info', // Level of the logging message
message: 'Hey! Log something?' // Descriptive message being logged.
};
除了级别和音讯之外的属性被视为“元属性”:
const {level, message, ...meta} = info;
咱们来入手写一个理论的例子。
主程序:
// @ts-nocheck
var express = require('express');
var app = express();
var DEFAULTPORT = 3003;
var port = process.env.PORT || DEFAULTPORT;
var winstonTest = require("./winstonTest");
app.get('/', function(_req, res){res.send("Hello world");
winstonTest.logMessage('234');
});
app.listen(port, function(){console.log("App listens on port:" + port);
});
winstonTest 的实现:
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: {service: 'user-service'},
transports: [
//
// - Write all logs with level `error` and below to `error.log`
// - Write all logs with level `info` and below to `combined.log`
//
new winston.transports.File({filename: 'data/error.log', level: 'error'}),
new winston.transports.File({filename: 'data/combined.log'}),
]
});
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({format: winston.format.simple(),
}));
}
console.log('Logger initialized successfully!');
function log(content){logger.info('hello', { message: content});
}
module.exports = {logMessage: log};
浏览器里输出如下 url:
http://localhost:3003/
在 combined.log 里生成了如下 log 文件:
这就是 winston 最根本的应用办法。
正文完