简体   繁体   English

Winston Logger 可以用在前端做日志记录吗?

[英]Can Winston Logger be used on the front-end for logging?

I am creating full mean stack app with我正在创建完整的平均堆栈应用程序

NodeJs, Angular 6, ExpressJs and MongoDB NodeJs、Angular 6、ExpressJs 和 MongoDB

I have managed to create a server and its working perfectly, instead of using console.log when logging errors in my app I have decided to use Winston Logger here is what I have now我已经设法创建了一个服务器并且它运行良好,而不是在我的应用程序中记录错误时使用console.log我决定在这里使用Winston Logger这就是我现在拥有的

Server side服务器端

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;

Note: Winston in server side works perfectly注意:服务器端的 Winston 完美运行

Client-Side客户端

I want to use winston in my client side angular 6 app.我想在我的客户端 angular 6 应用程序中使用 winston。

Example: in one of my components i have this.示例:在我的一个组件中,我有这个。

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;
        });
    });

As you can see I am using console.log(review) , Instead of console log I would like to use Winston .如您所见,我正在使用console.log(review) ,我想使用Winston而不是控制台日志。

How to use Winston logger in client-side?如何在客户端使用Winston logger am newbie to all this stuff any help will be apreciated.我是所有这些东西的新手,我们将不胜感激。

Yeah it is possible, however default transport for browser is very limited.是的,这是可能的,但是浏览器的默认传输非常有限。 I recommend to use https://www.npmjs.com/package/winston-transport-browserconsole我建议使用https://www.npmjs.com/package/winston-transport-browserconsole

npm install winston-transport-browserconsole -S

It is easy to use and supports logging json objects:它易于使用并支持记录 json 对象:

import * as winston from "winston";
import BrowserConsole from 'winston-transport-browserconsole';

const level = "debug";
winston.configure({
    transports: [
        new BrowserConsole(
            {
                format: winston.format.simple(),
                level,
            },
        ),
    ],
});

winston.debug("DEBUG ", {a: 1, b: "two"});

Yes - it can (technically) be used in the browser.是的 - 它可以(技术上)在浏览器中使用。 Should it be?应该是吗? Almost definitely not (sadly).几乎绝对不是(可悲的)。 Winston is a fantastic logger for node. Winston 是一个很棒的节点记录器。 But, emphasis on "for node".但是,强调“for node”。 If you want to use it on the client, you would need to add a bunch of node polyfills in addition to winston itself, which is very large relative to other client loggers.如果你想在客户端使用它,除了winston本身,你还需要添加一堆节点polyfill,这相对于其他客户端记录器来说是非常大的。 Between winston and those polyfills you are going to significantly increase the size of your artifact.在 winston 和那些 polyfills 之间,您将显着增加工件的大小。 Also, just fyi webpack 5 removed those node polyfills, so you would need to add them back manually.此外,仅供参考 webpack 5 删除了这些节点 polyfill,因此您需要手动添加它们。

According to this ticket: https://github.com/winstonjs/winston/issues/287 it's almost ready for browser use?根据这张票: https ://github.com/winstonjs/winston/issues/287 它几乎可以用于浏览器了? Or mostly ready?还是准备好了? It sounds like they recently started supporting logging in a browser environment.听起来他们最近开始支持在浏览器环境中登录。

可以在此处找到使用 winston 进行同构应用程序https://github.com/winstonjs/winston/issues/287#issuecomment-1171983379

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM