Last active
February 3, 2021 04:26
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* # 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 | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.