[英]How can I enhance Angular's $log with a timestamp and class name?
How can I make it so that Angular adds a timestamp and classname to its logs? 我怎样才能使Angular为其日志添加时间戳和类名?
Something like this: 像这样的东西:
$log.info('this log entry came from FooBar');
" 9:37:18 pm, FooBar: this log entry came from FooBar " “ 9:37:18 pm,FooBar:这个日志条目来自FooBar ”
Examples I found around the web are either not clear or combining many other things (like requirejs). 我在网络上发现的例子要么不清楚,要么结合许多其他事情(如requirejs)。 I did find working some examples which go into Angular decorators, but I'm was wondering if there isn't a simpler way.
我确实找到了一些进入Angular装饰器的例子,但我想知道是否有更简单的方法。
16-05-2015 : this code was turned into a GitHub project called angular-logger . 16-05-2015 :此代码变成了一个名为angular-logger的GitHub项目。 The code shown below is rather outdated.
下面显示的代码已经过时了。
You don't have to use decorators. 您不必使用装饰。 You can just trick Angular's $log with some basic javascript:
你可以用一些基本的javascript来欺骗Angular的$ log:
app.run(['$log', function($log) {
$log.getInstance = function(context) {
return {
log : enhanceLogging($log.log, context),
info : enhanceLogging($log.info, context),
warn : enhanceLogging($log.warn, context),
debug : enhanceLogging($log.debug, context),
error : enhanceLogging($log.error, context)
};
};
function enhanceLogging(loggingFunc, context) {
return function() {
var modifiedArguments = [].slice.call(arguments);
modifiedArguments[0] = [moment().format("dddd h:mm:ss a") + '::[' + context + ']> '] + modifiedArguments[0];
loggingFunc.apply(null, modifiedArguments);
};
}
}]);
Usage: 用法:
var logger = $log.getInstance('Awesome');
logger.info("This is awesome!");
Output: 输出:
Monday 9:37:18 pm::[Awesome]> This is awesome! 星期一下午9:37:18 :: [真棒]>真棒!
I used Moment.js for timestamp formatting. 我使用Moment.js进行时间戳格式化。 This example uses Angular's module run block support to configure the application before anything else starts running.
此示例使用Angular的模块运行块支持在其他任何开始运行之前配置应用程序。
For a more elegant and configurable solution, here's the same log enhancer, but as a configurable provider: 对于更优雅和可配置的解决方案,这里是相同的日志增强器,但作为可配置的提供程序:
angular.module('app').provider('logEnhancer', function() {
this.loggingPattern = '%s - %s: ';
this.$get = function() {
var loggingPattern = this.loggingPattern;
return {
enhanceAngularLog : function($log) {
$log.getInstance = function(context) {
return {
log : enhanceLogging($log.log, context, loggingPattern),
info : enhanceLogging($log.info, context, loggingPattern),
warn : enhanceLogging($log.warn, context, loggingPattern),
debug : enhanceLogging($log.debug, context, loggingPattern),
error : enhanceLogging($log.error, context, loggingPattern)
};
};
function enhanceLogging(loggingFunc, context, loggingPattern) {
return function() {
var modifiedArguments = [].slice.call(arguments);
modifiedArguments[0] = [ sprintf(loggingPattern, moment().format("dddd h:mm:ss a"), context) ] + modifiedArguments[0];
loggingFunc.apply(null, modifiedArguments);
};
}
}
};
};
});
To use and configure it: 要使用和配置它:
var app = angular.module('app', []);
app.config(['logEnhancerProvider', function(logEnhancerProvider) {
logEnhancerProvider.loggingPattern = '%s::[%s]> ';
}]);
app.run(['$log', 'logEnhancer', function($log, logEnhancer) {
logEnhancer.enhanceAngularLog($log);
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.