【发布时间】:2018-12-09 03:21:44
【问题描述】:
我正在使用
创建完整的平均堆栈应用程序NodeJs、Angular 6、ExpressJs 和 MongoDB
我已经设法创建了一个服务器并且它工作得很好,而不是在我的应用程序中记录错误时使用console.log 我决定使用Winston Logger 这是我现在所拥有的
服务器端
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 logger?我是所有这些东西的新手,我们将不胜感激。
【问题讨论】:
标签: javascript node.js angular express winston