Initial commit on Forgejo
Fresh repository history for elektrine/elektrine-haraka hosted at https://git.elektrine.com/elektrine/elektrine-haraka.
This commit is contained in:
commit
67c1d41a0c
82 changed files with 8713 additions and 0 deletions
65
lib/telemetry.js
Normal file
65
lib/telemetry.js
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
/**
|
||||
* Minimal structured telemetry helper.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
function compact(fields) {
|
||||
const out = {};
|
||||
Object.entries(fields || {}).forEach(([key, value]) => {
|
||||
if (value !== undefined) out[key] = value;
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
function emit(log_fn, level, scope, event, fields = {}) {
|
||||
const payload = compact({
|
||||
ts: new Date().toISOString(),
|
||||
level,
|
||||
scope,
|
||||
event,
|
||||
...fields
|
||||
});
|
||||
|
||||
const line = JSON.stringify(payload);
|
||||
log_fn(line);
|
||||
}
|
||||
|
||||
function create_plugin_logger(plugin, scope) {
|
||||
return {
|
||||
info(event, fields) {
|
||||
emit((line) => plugin.loginfo(line), 'info', scope, event, fields);
|
||||
},
|
||||
warn(event, fields) {
|
||||
emit((line) => plugin.logwarn(line), 'warn', scope, event, fields);
|
||||
},
|
||||
error(event, fields) {
|
||||
emit((line) => plugin.logerror(line), 'error', scope, event, fields);
|
||||
},
|
||||
debug(event, fields) {
|
||||
emit((line) => plugin.logdebug(line), 'debug', scope, event, fields);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function create_console_logger(scope) {
|
||||
return {
|
||||
info(event, fields) {
|
||||
emit((line) => console.log(line), 'info', scope, event, fields);
|
||||
},
|
||||
warn(event, fields) {
|
||||
emit((line) => console.warn(line), 'warn', scope, event, fields);
|
||||
},
|
||||
error(event, fields) {
|
||||
emit((line) => console.error(line), 'error', scope, event, fields);
|
||||
},
|
||||
debug(event, fields) {
|
||||
emit((line) => console.debug(line), 'debug', scope, event, fields);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
create_plugin_logger,
|
||||
create_console_logger
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue