docs: added explanation of passing appender module in config, and types

This commit is contained in:
Gareth Jones 2019-06-26 07:57:28 +10:00
parent e0abff4c86
commit 39cd2ef3f6
5 changed files with 34 additions and 6 deletions

View File

@ -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)

View File

@ -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' } }
});
```

View File

@ -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),

13
types/log4js.d.ts vendored
View File

@ -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;

View File

@ -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'}}
});