Search code examples
node.jswebpacklog4js-node

log4js appenders with webpack


Log4js uses dynamic require for loading appenders. Webpack doesn't know at build time which appender will be used at runtime.

How can I use not build in appenders? like @log4js-node/logstashudp


Solution

  • Let's say you want to add a new appender (that's not build in like tcp,file) like @log4js-node/logstashudp or any other appender.

    So instead of:

    log4js.configure({
      appenders: {
        udpAppender: {
          type: '@log4js-node/logstashudp',
          .....
        }
      }
    })
    

    You need to do it like this:

    import * as udp from '@log4js-node/logstashudp'
    log4js.configure({
      appenders: {
        udpAppender: {
          type: udp,
          .....
        }
      }
    })
    

    In short, you should load your module and send it in type property.

    Therefore, appender's module wont load in runtime and the webpack problem is solved!