How to make Winston work with Webpack?

I have an electronic application that uses node.js. I would like to use Winston to enter the application. I added winston to the package.json file, but when I run the build command for webpack, I get some warnings from the colors.js dependency in winston.

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

He then refers to winston and colors.js. Ignoring warnings does not work because the electronic application receives an exception while trying to download some files from winston.

I did some digging on SO and the github site, and they say colors.js have some dynamic requirements that cause problems with webpack. I also saw that other sample projects seem to work and work without any problems in their projects. Does anyone know how to properly enable winston registration package with webpack in an electronic application?

+8
javascript angular webpack electron winston
source share
1 answer

The problem is that Webpack cannot process the dynamic require statement in colors.js. So, you need to tell Webpack that Winston is an external library.

Here is 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 angular 2 electronically, create a logger.js file and then wrap it with the global TypeScript log service file (i.e. logging.service.ts). The logger.js file creates a registrar variable with the required 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'); 

The logging service is now available for use throughout the application.

Example:

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

All Articles