8000 Merge pull request #668 from log4js-node/gelf-deprecation · xyz-data/log4js-node@9700cf5 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 9700cf5

Browse files
authored
Merge pull request log4js-node#668 from log4js-node/gelf-deprecation
chore: deprecated the gelf appender
2 parents 0a483ec + f9ced3e commit 9700cf5

File tree

6 files changed

+120
-58
lines changed

6 files changed

+120
-58
lines changed

docs/appenders.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ The following appenders are included with log4js. Some require extra dependencie
2424
* [dateFile](dateFile.md)
2525
* [file](file.md)
2626
* [fileSync](fileSync.md)
27-
* [gelf](gelf.md)
2827
* [hipchat](hipchat.md)
2928
* [logFaces-HTTP](logFaces-HTTP.md)
3029
* [logFaces-UDP](logFaces-UDP.md)
@@ -43,6 +42,16 @@ The following appenders are included with log4js. Some require extra dependencie
4342
* [stdout](stdout.md)
4443
* [rabbitmq](rabbitmq.md)
4544

45+
## Optional Appenders
46+
47+
The following appenders are supported by log4js, but will issue deprecation warnings from version 2.6 onwards - they will be removed from the log4js core in version 3. If you are using these appenders, you should alter your dependencies to include them explicitly.
48+
49+
* [gelf](https://github.com/log4js-node/gelf)
50+
51+
For example, if you were previously using the gelf appender (`type: 'gelf'`) then you should add `@log4js-node/gelf` to your dependencies and change the type to `type: '@log4js-node/gelf'`.
52+
53+
To turn off the deprecation warnings, add `deprecationWarnings: false` to your log4js config. The core version of the appender will still work. But note that you will have to install the external appenders when version 3 is released as they will not be included at all.
54+
4655
## Other Appenders
4756

4857
Log4js can load appenders from outside the core appenders. The `type` config value is used as a require path if no matching appender can be found. For example, the following configuration will attempt to load an appender from the module 'cheese/appender', passing the rest of the config for the appender to that module:

docs/gelf.md

Lines changed: 0 additions & 53 deletions
This file was deleted.

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ There have been a few changes between log4js 1.x and 2.x (and 0.x too). You shou
1212
* coloured console logging to [stdout](stdout.md) or [stderr](stderr.md)
1313
* [file appender](file.md), with configurable log rolling based on file size or [date](dateFile.md)
1414
* [SMTP appender](smtp.md)
15-
* [GELF appender](gelf.md)
15+
* [GELF appender](https://github.com/log4js-node/gelf)
1616
* [Loggly appender](loggly.md)
1717
* [Logstash UDP appender](logstashUDP.md)
1818
* logFaces ([UDP](logFaces-UDP.md) and [HTTP](logFaces-HTTP.md)) appender

lib/appenders/gelf.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict';
22

3+
/**
4+
* This appender has been deprecated.
5+
* Updates and bug fixes should be made against https://github.com/lgo4js-node/gelf
6+
*/
37
const zlib = require('zlib');
48
// const levels = require('../levels');
59
const dgram = require('dgram');
@@ -131,6 +135,9 @@ function gelfAppender(layout, config, levels) {
131135
}
132136
};
133137

138+
// trigger a deprecation warning, with a pointer to the replacement lib
139+
app.deprecated = '@log4js-node/gelf';
140+
134141
return app;
135142
}
136143

