Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
angular-log4angular.js
(function (window, angular, undefined) {
'use strict';
var log4AngularModule = angular.module('bp.log4Angular', []).
provider('$log4Angular', $log4AngularProvider);
log4AngularModule.factory('consoleAppender', ['$log', function ($log) {
// Define the constructor function
function Appender($log) {
this.console = $log;
}
// Define the "instance" methods using the prototype and standard prototypical inheritance
Appender.prototype = {
log: function (message) { this.console.log(message); },
warn: function (message) { this.console.warn(message); },
debug: function (message) { this.console.debug(message); },
info: function (message) { this.console.info(message); },
error: function (message) { this.console.error(message); }
};
// Return the constructor - this is what defines the actual injectable in the DI framework
return (Appender);
}]);
log4AngularModule.factory('eventsAppender', ['$http', function ($http) {
var events = [];
var maximumEvents;
function queue(message) {
events.push(message);
if (events.length >= maximumEvents) {
var currentEvents = events.length;
$http.post('api/events', events).then(function (response) {
events.splice(0, currentEvents);
});
}
}
function Appender(options) {
maximumEvents = options && options.maximumEvents ? options.maximumEvents : 5;
}
Appender.prototype = {
log: function(message) { queue(message); },
warn: function (message) { queue(message); },
debug: function (message) { queue(message); },
info: function (message) { queue(message); },
error: function (message) { queue(message); }
};
return (Appender);
}]);
log4AngularModule.factory('appenderFactory', ['$injector', function($injector) {
var appenders = [];
return {
create: function (name, options) {
for (var i = 0; i < appenders.length; i++) {
if (appenders[i].name == name)
return appenders[i].instance;
}
var ctor = $injector.get(name);
var appender = $injector.instantiate(ctor, { options: options });
appenders.push({ name: name, instance: appender });
return appender;
}
};
}]);
log4AngularModule.factory('loggerFactory', ['appenderFactory', function (appenderFactory) {
function Logger(name, appenders, configuration) {
var self = this;
this.name = name;
this.appenders = [];
angular.forEach(appenders, function (item, index) {
var options = undefined;
for (var i = 0; i < configuration.length; i++) {
if (configuration[i].name == item) {
options = configuration[i].options;
break;
}
}
var appender = appenderFactory.create(item, options);
self.appenders.push(appender);
});
}
Logger.prototype = {
log: function(message) {
angular.forEach(this.appenders, function(appender, index) {
appender.log(message);
});
},
warn: function (message) {
angular.forEach(this.appenders, function (appender, index) {
appender.warn(message);
});
},
debug: function (message) {
angular.forEach(this.appenders, function (appender, index) {
appender.debug(message);
});
},
info: function (message) {
angular.forEach(this.appenders, function (appender, index) {
appender.info(message);
});
},
error: function (message) {
angular.forEach(this.appenders, function (appender, index) {
appender.error(message);
});
},
};
return {
create: function (logger, appenders, configuration) {
return new Logger(logger, appenders, configuration);
}
};
}]);
function $log4AngularProvider() {
var _instances = [];
var _loggers = [];
this.loggers = _loggers;
var _configuration = [];
this.appenders = {
configure: function (name, options) {
_configuration.push({ name: name, options: options });
}
};
this.$get = ['loggerFactory', function (loggerFactory) {
function getLoggerDefinition(name) {
var definition = undefined;
for (var i = 0; i < _loggers.length; i++) {
if (_loggers[i].name() == name) {
definition = _loggers[i];
break;
}
}
return definition;
}
return {
getLogger: function (name) {
for (var i = 0; i < _instances.length; i++) {
if (_instances[i].name == name) {
return _instances[i];
}
}
var definition = getLoggerDefinition(name);
var appenders = definition ? definition.appenders() : [];
var logger = loggerFactory.create(name, appenders, _configuration);
_instances.push(logger);
return logger;
}
};
}];
return this;
}
})(window, window.angular);
var AppModule = angular.module('App', ['bp.log4Angular'], [function () { }]);
AppModule.config(['$log4AngularProvider', function($log4AngularProvider) {
$log4AngularProvider.appenders.configure('eventsAppender', { maximumEvents: 5 });
$log4AngularProvider.loggers.push({
name: function () { return 'events'; },
appenders: function() {
return ['consoleAppender', 'eventsAppender'];
}
});
}]);
AppModule.controller('MainCtrl', ['$scope', '$log4Angular', function($scope, $log4Angular) {
var logger = $log4Angular.getLogger('events');
logger.log('{ id: 101, customerId: 202, message: "This is a log message."}');
logger.warn('{ id: 101, customerId: 202, message: "This is a warning."}');
logger.info('{ id: 101, customerId: 202, message: "This is an info message."}');
logger.error('{ id: 101, customerId: 202, message: "This is an error message."}');
logger.debug('{ id: 101, customerId: 202, message: "This is a debug message."}');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment