Skip to content

Instantly share code, notes, and snippets.

@lrvick
Last active February 3, 2021 04:26
Show Gist options
  • Save lrvick/6938531 to your computer and use it in GitHub Desktop.
Save lrvick/6938531 to your computer and use it in GitHub Desktop.
AngularJS - Global logging override module. Enables you to write custom hooks to intercept, display, manipulate or re-transfer $log logs as you see fit. Also allows one to easily globally disable logs application wide, useful for production.
/**
* # Global logging module
*
* This is a global set of hooks that catch all $log messages sent out by the
* application. Currently they are simply passed off directly to console.log
* but this could be updated later to allow them to be stored locally, sent to
* a server etc.
*/
angular.module('ngLogging', [])
angular.module('ngLogging').config
( function($provide) {
$provide.decorator
( '$log'
, function
( $delegate
, Logging
) {
var methods =
{ error:
function(){
if (Logging.enabled){
$delegate.error.apply(null,arguments)
Logging.error.apply(null,arguments)
}
}
, log:
function(){
if (Logging.enabled){
$delegate.log.apply(null,arguments)
Logging.log.apply(null,arguments)
}
}
, info:
function(){
if (Logging.enabled){
$delegate.info.apply(null,arguments)
Logging.info.apply(null,arguments)
}
}
, warn:
function(){
if (Logging.enabled){
$delegate.warn.apply(null,arguments)
Logging.warn.apply(null,arguments)
}
}
}
return methods
}
)
})
angular.module('ngLogging').service
( 'Logging'
, function
( $injector
) {
var service =
{ error:
function() {
self.type = 'error'
log.apply(self,arguments)
}
, warn:
function() {
self.type = 'warn'
log.apply(self,arguments)
}
, info:
function() {
self.type = 'info'
log.apply(self,arguments)
}
, log:
function() {
self.type = 'log'
log.apply(self,arguments)
}
, enabled : false
, logs : []
}
var log = function(){
args = []
angular.forEach(arguments, function(arg) {
if (typeof arg === 'object' ){
arg = JSON.stringify(arg)
}
args.push(arg);
})
var dd = new Date();
var hh = dd.getHours();
var mm = dd.getMinutes();
var ss = dd.getSeconds();
var ms = dd.getMilliseconds();
var logItem =
{ time : hh+":"+mm+":"+ss+":"+ms
, message: args.join('\n')
, type : type
}
service.logs.push(logItem)
var _$rootScope = $injector.get('$rootScope')
var _$timeout = $injector.get('$timeout')
_$timeout
( function(){
_$rootScope.$broadcast('log',logItem)
}
, 0
)
}
return service
})
@pinnprophead
Copy link

This looks cool. I'm curious about the logs array. Wouldn't this get big over time? What's it for?

@lrvick
Copy link
Author

lrvick commented Sep 29, 2014

In my case I just used it as a queue, and had hooks remove them from the array as they were sent off to my server.

@martinvalasek
Copy link

Thanks! In my case there was an error when this code got minified, the syntax should rather be:

angular.module('ngLogging')
  .config(['$provide', function($provide) {
    $provide.decorator('$log', ['$delegate', 'Logging', function($delegate, Logging) {

See http://solutionoptimist.com/2013/10/07/enhance-angularjs-logging-using-decorators/ - "Using $provider.decorator()".

@TehreemAnsari
Copy link

Is this for getting the array of logs based on levels? I'm sorry, but what should I do to get logs based on levels? like in mock, $log.warn.logs, the only problem is, I cant use mock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment