Skip to content Skip to sidebar Skip to footer

How Do I Get Winston To Work With Webpack?

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 bu

Solution 1:

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:

varwinston= 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: [
            newwinston.transports.Console( {
                level: 'warn', // Only write logs of warn level or higher
                colorize: true
            } ),
            newwinston.transports.File( {
                level: env === 'development' ? 'debug' : 'info',
                filename: logDir + '/logs.log',
                maxsize: 1024 * 1024 * 10// 10MB
            } )
        ],
        exceptionHandlers: [
            newwinston.transports.File( {
                filename: 'log/exceptions.log'
            } )
        ]
    } );
    ​
    module.exports = logger;

logging.service.ts:

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

Post a Comment for "How Do I Get Winston To Work With Webpack?"