8000 docs: added explanation of passing appender module in config, and types · Hacker-FEX/log4js-node@39cd2ef · GitHub
[go: up one dir, main page]

Skip to content

Commit 39cd2ef

Browse files
author
Gareth Jones
committed
docs: added explanation of passing appender module in config, and types
1 parent e0abff4 commit 39cd2ef

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# log4js-node changelog
22

3+
## 4.4.0
4+
* [Add option to pass appender module in config](https://github.com/log4js-node/log4js-node/pull/833) - thanks [@kaxelson](https://github.com/kaxelson)
5+
* [Updated dependencies](https://github.com/log4js-node/log4js-node/pull/900)
6+
37
## 4.3.2
48
* [Types for enableCallStack](https://github.com/log4js-node/log4js-node/pull/897) - thanks [@citrusjunoss](https://github.com/citrusjunoss)
59

docs/appenders.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,16 @@ Log4js checks the following places (in this order) for appenders based on the ty
6868
4. relative to the process' current working directory: `require(process.cwd() + '/' + type)`
6969

7070
If you want to write your own appender, read the [documentation](writing-appenders.md) first.
71+
72+
## Advanced configuration
73+
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
74+
in the appender module in the config instead of loading from the node.js require path. Here's an example:
75+
```javascript
76+
const myAppenderModule = {
77+
configure: (config, layouts, findAppender, levels) => { /* ...your appender config... */ }
78+
};
79+
log4js.configure({
80+
appenders: { custom: { type: myAppenderModule } },
81+
categories: { default: { appenders: ['custom'], level: 'debug' } }
82+
});
83+
```

lib/appenders/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ const loadAppenderModule = (type, config) => coreAppenders.get(type)
4242

4343
const createAppender = (name, config) => {
4444
const appenderConfig = config.appenders[name];
45-
const appenderModule =
46-
appenderConfig.type.configure ? appenderConfig.type : loadAppenderModule(appenderConfig.type, config);
45+
const appenderModule = appenderConfig.type.configure
46+
? appenderConfig.type : loadAppenderModule(appenderConfig.type, config);
4747
configuration.throwExceptionIf(
4848
config,
4949
configuration.not(appenderModule),

types/log4js.d.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ export interface CategoryFilterAppender {
104104
export interface NoLogFilterAppender {
105105
type: "noLogFilter";
106106
// the regular expression (or the regular expressions if you provide an array of values)
107-
// will be used for evaluating the events to pass to the appender.
108-
// The events, which will match the regular expression, will be excluded and so not logged.
107+
// will be used for evaluating the events to pass to the appender.
108+
// The events, which will match the regular expression, will be excluded and so not logged.
109109
exclude: string | string[];
110110
// the name of an appender, defined in the same configuration, that you want to filter.
111111
appender: string;
@@ -237,10 +237,14 @@ export interface StandardOutputAppender {
237237
}
238238

239239
export interface CustomAppender {
240-
type: string;
240+
type: string | AppenderModule;
241241
[key: string]: any;
242242
}
243243

244+
export interface AppenderModule {
245+
configure: Function
246+
}
247+
244248
export type Appender = CategoryFilterAppender
245249
| ConsoleAppender
246250
| FileAppender
@@ -253,7 +257,8 @@ export type Appender = CategoryFilterAppender
253257
| RecordingAppender
254258
| StandardErrorAppender
255259
| StandardOutputAppender
256-
| CustomAppender;
260+
| CustomAppender
261+
| AppenderModule;
257262

258263
export interface Levels {
259264
ALL: Level;

types/test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,9 @@ log4js.connectLogger(logger1, {
130130
log4js.connectLogger(logger2, {
131131
format: (req, _res, format) => format(`:remote-addr - ${req.id} - ":method :url HTTP/:http-version" :status :content-length ":referrer" ":user-agent"`)
132132
});
133+
134+
//support for passing in an appender module
135+
log4js.configure({
136+
appenders: { thing: { type: { configure: () => {} }}},
137+
categories: { default: { appenders: ['thing'], level: 'debug'}}
138+
});

0 commit comments

Comments
 (0)
0