A customizable angular logging service that a nice time stamp, visually aligns messages, has convenient logger name differentiation and provides support for localizing messages and it can post messages in batches to a server API.
This module uses lodash Lo-Dash and that is the only required dependency. The module also uses underscore.string but, you can provide your own string formatting function and not load that helper library.
bower install angular-ai-logger
git clone https://github.com/AquaticInformatics/angular-logger.git
npm install
bower install
gulp build
angular.module('someModule', ['ai.public.logger'])
.controller(function($scope, AiLogger) {
var logger = AiLogger.getLogger('someModule');
logger.info('This is an %s message', 'info');
logger.debug('This one is %s message %d', 'debug', 2);
});
Since this is a provider you can configure it once for the whole application:
angular.module('someModule', ['ai.public.logger'])
.config(function(AiLoggerProvider) {
AiLoggerProvider.setAppName('aiLoggerDemoApp');
AiLoggerProvider.setLogLevel('trace');
})
.controller(function($scope, AiLogger) {
var logger = AiLogger.getLogger('someModule');
logger.info('This is an %s message', 'info');
logger.debug('This one is %s message %d', 'debug', 2);
});
Simply install angular-loggly-logger and configure it. That is it.
Follow these instructions
Configures the top level logger name. If you don't provide one 'unknownApp' will be used.
The built-in translator just returns the given value, if you want translations you have to specify a function that will do that. The built-in translator is applied to every parameter passed to logging functions.
aiLogger uses underscore.string.sprintf to compose the logged message as well as to substitute any place holders in messages when there are many parameters.
aiLogger.info('this is a %s with a %d parameters', 'test', 3);
If you don't want to have underscore.string dependency or want to use an existing string formatter you can change it.
Sets the minimum log level that will be logged, the rest will be ignored. Error has log level 0, warning 1, info 2, debug 3 and trace 4. It is handy to set this level to value from a cookie or a url parameter.
The default time stamp format is yyyy-mm-dd HH:MM:ss.SSS (UTC). You can provide your own function that generates the format you like.
This pattern determines how the entire message is composed. The default pattern is: '%1$s %2$-7s %3$s - %4$s'. The first parameter is a time stamp, the second is log level (aligned within 7 spaces), third is logger name and 4th is composed message. If you want to switch them around you can change the %X value or you can exclude some information. For example, '%4s' will only log composed message. For more information on sprintf checkout javascript-sprintf
Returns another instance of a logger with a new name (new name is formed by appending given name to existing name).
Each instance of a logger can be configured independently at which minimum level the messages will be logged.
Logs the formatted message to output writer
Logs the formatted message to output writer if log level is warn or greater
Logs the formatted message to output writer if log level is info or greater
Logs the formatted message to output writer if log level is debug or greater
Logs the formatted message to output writer if log level is trace