Angular 1 (currently using 1.3.20) logging module supporing logz.io integration
This example module is a working module used in our application, but I haven't had time to "bowerize" it into a module. This should drop into an existing angular 1 application with minimal effort.
- stacktrace.js -- used to parse .map files and report source errors (currently using version 1.1.0)
- ng-lodash -- used for the omit feature below (currently using verison 0.5.0)
$log
messages are batch posted based on buffering- Intercepts errors and reports as level:error messages
This module is heavily stolen from various online blogs/examples, none of which I rememberd to include in my code. The core of my enhancement is the logzio.factory.js file, which is the magic that posts the data to logz.io. This factory may be lifted and inserted into your logging system to support logz.io.
The following is a real screenshot that was generated by an error in the application.
Enable the factory as follows:
logzioFactory.setConfig({
api_key: '**YOUR_LOGZIO_TOKEN**',
enable_xhr: true,
enable_exception: true,
log_level: 'info',
source_prefix: '../../../client/app/',
});
The above configuration:
- Publishes all
$log.info()
messages to logz.in - All exceptions and xhr errors are reported
- script.map file references our source code at "../../../client/app" (this is build-system specific, so your mileage may vary)
The default configuration variable is:
api_key: null,
logzio_type: "angular",
exception_logger_config: {
windowInSeconds: 5,
maxExceptionsPerWindow: 4, //max # of exceptions we log in the window interval
},
omit: [
'config.data.password'
],
enable_xhr: false,
enable_exception: false,
log_logger_config: {
global: false,
warn: true,
error: true,
info: true,
log: false,
debug: false
},
baseMeta: {},
delay_timing: 5000,
source_prefix: '',
Notes:
- windowInSeconds: Throttles errors to 5s. If multiple errors are generated within a 5s window, only the first 4 are reported (this prevents flooding logz.io).
- delay_timing: This buffers messages for 5s and then posts to logz.io
- omit: If you
To disable locally:
logzioFactory.setConfig({
log_level: 'off'
});
You may have some metadata information for your application (a JSON object) that you wish to include in each log message (for searching). Configure this meta information as follows (in your initialization routine):
var meta = {
role: 'angular'
};
if ($rootScope.currentUser) {
meta.username = $rootScope.currentUser.username;
meta.env = $rootScope.currentUser.node_env;
}
logzioFactory.setConfig({
baseMeta: meta
});