log4js appenders with webpack

354 Views Asked by At

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

1

There are 1 best solutions below

0
Alon Malivanchuk On BEST ANSWER

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!