cctech:batch-logger

v0.6.2Published last year

Batch - Logger

Basically a custom Logger that is a wrapper around the winston dependency. Passes chunked batches of client logs to the server through the binary protocol to not overload the server.

Configuration

Client's logger

namedefaultdescription
storageNamelogsName of the IndexedDB storage
cachedLogsLimit50How many logs can be cached in case of IndexedDB not being available
sendLogsTimeout3000Batched logs will be sent to the server after not receiving any for this timeout. Timeout is reseted every time logs were passed to the server or were added to the IndexedDB
forceSendLogsTimeout10000Batched logs will be sent to the server always after no longer than this timeout. Every time logs will be passed to the server this timeout is reseted.
resendResponseLimit20000After what time the logs should be resend when there was no confirmation from the server.
chunkSize50Maximum amount of logs to be sent in one chunk.
serverHookMeteor.connectionHook to the DDP connection where logs should be sent. documentation
fetchDatemoment.utc().format('YYYY-MM-DD HH:mm:ss.SSS')Overriding the date of client logs sent to the server.

Example usage

1import BatchLogger from 'meteor/cctech:batch-logger';
2
3const Logger = new BatchLogger({
4    storageName: 'movies',
5    cachedLogsLimit: 10,
6    sendLogsTimeout: 1000,
7    forceSendLogsTimeout: 5000,
8    resendResponseLimit: 10000,
9    chunkSize: 2,
10    serverHook: DDP.connect('127.0.0.1:3000'),
11    fetchDate: () => moment.utc().format('YYYY-MM-DD HH:mm:ss.SSS')
12});

Server's logger

namedefaultdescription
minimumLogLeveldebugMinimum level of logs to be displayed
showConsoletrueDetermines if logs should be printed in the server console
clientFileSettingsnullConfiguration for file saving logs from the client
serverFileSettingsnullConfiguration for file saving logs from the server
lokiSettingsnullConfiguration for sending logs to Grafana Loki service
onReceiveCallbackempty functionConfiguration for onReceive callback
cacheCleanupInterval600000How often cached logs hash should be checks
cachedLogsTTL2How long should be cached logs md5 hash (in hours - min is 2)
requireClientAuthenticationfalseRequire user to be authenticated to accept logs from client

clientFileSettings & serverFileSettings

namedefaultdescription
fileNameclient / serverName of the file with a path to store logs
maxSize26214400Maximum size of one of the logs files
maxFiles5Maximum amount of log files
leveldebugMinimum level of log to be saved

Example usage

1import BatchLogger from 'meteor/cctech:batch-logger';
2
3const clientFileSettings = {
4    fileName: './data/client-movies',
5    maxSize: 26214400,
6    maxFiles: 2,
7    level: 'warn'
8};
9
10const serverFileSettings = {
11    fileName: './data/server-movies',
12    maxSize: 46214400,
13    maxFiles: 5,
14    level: 'debug'
15};
16
17const Logger = new BatchLogger({
18    minimumLogLevel: 'warn',
19    showConsole: false,
20    clientFileSettings,
21    serverFileSettings
22});

API

The usage is straightforward with API offering debug/info/warn/error methods.

debug(...messages, options (optional))

info(...messages, options (optional))

warn(...messages, options (optional))

error(...messages, options (optional))

example

1Logger.debug('My', 'message', 'in', 'parts', {
2    date: new Date(),
3    labels: { UI: 'abcdefg' }
4});

forceSendLogs()

You can at any time force the app to send all the stored logs. This can be really usefull if you want for example to close the app but make sure that before it all logs will be received on the server.

example

1Logger.forceSendLogs().then(() => {
2    app.close();
3});

deactivate()

Can be invoked only if it's active

Deactivates Batch Logging and returns every log that was not sent to the server or did not receive any confirmation from it.

example

1Logger.deactivate().then((logs) => {
2    // logs - batched logs that were not sent to the server (or did not receive confirmation back)
3});

activate()

Can be invoked only if it's not active. That means that deactivate had to be run previously.

Activates again the Batch Logging.

example

1Logger.activate();

applyOffsetToLogs(number: offset)

Client only

If there are no cached/stored logs (all were sent to the server) then this method won't do anything

Applies provided offset to each cached/stored log

example

1const fiveSeconds = 5000;
2Logger.applyOffsetToLogs(fiveSeconds);

This example will add 5 seconds to each log's date

FailOver

IndexedDB is not supported by the device

In case of not being able to open/access IndexedDB the mechanism will send every received log instantly to the server using the protocol. The logs won't be batched anymore.

Can not make a proper DDP connection to the receiver

All logs will still be constantly being stored to the internal's IndexedDB storage. However they won't be sent to the receiver.

If you are aware that there is a problem with the receiver and you want to send the logs somewhere else then check out the DDP documentation and switch the receiver to a new one.

However, if you don't have any additional receiver then you can run deactivate and retrieve the logs stored in IndexedDB.