Remote logging
Good practices
- Do not make a REST call for every line, instead, batch the calls over some time (e.g. 5-30 seconds of logs at a time)
- Use JSON for all your logs. Newline-separated JSON is a decent default format.
- Add metadata on the server side:
- current time
- client session (e.g. a random id)
- other metadata (e.g. client account)
- If you want to machine-parse your logs or run analytics on them, you might want to log everything as a single object {} with different keys.
- Wait until jQuery is loaded via .jQueryWait
For example, use "op" / "event" with a human-legible description, and separate the parameters of the operation:
{ "op": "publish", "value": "Hello world", "to": "user/1" }
which is close enough to be user-readable.
Logging as JSON over a remote connection
One way to do this is to use the jQuery backend, which provides a bit more robustness. You need a server that receives the POST request and appends the log lines to a file (log lines are passed as an array):
And then, you'd configure Minilog something like this (with a filter that takes all logs from "foo" and "bar" that are not "debug" level):
AjaxLogger.jQueryWait( // ... ); ;(function() { Minilog // allow when the name matches foo or bar, and level >= debug // remove the line to pipe all log lines .pipe(new Minilog.Filter().allow(/^(foo|bar).*/, 'debug')) .pipe(new Minilog.backends.jQuery({ url: 'http://localhost:8080/log', interval: 5000 })); }());
Writing Node backends
When running in Node, you should use the Stringify formatter, since internally the streams have a richer signature which makes things easier to express:
write(name, level, args)
but Node's streams want strings:
write(chunk, [encoding], [callback])
Minilog v1 automatically stringified inputs, but this made the experience much worse in all cases other than where you were piping to a disk since the streams then needed to re-parse the chunks.
To use external APIs which expect .write(str)
, use the Stringifier formatter first:
Minilog.pipe(new Minilog.Stringifier()).pipe(process.stdout);