Loggerhead-module
A simple npm module to send frontend log messages to a server.
How it works
Loggerhead-module.js
sends a log message to a configurable endpoint URL by a POST request each time one of the log level
function is called.
Log levels
Log levels are
info
debug
warning
error
By default all levels are enabled. Whenever a log level function is called, it receives a string message, it prepares a POST
request and it forwards the message to the set URL
, injecting in the POST
data the log level
information. At the same time, all the same correspondant levels are available and enabled for the console
to be shown. The enable/disable can be toggled by config parameters.
Note: each log level function returns the Promise
that sends the POST
request, this way it is possible to add a .then()
slice in order to apply some other action on the response
(if any) from the server after storing the log message. See an example below:
Loggerhead ;
Headers
Loggerhead-module.js
provides also a way to customize headers
values of the POST
request: this can be used to add some authentication parameters, for instance. The default method behaves like a proxy
receiving and returning the default headers
map (*). In order to add more headers
parameters this method can be overridden by a function that receives and returns the headers
map as well, but it does something in the middle. See below an example:
// this is the default methodLoggerhead { return headers;} // this overrides the default method adding the `X-CSRF-Token` parameters in the `headers` mapLoggerhead { headers; return headers;}
(*) Note: by default headers
contains only {'Content-Type': 'application/json; charset=utf-8')}
Installation
npm install loggerhead-module
How to use
/* Minimal code to get Loggerhead working properly */ const Loggerhead = ; /* Let's use Loggerhead functions to send some log messages */ Loggerhead; Loggerhead ; /* Let's disable debug log level */ Loggerhead; /* Let's disable info and debug levels for the console */ Loggerhead;
Use case
This module
can be used in a browser scenario, adding listeners
for specific events
and callback
log functions in order to send and store log messages about the event
that just triggered the Loggerhead
.
// store a log message about the page has been loaded window; // store a log message about a page has been left window; // store a log message about the error that just happened window;
Config parameters
// the server endpoint where to send logsurl: String // log levels, enabled by defaultlevels: info: Boolean debug: Boolean warning: Boolean error: Boolean // console log levels, enabled by defaultconsole: info: Boolean debug: Boolean warning: Boolean error: Boolean
Parameters are configurable passing a partial or complete config object with desired values at the beginning to the create
method call, or later on to the set
method:
const Loggerhead = ; Loggerhead;