-
Notifications
You must be signed in to change notification settings - Fork 0
Create Subscriber
In order to create a new Subscriber, there are some things that you need to keep in mind.
Coming out from the way the stream works, the minimum requirements for a new Subscriber are the following:
- Be a constructor, accepting
settings {Object}parameter - Contain a
handlefunction, that accepts amessage {Object}
ES6 syntax:
class CustomSubscriber {
constructor(settings) {
this.settings = settings || {};
}
handle(message) {
// logic
}
}
module.exports = CustomSubscriber;settings contains config parameters of the stream
The message object contains the following set of properties:
message.payload:
{
level: {'silly'|'verbose'|'debug'|'info'|'warn'|'error'},
text: {string},
meta: {
instanceId: {string},
notify: {true}, // by default,
stack: {string}, // in case error was given to log,
... (other metadata provided in runtime)
},
error: {Error} // in case message was an error
}message.logLevels
message.logLevels is an ES6 Map, containing - entries of the log levels.
silly -> 0
verbose -> 1
debug -> 2
info -> 3
warn -> 4
error -> 5
Message object also exposes the following function:
message.getPrefix(settings {Object}, delimiter {string})
This function returns a prefix for the message based on settings and / or env variables.
By default, a delimiter is :
If you want to allow user to pre-configure your subscriber, instead of exposing a class:
module.exports = CustomSubscriber;expose the wrapper function:
const logtify = require('logtify');
const streamBuffer = logtify.streamBuffer;
const { stream } = logtify();
module.exports = (configs) => {
// some logic, validation, etc
const subscriberData = {
class: CustomSubscriber,
config: configs
};
streamBuffer.addSubscriber(subscriberData);
const mergedConfigs = Object.assign({}, configs, stream.settings);
stream.subscribe(new CustomSubscriber(mergedConfigs));
return subscriberData;
}This way the stream will take the given configs, merged with the original stream's settings and pass it to the constructor of a class during the subscriber initialization.
Exposing an adapter
expose the wrapper function:
const logtify = require('logtify');
const streamBuffer = logtify.streamBuffer;
const { stream } = logtify();
module.exports = (configs) => {
// some logic, validation, etc
const subscriberData = {
class: CustomSubscriber,
config: configs,
adapter: {
name: 'unicorn',
class: CustomAdapter
}
};
streamBuffer.addSubscriber(subscriberData);
const mergedConfigs = Object.assign({}, configs, stream.settings);
stream.subscribe(new CustomSubscriber(mergedConfigs));
stream.bindAdapter('unicorn', new CustomAdapter(stream, mergedConfigs));
return subscriberData;
}After this point, when a user uses your subscriber with a stream, he the adapter will be automatically initialized and will be then accessible via:
const { unicorn } = require('logtify')(...);Stream may get some presets as part of the settings. You can create yours and process them within your particular subscriber:
class CustomSubscriber {
constructor(settings) {
if (settings.presets) {
this.presets = settings.presets; // {Array} of strings
}
}
}