lib/configuration.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,22 @@ class Configuration {
8585
debug(`pm2 enabled ? ${this.pm2}`);
8686
debug(`pm2InstanceVar = ${this.pm2InstanceVar}`);
8787
debug(`process.env[${this.pm2InstanceVar}] = ${process.env[this.pm2InstanceVar]}`);
88-
return appenderModule.configure(
88+
89+
const appender = appenderModule.configure(
8990
config,
9091
layouts,
9192
this.configuredAppenders.get.bind(this.configuredAppenders),
9293
this.configuredLevels
9394
);
95+
96+
if (appender.deprecated && this.deprecationWarnings) {
97+
console.error(`Appender "${name}" uses a deprecated type "${config.type}", ` + // eslint-disable-line
98+
'which will be removed in log4js v3. ' +
99+
`You should change it to use "${appender.deprecated}". ` +
100+
'To turn off this warning add "deprecationWarnings: false" to your config.');
101+
}
102+
103+
return appender;
94104
}
95105
return () => {};
96106
}
@@ -204,6 +214,10 @@ class Configuration {
204214
this.throwExceptionIf(not(anObject(candidate.categories)), 'must have a property "categories" of type object.');
205215

206216
this.disableClustering = this.candidate.disableClustering || !cluster;
217+
this.deprecationWarnings = true;
218+
if ('deprecationWarnings' in this.candidate) {
219+
this.deprecationWarnings = this.candidate.deprecationWarnings;
220+
}
207221

208222
this.pm2 = this.candidate.pm2;
209223
this.pm2InstanceVar = this.candidate.pm2InstanceVar || 'NODE_APP_INSTANCE';

test/tap/configuration-validation-test.js

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const util = require('util');
66
const path = require('path');
77
const sandbox = require('sandboxed-module');
88

9-
function testAppender(label) {
9+
function testAppender(label, deprecated) {
1010
return {
1111
configure: function (config, layouts, findAppender) {
1212
return {
@@ -15,7 +15,8 @@ function testAppender(label) {
1515
label: label,
1616
config: config,
1717
layouts: layouts,
18-
findAppender: findAppender
18+
findAppender: findAppender,
19+
deprecated: deprecated
1920
};
2021
}
2122
};
@@ -324,5 +325,89 @@ test('log4js configuration validation', (batch) => {
324325
t.end();
325326
});
326327

328+
batch.test('should display deprecation warning if needed', (t) => {
329+
let deprecationMessage;
330+
const SandboxedConfiguration = sandbox.require(
331+
'../../lib/configuration',
332+
{
333+
singleOnly: true,
334+
requires: {
335+
'./appenders/gelf': testAppender('gelf', '@some/lib')
336+
},
337+
globals: {
338+
console: {
339+
error: (msg) => {
340+
deprecationMessage = msg;
341+
}
342+
}
343+
}
344+
}
345+
);
346+
347+
const config = new SandboxedConfiguration({
348+
appenders: { thing: { type: 'gelf' } },
349+
categories: { default: { appenders: ['thing'], level: 'debug' } }
350+
});
351+
352+
t.test('should output warning on console.error', (assert) => {
353+
assert.equal(
354+
deprecationMessage,
355+
'Appender "thing" uses a deprecated type "gelf", which will be removed in log4js v3. ' +
356+
'You should change it to use "@some/lib". ' +
357+
'To turn off this warning add "deprecationWarnings: false" to your config.'
358+
);
359+
assert.end();
360+
});
361+
362+
t.test('should still return an appender', (assert) => {
363+
const thing = config.appenders.get('thing');
364+
assert.ok(thing.configureCalled);
365+
assert.equal(thing.type, 'gelf');
366+
assert.end();
367+
});
368+
369+
t.end();
370+
});
371+
372+
batch.test('should not display deprecation warning if turned off', (t) => {
373+
let deprecationMessage;
374+
const SandboxedConfiguration = sandbox.require(
375+
'../../lib/configuration',
376+
{
377+
singleOnly: true,
378+
requires: {
379+
'./appenders/gelf': testAppender('gelf', '@some/lib')
380+
},
381+
globals: {
382+
console: {
383+
error: (msg) => {
384+
deprecationMessage = msg;
385+
}
386+
}
387+
}
388+
}
389+
);
390+
391+
const config = new SandboxedConfiguration({
392+
appenders: { thing: { type: 'gelf' } },
393+
categories: { default: { appenders: ['thing'], level: 'debug' } },
394+
deprecationWarnings: false
395+
});
396+
397+
t.test('should not output warning on console.error', (assert) => {
398+
assert.notOk(deprecationMessage);
399+
assert.end();
400+
});
401+
402+
t.test('should still return an appender', (assert) => {
403+
const thing = config.appenders.get('thing');
404+
assert.ok(thing.configureCalled);
405+
assert.equal(thing.type, 'gelf');
406+
assert.end();
407+
});
408+
409+
t.end();
410+
});
411+
327412
batch.end();
328413
});

0 commit comments

Comments
 (0)
0