node-test-bed-adapter
Version:
An adapter to connect a node.js application to the Test-bed's Common Information Space or Common Simulation Space.
55 lines • 1.7 kB
JavaScript
import { LogLevelToType } from './index.mjs';
import { LogTopic } from '../index.mjs';
/**
* Based on winston-k
* source: https://github.com/jackielihf/winston-k/blob/master/logger.js
*/
export class KafkaLogger {
adapter;
id;
isInitialized = false;
stringBasedKey;
constructor(options) {
this.id = options.clientId;
this.adapter = options.adapter;
this.stringBasedKey =
typeof options.stringBasedKey === 'undefined'
? true
: options.stringBasedKey;
// this.adapter
// .addProducerTopics(LogTopic)
// .then(() => (this.isInitialized = true));
}
log(level, msg, callback) {
if (this.isInitialized === false) {
return;
}
const payload = {
topic: LogTopic,
messages: [
{
key: this.stringBasedKey ? this.id.toString() : undefined,
value: {
id: this.id,
level: LogLevelToType(level),
dateTimeSent: Date.now(),
log: msg,
},
},
],
};
this.adapter.send(payload, (err, res) => {
if (err) {
if (typeof err === 'string') {
err = `[KAFKA] ${err}`;
}
else if (err.hasOwnProperty('message')) {
err.message = `[KAFKA] ${err.message}`;
}
return callback(err, null);
}
callback(undefined, res);
});
}
}
//# sourceMappingURL=kafka-logger.mjs.map