我正在创建一个完整的MEAN技术栈应用,使用的技术包括:
Node.js,Angular 6,Express.js和MongoDB。
我已经成功地创建了服务器,并且它运行得非常完美。在我的应用程序中记录错误时,我决定使用Winston Logger
而不是console.log
。这是我现在的代码:
服务器端
var appRoot = require('app-root-path');
var winston = require('winston');
// define the custom settings for each transport (file, console)
var options = {
file: {
level: 'info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
console: {
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
},
};
// instantiate a new Winston Logger with the settings defined above
const logger = winston.createLogger({
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console)
],
exitOnError: false, // do not exit on handled exceptions
});
// create a stream object with a 'write' function that will be used by `morgan`
logger.stream = {
write: function (message, encoding) {
// use the 'info' log level so the output will be picked up by both transports (file and console)
logger.info(message);
},
};
module.exports = logger;
注意:Winston在服务器端可以完美运行
客户端
我想在我的Angular 6客户端应用程序中使用winston。
例如,在我的某个组件中,我有以下代码:
import * as logger from "winston";
.........
this.activeRouter.params.subscribe((params) => {
// tslint:disable-next-line:prefer-const
let id = params['id'];
this.moviesService.getReview(id)
.subscribe(review => {
console.log(review);
this.review = review;
});
});
你可以看到我正在使用console.log(review)
,但我想使用Winston
。
如何在客户端使用Winston日志记录器
?我是一个新手,对这些东西一窍不通,任何帮助都将不胜感激。