如何让 Winston 使用 Webpack?

How do I get Winston to work with Webpack?(如何让 Winston 使用 Webpack?)
本文介绍了如何让 Winston 使用 Webpack?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个使用 node.js 的电子应用程序.我想使用 Winston 登录应用程序.我已将 winston 添加到我的 package.json 文件中,但是当我为 webpack 运行构建命令时,我收到了来自 winston 中的 colors.js 依赖项的一些警告.

I have an electron application which is using node.js. I would like to use Winston for logging in the application. I've added winston to my package.json file, but when I run the build command for webpack I'm getting some warnings from the colors.js dependency in winston.

'...the request of a dependency is an expression...'

然后它引用winston 和colors.js.忽略警告不起作用,因为电子应用程序在尝试从 winston 加载一些文件时遇到异常.

It then references winston and colors.js. Ignoring the warnings doesn't work, as the electron application gets an exception trying to load some files from winston.

我在 SO 和 github 站点上进行了一些挖掘,他们说 colors.js 有一些动态的 require 语句,webpack 有问题.我还看到其他示例项目似乎已经启动并运行了winston,并且在他们的项目中没有任何问题.有谁知道如何在电子应用程序中正确包含带有 webpack 的 winston 日志记录包?

I did some digging on SO and the github site and they say that colors.js has some dynamic require statements that webpack is having issues with. I've also seen that other sample projects seem to have winston up and running without any issues in their projects. Does anyone know how to correctly include the winston logging package with webpack in an electron app?

推荐答案

这个问题有两个方面:

1) winston 直接或间接依赖于 color.js,因此一旦 winston 存在,依赖项就会自动包含在内.在它的一些旧版本中,它包含一个动态的 require 语句,这导致:

1) winston directly or indirectly depends on color.js, so that dependency automatically gets included, once winston is there. In some older versions of it, it included a dynamic require statement, which leads to this:

2) 一个依赖有动态的 require 语句,Webpack 无法处理;您可以配置 webpack 以便它可以忽略此特定情况,或者也可以将 winston 升级到更新版本,因此 color.js 将在没有动态要求的变体中被选择(参见 https://github.com/winstonjs/winston/issues/984).

2) a dependency has a dynamic require statement that Webpack cannot handle; you can either configure webpack so it can ignore this specific case, or also upgrade winston to a newer version, so color.js will be picked in a variant without that dynamic require (see https://github.com/winstonjs/winston/issues/984).

要告诉 Webpack 与动态 require 相处,你需要告诉 Webpack Winston 是一个外部库.

To tell Webpack to get along with the dynamic require, you need to tell Webpack that Winston is an external library.

这是我的 webpack.config.js 中的一个示例:

Here's an example from my webpack.config.js:

 externals: {
    'electron': 'require("electron")',
    'net': 'require("net")',
    'remote': 'require("remote")',
    'shell': 'require("shell")',
    'app': 'require("app")',
    'ipc': 'require("ipc")',
    'fs': 'require("fs")',
    'buffer': 'require("buffer")',
    'winston': 'require("winston")',
    'system': '{}',
    'file': '{}'
},

要使用电子使记录器在 Angular 2 应用程序中可用,请创建一个 logger.js 文件,然后用全局日志记录服务 TypeScript 文件(即 logging.service.ts)包装它.logger.js 文件使用所需的 Winston 配置设置创建 logger 变量.

To make the logger available in an angular 2 app using electron, create a logger.js file and then wrap it with a global logging service TypeScript file (i.e. logging.service.ts). The logger.js file creates the logger variable with the desired Winston configuration settings.

logger.js:

    var winston = require( 'winston' ),
    fs = require( 'fs' ),
    logDir = 'log', // Or read from a configuration
    env = process.env.NODE_ENV || 'development',
    logger;



     winston.setLevels( winston.config.npm.levels );
    winston.addColors( winston.config.npm.colors );

    if ( !fs.existsSync( logDir ) ) {
        // Create the directory if it does not exist
        fs.mkdirSync( logDir );
    }
    logger = new( winston.Logger )( {
        transports: [
            new winston.transports.Console( {
                level: 'warn', // Only write logs of warn level or higher
                colorize: true
            } ),
            new winston.transports.File( {
                level: env === 'development' ? 'debug' : 'info',
                filename: logDir + '/logs.log',
                maxsize: 1024 * 1024 * 10 // 10MB
            } )
        ],
        exceptionHandlers: [
            new winston.transports.File( {
                filename: 'log/exceptions.log'
            } )
        ]
    } );
    
    module.exports = logger;

logging.service.ts:

logging.service.ts:

export var LoggerService = require('./logger.js');

现在日志服务可以在整个应用程序中使用.

Now the logging service is available for use throughout the application.

例子:

import {LoggerService} from '<path>';
...    
LoggerService.log('info', 'Login successful for user ' + this.user.email);

这篇关于如何让 Winston 使用 Webpack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)