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
name | default | description |
---|---|---|
storageName | logs | Name of the IndexedDB storage |
cachedLogsLimit | 50 | How many logs can be cached in case of IndexedDB not being available |
sendLogsTimeout | 3000 | Batched 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 |
forceSendLogsTimeout | 10000 | Batched 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. |
resendResponseLimit | 20000 | After what time the logs should be resend when there was no confirmation from the server. |
chunkSize | 50 | Maximum amount of logs to be sent in one chunk. |
serverHook | Meteor.connection | Hook to the DDP connection where logs should be sent. documentation |
fetchDate | moment.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
name | default | description |
---|---|---|
minimumLogLevel | debug | Minimum level of logs to be displayed |
showConsole | true | Determines if logs should be printed in the server console |
clientFileSettings | null | Configuration for file saving logs from the client |
serverFileSettings | null | Configuration for file saving logs from the server |
lokiSettings | null | Configuration for sending logs to Grafana Loki service |
onReceiveCallback | empty function | Configuration for onReceive callback |
cacheCleanupInterval | 600000 | How often cached logs hash should be checks |
cachedLogsTTL | 2 | How long should be cached logs md5 hash (in hours - min is 2) |
requireClientAuthentication | false | Require user to be authenticated to accept logs from client |
clientFileSettings & serverFileSettings
name | default | description |
---|---|---|
fileName | client / server | Name of the file with a path to store logs |
maxSize | 26214400 | Maximum size of one of the logs files |
maxFiles | 5 | Maximum amount of log files |
level | debug | Minimum 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.