我有一个在Node.js(服务器端)上运行的应用程序,我想只根据环境变量记录错误(例如:log_level = error node api.js),所以这只记录控制台上的错误。
我有一个使用winston库的log.js文件
var fs = require("fs"),
mkdirp = require("mkdirp"),
path = require("path"),
winston = require("winston");
//const env = process.env.node_env || "development";
var filename = path.join(__dirname, "../app-debug.log");
//
// Remove the file, ignoring any errors
//
try {
fs.unlinkSync(filename);
} catch (ex) {}
var logger = new(winston.Logger)({
levels: {
trace: 0,
input: 1,
verbose: 2,
error: 3,
debug: 4,
info: 5,
data: 6,
help: 7,
warn: 8
},
colors: {
trace: "magenta",
input: "grey",
verbose: "cyan",
prompt: "red",
error: "blue",
info: "green",
data: "grey",
help: "cyan",
warn: "yellow"
},
transports: [
new(winston.transports.Console)({
prettyPrint: true,
colorize: true,
silent: false,
timestamp: true,
level: "error"
}),
new(winston.transports.File)({
filename: filename
})
]
});
module.exports = logger;
此文件位于我的根目录中,现在发生的是所有日志级别都打印在我的控制台中。我做错了什么?有什么帮助吗?