如何让Winston与Webpack合作? [英] How do I get Winston to work with Webpack?

查看:80
本文介绍了如何让Winston与Webpack合作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 '...依赖的请求是表达式...'

然后引用winston和colors.js。忽略警告不起作用,因为电子应用程序尝试从winston加载某些文件时出现异常。



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

解决方案

此问题有两个方面:



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



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



<要告诉Webpack相应的动态需求,你需要告诉Webpack Winston是一个外部库。



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

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

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



logger.js:

  var winston = require('winston'),
fs = require('fs'),
logDir ='log',//或者从配置中读取
env = process.env.NODE_ENV || '开发',
logger;



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

if(!fs.existsSync(logDir)){
//如果目录不存在则创建目录
fs.mkdirSync(logDir);
}
logger = new(winston.Logger)({
transports:[
new winston.transports.Console({
level:'warn',//只写警告级别或更高的日志
colorize:true
}),
new winston.transports.File({
level:env ==='development'?'debug ':'info',
文件名:logDir +'/ logs.log',
maxsize:1024 * 1024 * 10 // 10MB
})
],
exceptionHandlers:[
new winston.transports.File({
filename:'log / exceptions.log'
})
]
});

module.exports = logger;

logging.service.ts:

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

现在,日志服务可在整个应用程序中使用。



示例:

 从'< path>导入{LoggerService} ;'; 
...
LoggerService.log('info','用户登录成功'+ this.user.email);


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...'

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.

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?

解决方案

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

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

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': '{}'
},

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:

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

这篇关于如何让Winston与Webpack合作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