mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
Merge pull request #698 from log4js-node/remove-mailgun
Remove mailgun appender
This commit is contained in:
commit
24da52ea7c
@ -28,7 +28,6 @@ The following appenders are included with log4js. Some require extra dependencie
|
||||
* [logFaces-UDP](logFaces-UDP.md)
|
||||
* [logLevelFilter](logLevelFilter.md)
|
||||
* [logstashHTTP](logstashHTTP.md)
|
||||
* [mailgun](mailgun.md)
|
||||
* [multiFile](multiFile.md)
|
||||
* [multiprocess](multiprocess.md)
|
||||
* [rabbitmq](rabbitmq.md)
|
||||
@ -49,6 +48,7 @@ The following appenders are supported by log4js, but are no longer distributed w
|
||||
* [hipchat](https://github.com/log4js-node/hipchat)
|
||||
* [loggly](https://github.com/log4js-node/loggly)
|
||||
* [logstashUDP](https://github.com/log4js-node/logstashUDP)
|
||||
* [mailgun](https://github.com/log4js-node/mailgun)
|
||||
|
||||
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'`.
|
||||
|
||||
|
||||
@ -1,30 +0,0 @@
|
||||
# Mailgun Appender
|
||||
|
||||
This appender uses the [mailgun](https://www.mailgun.com) service to send log messages as emails. It requires the [mailgun-js](https://www.npmjs.com/package/mailgun-js) package to be added to your dependencies.
|
||||
|
||||
## Configuration
|
||||
|
||||
* `type` - `mailgun`
|
||||
* `apiKey` - `string` - your mailgun API key
|
||||
* `domain` - `string` - your domain
|
||||
* `from` - `string`
|
||||
* `to` - `string`
|
||||
* `subject` - `string`
|
||||
* `layout` - `object` (optional, defaults to basicLayout) - see [layouts](layouts.md)
|
||||
|
||||
The body of the email will be the result of applying the layout to the log event. Refer to the mailgun docs for how to obtain your API key.
|
||||
|
||||
## Example
|
||||
|
||||
```javascript
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
type: 'mailgun',
|
||||
apiKey: '123456abc',
|
||||
domain: 'some.company',
|
||||
from: 'logging@some.service',
|
||||
to: 'important.bosses@some.company',
|
||||
subject: 'Error: Developers Need To Be Fired'
|
||||
}
|
||||
});
|
||||
```
|
||||
@ -1,35 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const mailgunFactory = require('mailgun-js');
|
||||
|
||||
function mailgunAppender(config, layout) {
|
||||
const mailgun = mailgunFactory({
|
||||
apiKey: config.apikey,
|
||||
domain: config.domain
|
||||
});
|
||||
|
||||
return (loggingEvent) => {
|
||||
const data = {
|
||||
from: config.from,
|
||||
to: config.to,
|
||||
subject: config.subject,
|
||||
text: layout(loggingEvent, config.timezoneOffset)
|
||||
};
|
||||
|
||||
/* eslint no-unused-vars:0 */
|
||||
mailgun.messages().send(data, (error, body) => {
|
||||
if (error !== null) console.error('log4js.mailgunAppender - Error happened', error);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function configure(config, layouts) {
|
||||
let layout = layouts.basicLayout;
|
||||
if (config.layout) {
|
||||
layout = layouts.layout(config.layout.type, config.layout);
|
||||
}
|
||||
|
||||
return mailgunAppender(config, layout);
|
||||
}
|
||||
|
||||
module.exports.configure = configure;
|
||||
@ -62,7 +62,6 @@
|
||||
"validate-commit-msg": "^2.14.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"mailgun-js": "^0.7.0",
|
||||
"nodemailer": "^2.5.0",
|
||||
"redis": "^2.7.1",
|
||||
"slack-node": "~0.2.0",
|
||||
|
||||
@ -1,182 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const test = require('tap').test;
|
||||
const layouts = require('../../lib/layouts');
|
||||
const sandbox = require('@log4js-node/sandboxed-module');
|
||||
|
||||
function setupLogging(category, options) {
|
||||
const msgs = [];
|
||||
|
||||
const mailgunCredentials = {
|
||||
apiKey: options.apikey,
|
||||
domain: options.domain
|
||||
};
|
||||
|
||||
const fakeMailgun = function () {
|
||||
return {
|
||||
messages: function () {
|
||||
return {
|
||||
config: options,
|
||||
send: function (data, callback) {
|
||||
msgs.push(data);
|
||||
callback(false, { status: 'OK' });
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const fakeLayouts = {
|
||||
layout: function (type, config) {
|
||||
this.type = type;
|
||||
this.config = config;
|
||||
return layouts.messagePassThroughLayout;
|
||||
},
|
||||
basicLayout: layouts.basicLayout,
|
||||
messagePassThroughLayout: layouts.messagePassThroughLayout
|
||||
};
|
||||
|
||||
const fakeConsole = {
|
||||
errors: [],
|
||||
logs: [],
|
||||
error: function (msg, value) {
|
||||
this.errors.push({ msg: msg, value: value });
|
||||
},
|
||||
log: function (msg, value) {
|
||||
this.logs.push({ msg: msg, value: value });
|
||||
}
|
||||
};
|
||||
|
||||
const log4js = sandbox.require('../../lib/log4js', {
|
||||
requires: {
|
||||
'mailgun-js': fakeMailgun,
|
||||
'./layouts': fakeLayouts
|
||||
},
|
||||
globals: {
|
||||
console: fakeConsole
|
||||
}
|
||||
});
|
||||
options = options || {};
|
||||
options.type = 'mailgun';
|
||||
log4js.configure({
|
||||
appenders: { mailgun: options },
|
||||
categories: { default: { appenders: ['mailgun'], level: 'trace' } }
|
||||
});
|
||||
|
||||
return {
|
||||
logger: log4js.getLogger(category),
|
||||
mailer: fakeMailgun,
|
||||
layouts: fakeLayouts,
|
||||
console: fakeConsole,
|
||||
mails: msgs,
|
||||
credentials: mailgunCredentials
|
||||
};
|
||||
}
|
||||
|
||||
function checkMessages(assert, result) {
|
||||
for (let i = 0; i < result.mails.length; ++i) {
|
||||
assert.equal(result.mails[i].from, 'sender@domain.com');
|
||||
assert.equal(result.mails[i].to, 'recepient@domain.com');
|
||||
assert.equal(result.mails[i].subject, 'This is subject');
|
||||
assert.ok(new RegExp(`.+Log event #${i + 1}`).test(result.mails[i].text));
|
||||
}
|
||||
}
|
||||
|
||||
test('log4js mailgunAppender', (batch) => {
|
||||
batch.test('mailgun setup', (t) => {
|
||||
const result = setupLogging('mailgun setup', {
|
||||
apikey: 'APIKEY',
|
||||
domain: 'DOMAIN',
|
||||
from: 'sender@domain.com',
|
||||
to: 'recepient@domain.com',
|
||||
subject: 'This is subject'
|
||||
});
|
||||
|
||||
t.test('mailgun credentials should match', (assert) => {
|
||||
assert.equal(result.credentials.apiKey, 'APIKEY');
|
||||
assert.equal(result.credentials.domain, 'DOMAIN');
|
||||
assert.end();
|
||||
});
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('basic usage', (t) => {
|
||||
const result = setupLogging('basic usage', {
|
||||
apikey: 'APIKEY',
|
||||
domain: 'DOMAIN',
|
||||
from: 'sender@domain.com',
|
||||
to: 'recepient@domain.com',
|
||||
subject: 'This is subject'
|
||||
});
|
||||
|
||||
result.logger.info('Log event #1');
|
||||
|
||||
t.equal(result.mails.length, 1, 'should be one message only');
|
||||
checkMessages(t, result);
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('config with layout', (t) => {
|
||||
const result = setupLogging('config with layout', {
|
||||
layout: {
|
||||
type: 'tester'
|
||||
}
|
||||
});
|
||||
t.equal(result.layouts.type, 'tester', 'should configure layout');
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('error when sending email', (t) => {
|
||||
const setup = setupLogging('separate email for each event', {
|
||||
apikey: 'APIKEY',
|
||||
domain: 'DOMAIN',
|
||||
from: 'sender@domain.com',
|
||||
to: 'recepient@domain.com',
|
||||
subject: 'This is subject'
|
||||
});
|
||||
|
||||
setup.mailer.messages = function () {
|
||||
return {
|
||||
send: function (msg, cb) {
|
||||
cb({ msg: 'log4js.mailgunAppender - Error happened' }, null);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
setup.logger.info('This will break');
|
||||
const cons = setup.console;
|
||||
|
||||
t.test('should be logged to console', (assert) => {
|
||||
assert.equal(cons.errors.length, 1);
|
||||
assert.equal(cons.errors[0].msg, 'log4js.mailgunAppender - Error happened');
|
||||
assert.end();
|
||||
});
|
||||
t.end();
|
||||
});
|
||||
|
||||
batch.test('separate email for each event', (t) => {
|
||||
const setup = setupLogging('separate email for each event', {
|
||||
apikey: 'APIKEY',
|
||||
domain: 'DOMAIN',
|
||||
from: 'sender@domain.com',
|
||||
to: 'recepient@domain.com',
|
||||
subject: 'This is subject'
|
||||
});
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #1');
|
||||
}, 0);
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #2');
|
||||
}, 500);
|
||||
setTimeout(() => {
|
||||
setup.logger.info('Log event #3');
|
||||
}, 1100);
|
||||
setTimeout(() => {
|
||||
t.equal(setup.mails.length, 3, 'should be three messages');
|
||||
checkMessages(t, setup);
|
||||
t.end();
|
||||
}, 3000);
|
||||
});
|
||||
|
||||
batch.end();
|
||||
});
|
||||
14
types/log4js.d.ts
vendored
14
types/log4js.d.ts
vendored
@ -215,19 +215,6 @@ export interface LogLevelFilterAppender {
|
||||
maxLevel?: string;
|
||||
}
|
||||
|
||||
export interface MailgunAppender {
|
||||
type: 'mailgun';
|
||||
// your mailgun API key
|
||||
apiKey: string;
|
||||
// your domain
|
||||
domain: string;
|
||||
from: string;
|
||||
to: string;
|
||||
subject: string;
|
||||
// (defaults to basicLayout)
|
||||
layout?: Layout;
|
||||
}
|
||||
|
||||
export interface MultiFileAppender {
|
||||
type: 'multiFile';
|
||||
// the base part of the generated log filename
|
||||
@ -354,7 +341,6 @@ export type Appender = CategoryFilterAppender
|
||||
| LogFacesHTTPAppender
|
||||
| LogFacesUDPAppender
|
||||
| LogLevelFilterAppender
|
||||
| MailgunAppender
|
||||
| MultiFileAppender
|
||||
| MultiprocessAppender
|
||||
| RedisAppender
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user