@webimpian/zonemta-log-central 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +81 -0
  3. package/index.js +135 -0
  4. package/package.json +32 -0
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Webimpian
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # ZoneMTA → Log Central
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@webimpian/zonemta-log-central.svg)](https://www.npmjs.com/package/@webimpian/zonemta-log-central)
4
+ [![License](https://img.shields.io/npm/l/@webimpian/zonemta-log-central.svg)](LICENSE.md)
5
+
6
+ [ZoneMTA](https://github.com/zone-eu/zone-mta) plugin that ships mail delivery logs to a [Log Central](https://log.dev-aplikasiniaga.com) server: every queued, delivered, deferred, and bounced message becomes a searchable log entry with message-id, recipient, sending zone, and the remote MX response.
7
+
8
+ Built on [`@webimpian/log-central-node`](https://www.npmjs.com/package/@webimpian/log-central-node) — batched background delivery, capped backoff, and bounded memory, so a Log Central outage can never disrupt mail flow. Hook handlers swallow their own failures and always advance ZoneMTA's plugin chain.
9
+
10
+ ## Installation
11
+
12
+ Inside your ZoneMTA application directory:
13
+
14
+ ```bash
15
+ npm install @webimpian/zonemta-log-central
16
+ ```
17
+
18
+ Enable the plugin in the ZoneMTA config:
19
+
20
+ ```toml
21
+ # config/plugins/log-central.toml
22
+ ["modules/@webimpian/zonemta-log-central"]
23
+ enabled=["receiver", "main", "sender"]
24
+ url="https://logs.example.com/api"
25
+ token="your-project-key"
26
+ app="your-mta-slug"
27
+ environment="production"
28
+ channel="zonemta"
29
+ ```
30
+
31
+ Alternatively, if you keep local plugins in a `plugins/` folder, drop in a one-line shim as `plugins/log-central.js`:
32
+
33
+ ```js
34
+ module.exports = require('@webimpian/zonemta-log-central');
35
+ ```
36
+
37
+ and enable it as `["log-central"]` with the same options.
38
+
39
+ ## What gets logged
40
+
41
+ | ZoneMTA hook | Level | Entry |
42
+ |---|---|---|
43
+ | `message:queue` | info | Message accepted into the queue (id, from, recipients, interface) |
44
+ | `sender:delivered` | info | MX accepted the message (recipient, zone, MX response) |
45
+ | `sender:responseError` | warning | Delivery deferred/failed (recipient, zone, error response) |
46
+ | `queue:bounce` | error | Message bounced permanently (recipient, response, category) |
47
+ | `log:entry` | debug | Raw ZoneMTA message-event feed — **off by default** |
48
+
49
+ Toggle any of them in the config:
50
+
51
+ ```toml
52
+ ["modules/@webimpian/zonemta-log-central".events]
53
+ queued=false
54
+ raw=true
55
+ ```
56
+
57
+ ## Options
58
+
59
+ | Option | Default | Purpose |
60
+ |---|---|---|
61
+ | `url` | *(required)* | Log Central ingest base URL, e.g. `https://logs.example.com/api` |
62
+ | `token` | *(required)* | The app's project key (bearer token) |
63
+ | `app` | *(required)* | The app's slug, must match the token's app |
64
+ | `environment` | `production` | Environment tag on every entry |
65
+ | `channel` | `zonemta` | Log channel shown in the Log Central viewer |
66
+ | `hostname` | `os.hostname()` | Hostname tag on every entry |
67
+ | `batchSize` / `flushInterval` / `maxQueue` | see client | Passed through to [`@webimpian/log-central-node`](https://www.npmjs.com/package/@webimpian/log-central-node) |
68
+ | `events` | all on, `raw` off | Toggle individual hooks (see above) |
69
+
70
+ If `url`, `token`, or `app` is missing, the plugin logs a warning through ZoneMTA's logger and disables itself — it never prevents ZoneMTA from starting.
71
+
72
+ ## Testing
73
+
74
+ ```bash
75
+ npm install
76
+ npm test
77
+ ```
78
+
79
+ ## License
80
+
81
+ MIT — see [LICENSE.md](LICENSE.md).
package/index.js ADDED
@@ -0,0 +1,135 @@
1
+ 'use strict';
2
+
3
+ const { LogCentralClient } = require('@webimpian/log-central-node');
4
+
5
+ const DEFAULT_EVENTS = {
6
+ queued: true,
7
+ delivered: true,
8
+ deferred: true,
9
+ bounced: true,
10
+ raw: false,
11
+ };
12
+
13
+ module.exports.title = 'Log Central';
14
+
15
+ module.exports.init = function (app, done) {
16
+ const config = app.config || {};
17
+
18
+ let client;
19
+
20
+ try {
21
+ client = new LogCentralClient({
22
+ url: config.url,
23
+ token: config.token,
24
+ app: config.app,
25
+ environment: config.environment || 'production',
26
+ channel: config.channel || 'zonemta',
27
+ hostname: config.hostname,
28
+ batchSize: config.batchSize,
29
+ flushInterval: config.flushInterval,
30
+ maxQueue: config.maxQueue,
31
+ fetch: config.fetch,
32
+ });
33
+ } catch (err) {
34
+ if (app.logger && typeof app.logger.error === 'function') {
35
+ app.logger.error('LogCentral', 'plugin disabled: %s', err.message);
36
+ }
37
+
38
+ return done();
39
+ }
40
+
41
+ app.logCentral = client;
42
+
43
+ const events = Object.assign({}, DEFAULT_EVENTS, config.events);
44
+
45
+ // A hook that throws breaks ZoneMTA's plugin chain and with it the mail
46
+ // flow, so every handler swallows its own errors and always calls next.
47
+ const safely = handler => (...args) => {
48
+ const next = typeof args[args.length - 1] === 'function' ? args.pop() : null;
49
+
50
+ try {
51
+ handler(...args);
52
+ } catch {
53
+ // never disrupt mail flow because of logging
54
+ }
55
+
56
+ if (next) {
57
+ next();
58
+ }
59
+ };
60
+
61
+ if (events.queued) {
62
+ app.addHook('message:queue', safely((envelope, messageInfo) => {
63
+ const recipients = Array.isArray(envelope.to) ? envelope.to : [];
64
+
65
+ client.info(
66
+ `Queued ${envelope.id} from ${envelope.from || '<>'} to ${recipients.length} recipient${recipients.length === 1 ? '' : 's'}`,
67
+ {
68
+ id: envelope.id,
69
+ from: envelope.from,
70
+ to: recipients,
71
+ interface: envelope.interface,
72
+ origin: envelope.origin,
73
+ transtype: envelope.transtype,
74
+ subject: messageInfo && messageInfo.subject,
75
+ },
76
+ );
77
+ }));
78
+ }
79
+
80
+ if (events.delivered) {
81
+ app.addHook('sender:delivered', safely((delivery, info) => {
82
+ client.info(`Delivered ${delivery.id}.${delivery.seq} to ${delivery.recipient}`, {
83
+ id: delivery.id,
84
+ seq: delivery.seq,
85
+ recipient: delivery.recipient,
86
+ domain: delivery.domain,
87
+ zone: delivery.sendingZone,
88
+ mx: info && (info.mx || info.host),
89
+ response: info && info.response,
90
+ });
91
+ }));
92
+ }
93
+
94
+ if (events.deferred) {
95
+ app.addHook('sender:responseError', safely((delivery, connection, err) => {
96
+ const reason = (err && (err.response || err.message)) || 'unknown error';
97
+
98
+ client.warning(`Delivery failed for ${delivery.id}.${delivery.seq} to ${delivery.recipient}: ${reason}`, {
99
+ id: delivery.id,
100
+ seq: delivery.seq,
101
+ recipient: delivery.recipient,
102
+ domain: delivery.domain,
103
+ zone: delivery.sendingZone,
104
+ response: err && err.response,
105
+ category: err && err.category,
106
+ });
107
+ }));
108
+ }
109
+
110
+ if (events.bounced) {
111
+ app.addHook('queue:bounce', safely(bounce => {
112
+ const recipient = bounce.to || bounce.recipient || 'unknown recipient';
113
+
114
+ client.error(`Bounced ${bounce.id}${bounce.seq ? `.${bounce.seq}` : ''} to ${recipient}: ${bounce.response || ''}`, {
115
+ id: bounce.id,
116
+ seq: bounce.seq,
117
+ from: bounce.from,
118
+ to: recipient,
119
+ response: bounce.response,
120
+ category: bounce.category,
121
+ zone: bounce.sendingZone,
122
+ });
123
+ }));
124
+ }
125
+
126
+ if (events.raw) {
127
+ app.addHook('log:entry', safely(entry => {
128
+ const message = (entry && (entry.short_message || entry.action)) || 'zonemta log entry';
129
+
130
+ client.debug(message, entry || {});
131
+ }));
132
+ }
133
+
134
+ done();
135
+ };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@webimpian/zonemta-log-central",
3
+ "version": "1.0.0",
4
+ "description": "ZoneMTA plugin that ships mail delivery logs (queued, delivered, deferred, bounced) to a Log Central server.",
5
+ "keywords": [
6
+ "zonemta",
7
+ "zone-mta",
8
+ "logging",
9
+ "log-central"
10
+ ],
11
+ "license": "MIT",
12
+ "author": "Khairul Imran <infrastructure@webimpian.com>",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/webimpian-os/zonemta-log-central.git"
16
+ },
17
+ "engines": {
18
+ "node": ">=18"
19
+ },
20
+ "main": "index.js",
21
+ "files": [
22
+ "index.js",
23
+ "LICENSE.md",
24
+ "README.md"
25
+ ],
26
+ "dependencies": {
27
+ "@webimpian/log-central-node": "^1.0.0"
28
+ },
29
+ "scripts": {
30
+ "test": "node --test"
31
+ }
32
+ }