简体   繁体   English

如何让Winston与Webpack合作?

[英]How do I get Winston to work with Webpack?

I have an electron application which is using node.js. 我有一个使用node.js的电子应用程序。 I would like to use Winston for logging in the application. 我想使用Winston登录应用程序。 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. 我已经将winston添加到我的package.json文件中,但是当我运行webpack的build命令时,我从winston中的colors.js依赖项中得到了一些警告。

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

It then references winston and colors.js. 然后它引用了winston和colors.js。 Ignoring the warnings doesn't work, as the electron application gets an exception trying to load some files from winston. 忽略警告不起作用,因为电子应用程序尝试从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. 我在SO和github网站上做了一些挖掘,他们说colors.js有一些webpack遇到问题的动态需求语句。 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? 有谁知道如何在电子应用中正确包含带有webpack的winston日志包?

There are two sides to this issue: 这个问题有两个方面:

1) winston directly or indirectly depends on color.js, so that dependency automatically gets included, once winston is there. 1)winston直接或间接依赖于color.js,因此一旦winston存在,依赖关系就会自动包含在内。 In some older versions of it, it included a dynamic require statement, which leads to this: 在它的一些旧版本中,它包含一个动态需求语句,这导致:

2) a dependency has a dynamic require statement that Webpack cannot handle; 2)依赖项具有Webpack无法处理的动态require语句; 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以便它可以忽略这个特定的情况,或者也可以将winston升级到更新的版本,因此color.js将在没有动态需求的变体中被选中(参见https://github.com/winstonjs/winston/)问题/ 984 )。

To tell Webpack to get along with the dynamic require, you need to tell Webpack that Winston is an external library. 要告诉Webpack与动态需求相处,您需要告诉Webpack Winston是一个外部库。

Here's an example from my webpack.config.js: 这是我的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': '{}'
},

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 (ie logging.service.ts). 要使用电子在角度2应用程序中使用记录器,请创建一个logger.js文件,然后使用全局日志记录服务TypeScript文件(即logging.service.ts)将其包装。 The logger.js file creates the logger variable with the desired Winston configuration settings. logger.js文件使用所需的Winston配置设置创建记录器变量。

logger.js: 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. 现在,日志服务可在整个应用程序中使用。

Example: 例:

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

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

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