diff --git a/CHANGELOG.md b/CHANGELOG.md index 07a887d..bff971e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # log4js-node changelog +## 4.4.0 +* [Add option to pass appender module in config](https://github.com/log4js-node/log4js-node/pull/833) - thanks [@kaxelson](https://github.com/kaxelson) +* [Updated dependencies](https://github.com/log4js-node/log4js-node/pull/900) + ## 4.3.2 * [Types for enableCallStack](https://github.com/log4js-node/log4js-node/pull/897) - thanks [@citrusjunoss](https://github.com/citrusjunoss) diff --git a/docs/appenders.md b/docs/appenders.md index c511ca4..15153a6 100644 --- a/docs/appenders.md +++ b/docs/appenders.md @@ -68,3 +68,16 @@ Log4js checks the following places (in this order) for appenders based on the ty 4. relative to the process' current working directory: `require(process.cwd() + '/' + type)` If you want to write your own appender, read the [documentation](writing-appenders.md) first. + +## Advanced configuration +If you've got a custom appender of your own, or are using webpack (or some other bundler), you may find it easier to pass +in the appender module in the config instead of loading from the node.js require path. Here's an example: +```javascript +const myAppenderModule = { + configure: (config, layouts, findAppender, levels) => { /* ...your appender config... */ } +}; +log4js.configure({ + appenders: { custom: { type: myAppenderModule } }, + categories: { default: { appenders: ['custom'], level: 'debug' } } +}); +``` diff --git a/lib/appenders/index.js b/lib/appenders/index.js index c012142..ab78fcc 100644 --- a/lib/appenders/index.js +++ b/lib/appenders/index.js @@ -42,8 +42,8 @@ const loadAppenderModule = (type, config) => coreAppenders.get(type) const createAppender = (name, config) => { const appenderConfig = config.appenders[name]; - const appenderModule = - appenderConfig.type.configure ? appenderConfig.type : loadAppenderModule(appenderConfig.type, config); + const appenderModule = appenderConfig.type.configure + ? appenderConfig.type : loadAppenderModule(appenderConfig.type, config); configuration.throwExceptionIf( config, configuration.not(appenderModule), diff --git a/types/log4js.d.ts b/types/log4js.d.ts index a3c56ac..5d2e8a1 100644 --- a/types/log4js.d.ts +++ b/types/log4js.d.ts @@ -104,8 +104,8 @@ export interface CategoryFilterAppender { export interface NoLogFilterAppender { type: "noLogFilter"; // the regular expression (or the regular expressions if you provide an array of values) - // will be used for evaluating the events to pass to the appender. - // The events, which will match the regular expression, will be excluded and so not logged. + // will be used for evaluating the events to pass to the appender. + // The events, which will match the regular expression, will be excluded and so not logged. exclude: string | string[]; // the name of an appender, defined in the same configuration, that you want to filter. appender: string; @@ -237,10 +237,14 @@ export interface StandardOutputAppender { } export interface CustomAppender { - type: string; + type: string | AppenderModule; [key: string]: any; } +export interface AppenderModule { + configure: Function +} + export type Appender = CategoryFilterAppender | ConsoleAppender | FileAppender @@ -253,7 +257,8 @@ export type Appender = CategoryFilterAppender | RecordingAppender | StandardErrorAppender | StandardOutputAppender - | CustomAppender; + | CustomAppender + | AppenderModule; export interface Levels { ALL: Level; diff --git a/types/test.ts b/types/test.ts index 918ffca..66c7ad3 100644 --- a/types/test.ts +++ b/types/test.ts @@ -130,3 +130,9 @@ log4js.connectLogger(logger1, { log4js.connectLogger(logger2, { format: (req, _res, format) => format(`:remote-addr - ${req.id} - ":method :url HTTP/:http-version" :status :content-length ":referrer" ":user-agent"`) }); + +//support for passing in an appender module +log4js.configure({ + appenders: { thing: { type: { configure: () => {} }}}, + categories: { default: { appenders: ['thing'], level: 'debug'}} +});