winston 3.2.1 版本没有创建日志文件
winston 3.2.1 version is not creating log file
我尝试使用节点 js.winston 2.4.0 版本创建 winston(记录器文件),它成功地将信息和错误存储在日志中 file.but 最新版本它创建了日志文件,但无法存储错误和信息messages.How 修复它
Winston.js
var winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.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()
}));
}
app.js
/**
* @fileoverview Root file of the application.
*/
// Import ExpressJS
const winston = require('winston');
const express = require('express');
const app = express();
// StartUp Processes
require('./startup/logging_error_startup');
require('./startup/routes_startup')(app);
require('./startup/check_security_key_startup')();
require('./startup/validation_startup')();
require('./startup/swagger_startup')(app);
require('./startup/production_startup')(app);
// Start the server
const port = process.env.PORT || 4202;
app.listen(port, () => winston.info(`Server Started and Listening on port ${port}`));
我得到了输出
winston] Attempt to write logs with no transports {"message":"Server Started and Listening on port 4202","level":"info"}
您需要将 Winston.js
文件中的记录器对象导入 app.js
。在您的 app.js
代码中,Winston 导入是针对 npm 包的,而不是针对在 Winston.js
中创建的记录器对象。
在Winston.js中导出记录器对象:
var winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.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()
}));
}
module.exports = logger;
并且在 app.js 中,在导入后使用该记录器对象。
const logger = require('./Winston'); //assuming Winston.js is in the same folder level as app.js
const express = require('express');
const app = express();
// StartUp Processes
require('./startup/logging_error_startup');
require('./startup/routes_startup')(app);
require('./startup/check_security_key_startup')();
require('./startup/validation_startup')();
require('./startup/swagger_startup')(app);
require('./startup/production_startup')(app);
// Start the server
const port = process.env.PORT || 4202;
app.listen(port, () => logger.info(`Server Started and Listening on port ${port}`));
或者,您可以使用
修改winston.js
中的默认记录器
var winston = require('winston');
const newLogger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [.........
winston.add(newLogger);
而不是
var winston = require('winston');
const logger = winston.createLogger({....
module.exports = logger
并使用
在每个文件中导入默认记录器
const logger = require('winston')
logger.info('log');
每次将记录器导入不同文件时无需处理相对路径。
我尝试使用节点 js.winston 2.4.0 版本创建 winston(记录器文件),它成功地将信息和错误存储在日志中 file.but 最新版本它创建了日志文件,但无法存储错误和信息messages.How 修复它
Winston.js
var winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.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()
}));
}
app.js
/**
* @fileoverview Root file of the application.
*/
// Import ExpressJS
const winston = require('winston');
const express = require('express');
const app = express();
// StartUp Processes
require('./startup/logging_error_startup');
require('./startup/routes_startup')(app);
require('./startup/check_security_key_startup')();
require('./startup/validation_startup')();
require('./startup/swagger_startup')(app);
require('./startup/production_startup')(app);
// Start the server
const port = process.env.PORT || 4202;
app.listen(port, () => winston.info(`Server Started and Listening on port ${port}`));
我得到了输出
winston] Attempt to write logs with no transports {"message":"Server Started and Listening on port 4202","level":"info"}
您需要将 Winston.js
文件中的记录器对象导入 app.js
。在您的 app.js
代码中,Winston 导入是针对 npm 包的,而不是针对在 Winston.js
中创建的记录器对象。
在Winston.js中导出记录器对象:
var winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.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()
}));
}
module.exports = logger;
并且在 app.js 中,在导入后使用该记录器对象。
const logger = require('./Winston'); //assuming Winston.js is in the same folder level as app.js
const express = require('express');
const app = express();
// StartUp Processes
require('./startup/logging_error_startup');
require('./startup/routes_startup')(app);
require('./startup/check_security_key_startup')();
require('./startup/validation_startup')();
require('./startup/swagger_startup')(app);
require('./startup/production_startup')(app);
// Start the server
const port = process.env.PORT || 4202;
app.listen(port, () => logger.info(`Server Started and Listening on port ${port}`));
或者,您可以使用
修改winston.js
中的默认记录器
var winston = require('winston');
const newLogger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [.........
winston.add(newLogger);
而不是
var winston = require('winston');
const logger = winston.createLogger({....
module.exports = logger
并使用
在每个文件中导入默认记录器const logger = require('winston')
logger.info('log');
每次将记录器导入不同文件时无需处理相对路径。