mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-27 23:14:00 +00:00
feat($log): add $log.debug()
New debug() method with suppressable output via $logProvider.debugEnabled() Closes #1592
This commit is contained in:
parent
93070f1488
commit
9e991ddb1d
2 changed files with 89 additions and 8 deletions
|
|
@ -33,7 +33,33 @@
|
||||||
</example>
|
</example>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc object
|
||||||
|
* @name ng.$logProvider
|
||||||
|
* @description
|
||||||
|
* Use the `$logProvider` to configure how the application logs messages
|
||||||
|
*/
|
||||||
function $LogProvider(){
|
function $LogProvider(){
|
||||||
|
var debug = true,
|
||||||
|
self = this;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc property
|
||||||
|
* @name ng.$logProvider#debugEnabled
|
||||||
|
* @methodOf ng.$logProvider
|
||||||
|
* @description
|
||||||
|
* @param {string=} flag enable or disable debug level messages
|
||||||
|
* @returns {*} current value if used as getter or itself (chaining) if used as setter
|
||||||
|
*/
|
||||||
|
this.debugEnabled = function(flag) {
|
||||||
|
if (isDefined(flag)) {
|
||||||
|
debug = flag;
|
||||||
|
return this;
|
||||||
|
} else {
|
||||||
|
return debug;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
this.$get = ['$window', function($window){
|
this.$get = ['$window', function($window){
|
||||||
return {
|
return {
|
||||||
/**
|
/**
|
||||||
|
|
@ -74,7 +100,25 @@ function $LogProvider(){
|
||||||
* @description
|
* @description
|
||||||
* Write an error message
|
* Write an error message
|
||||||
*/
|
*/
|
||||||
error: consoleLog('error')
|
error: consoleLog('error'),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc method
|
||||||
|
* @name ng.$log#debug
|
||||||
|
* @methodOf ng.$log
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* Write a debug message
|
||||||
|
*/
|
||||||
|
debug: (function () {
|
||||||
|
var fn = consoleLog('debug');
|
||||||
|
|
||||||
|
return function() {
|
||||||
|
if (debug) {
|
||||||
|
fn.apply(self, arguments);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}())
|
||||||
};
|
};
|
||||||
|
|
||||||
function formatError(arg) {
|
function formatError(arg) {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,13 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
function initService(debugEnabled) {
|
||||||
|
return module(function($logProvider){
|
||||||
|
$logProvider.debugEnabled(debugEnabled);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
describe('$log', function() {
|
describe('$log', function() {
|
||||||
var $window, logger, log, warn, info, error;
|
var $window, logger, log, warn, info, error, debug;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -12,6 +18,7 @@ describe('$log', function() {
|
||||||
warn = function() { logger+= 'warn;'; };
|
warn = function() { logger+= 'warn;'; };
|
||||||
info = function() { logger+= 'info;'; };
|
info = function() { logger+= 'info;'; };
|
||||||
error = function() { logger+= 'error;'; };
|
error = function() { logger+= 'error;'; };
|
||||||
|
debug = function() { logger+= 'debug;'; };
|
||||||
|
|
||||||
$provide.provider('$log', $LogProvider);
|
$provide.provider('$log', $LogProvider);
|
||||||
$provide.value('$exceptionHandler', angular.mock.rethrow);
|
$provide.value('$exceptionHandler', angular.mock.rethrow);
|
||||||
|
|
@ -23,14 +30,16 @@ describe('$log', function() {
|
||||||
$window.console = {log: log,
|
$window.console = {log: log,
|
||||||
warn: warn,
|
warn: warn,
|
||||||
info: info,
|
info: info,
|
||||||
error: error};
|
error: error,
|
||||||
|
debug: debug};
|
||||||
},
|
},
|
||||||
function($log) {
|
function($log) {
|
||||||
$log.log();
|
$log.log();
|
||||||
$log.warn();
|
$log.warn();
|
||||||
$log.info();
|
$log.info();
|
||||||
$log.error();
|
$log.error();
|
||||||
expect(logger).toEqual('log;warn;info;error;');
|
$log.debug();
|
||||||
|
expect(logger).toEqual('log;warn;info;error;debug;');
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
@ -44,7 +53,8 @@ describe('$log', function() {
|
||||||
$log.warn();
|
$log.warn();
|
||||||
$log.info();
|
$log.info();
|
||||||
$log.error();
|
$log.error();
|
||||||
expect(logger).toEqual('log;log;log;log;');
|
$log.debug();
|
||||||
|
expect(logger).toEqual('log;log;log;log;log;');
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
@ -55,6 +65,7 @@ describe('$log', function() {
|
||||||
$log.warn();
|
$log.warn();
|
||||||
$log.info();
|
$log.info();
|
||||||
$log.error();
|
$log.error();
|
||||||
|
$log.debug();
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
@ -64,22 +75,48 @@ describe('$log', function() {
|
||||||
log.apply = log.call =
|
log.apply = log.call =
|
||||||
warn.apply = warn.call =
|
warn.apply = warn.call =
|
||||||
info.apply = info.call =
|
info.apply = info.call =
|
||||||
error.apply = error.call = null;
|
error.apply = error.call =
|
||||||
|
debug.apply = debug.call = null;
|
||||||
|
|
||||||
$window.console = {log: log,
|
$window.console = {log: log,
|
||||||
warn: warn,
|
warn: warn,
|
||||||
info: info,
|
info: info,
|
||||||
error: error};
|
error: error,
|
||||||
|
debug: debug};
|
||||||
},
|
},
|
||||||
function($log) {
|
function($log) {
|
||||||
$log.log.apply($log);
|
$log.log.apply($log);
|
||||||
$log.warn.apply($log);
|
$log.warn.apply($log);
|
||||||
$log.info.apply($log);
|
$log.info.apply($log);
|
||||||
$log.error.apply($log);
|
$log.error.apply($log);
|
||||||
expect(logger).toEqual('log;warn;info;error;');
|
$log.debug.apply($log);
|
||||||
|
expect(logger).toEqual('log;warn;info;error;debug;');
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
describe("$log.debug", function () {
|
||||||
|
|
||||||
|
beforeEach(initService(false));
|
||||||
|
|
||||||
|
it("should skip debugging output if disabled", inject(
|
||||||
|
function(){
|
||||||
|
$window.console = {log: log,
|
||||||
|
warn: warn,
|
||||||
|
info: info,
|
||||||
|
error: error,
|
||||||
|
debug: debug};
|
||||||
|
},
|
||||||
|
function($log) {
|
||||||
|
$log.log();
|
||||||
|
$log.warn();
|
||||||
|
$log.info();
|
||||||
|
$log.error();
|
||||||
|
$log.debug();
|
||||||
|
expect(logger).toEqual('log;warn;info;error;');
|
||||||
|
}
|
||||||
|
));
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('$log.error', function() {
|
describe('$log.error', function() {
|
||||||
var e, $log, errorArgs;
|
var e, $log, errorArgs;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue