Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 9e991dd

Browse files
nlaplantevojtajina
authored andcommitted
feat($log): add $log.debug()
New debug() method with suppressable output via $logProvider.debugEnabled() Closes #1592
1 parent 93070f1 commit 9e991dd

File tree

2 files changed

+89
-8
lines changed

2 files changed

+89
-8
lines changed

src/ng/log.js

+45-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,33 @@
3333
</example>
3434
*/
3535

36+
/**
37+
* @ngdoc object
38+
* @name ng.$logProvider
39+
* @description
40+
* Use the `$logProvider` to configure how the application logs messages
41+
*/
3642
function $LogProvider(){
43+
var debug = true,
44+
self = this;
45+
46+
/**
47+
* @ngdoc property
48+
* @name ng.$logProvider#debugEnabled
49+
* @methodOf ng.$logProvider
50+
* @description
51+
* @param {string=} flag enable or disable debug level messages
52+
* @returns {*} current value if used as getter or itself (chaining) if used as setter
53+
*/
54+
this.debugEnabled = function(flag) {
55+
if (isDefined(flag)) {
56+
debug = flag;
57+
return this;
58+
} else {
59+
return debug;
60+
}
61+
};
62+
3763
this.$get = ['$window', function($window){
3864
return {
3965
/**
@@ -74,7 +100,25 @@ function $LogProvider(){
74100
* @description
75101
* Write an error message
76102
*/
77-
error: consoleLog('error')
103+
error: consoleLog('error'),
104+
105+
/**
106+
* @ngdoc method
107+
* @name ng.$log#debug
108+
* @methodOf ng.$log
109+
*
110+
* @description
111+
* Write a debug message
112+
*/
113+
debug: (function () {
114+
var fn = consoleLog('debug');
115+
116+
return function() {
117+
if (debug) {
118+
fn.apply(self, arguments);
119+
}
120+
}
121+
}())
78122
};
79123

80124
function formatError(arg) {

test/ng/logSpec.js

+44-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
'use strict';
22

3+
function initService(debugEnabled) {
4+
return module(function($logProvider){
5+
$logProvider.debugEnabled(debugEnabled);
6+
});
7+
}
8+
39
describe('$log', function() {
4-
var $window, logger, log, warn, info, error;
10+
var $window, logger, log, warn, info, error, debug;
511

612

713

@@ -12,6 +18,7 @@ describe('$log', function() {
1218
warn = function() { logger+= 'warn;'; };
1319
info = function() { logger+= 'info;'; };
1420
error = function() { logger+= 'error;'; };
21+
debug = function() { logger+= 'debug;'; };
1522

1623
$provide.provider('$log', $LogProvider);
1724
$provide.value('$exceptionHandler', angular.mock.rethrow);
@@ -23,14 +30,16 @@ describe('$log', function() {
2330
$window.console = {log: log,
2431
warn: warn,
2532
info: info,
26-
error: error};
33+
error: error,
34+
debug: debug};
2735
},
2836
function($log) {
2937
$log.log();
3038
$log.warn();
3139
$log.info();
3240
$log.error();
33-
expect(logger).toEqual('log;warn;info;error;');
41+
$log.debug();
42+
expect(logger).toEqual('log;warn;info;error;debug;');
3443
}
3544
));
3645

@@ -44,7 +53,8 @@ describe('$log', function() {
4453
$log.warn();
4554
$log.info();
4655
$log.error();
47-
expect(logger).toEqual('log;log;log;log;');
56+
$log.debug();
57+
expect(logger).toEqual('log;log;log;log;log;');
4858
}
4959
));
5060

@@ -55,6 +65,7 @@ describe('$log', function() {
5565
$log.warn();
5666
$log.info();
5767
$log.error();
68+
$log.debug();
5869
}
5970
));
6071

@@ -64,22 +75,48 @@ describe('$log', function() {
6475
log.apply = log.call =
6576
warn.apply = warn.call =
6677
info.apply = info.call =
67-
error.apply = error.call = null;
78+
error.apply = error.call =
79+
debug.apply = debug.call = null;
6880

6981
$window.console = {log: log,
7082
warn: warn,
7183
info: info,
72-
error: error};
84+
error: error,
85+
debug: debug};
7386
},
7487
function($log) {
7588
$log.log.apply($log);
7689
$log.warn.apply($log);
7790
$log.info.apply($log);
7891
$log.error.apply($log);
79-
expect(logger).toEqual('log;warn;info;error;');
92+
$log.debug.apply($log);
93+
expect(logger).toEqual('log;warn;info;error;debug;');
8094
})
8195
);
8296

97+
describe("$log.debug", function () {
98+
99+
beforeEach(initService(false));
100+
101+
it("should skip debugging output if disabled", inject(
102+
function(){
103+
$window.console = {log: log,
104+
warn: warn,
105+
info: info,
106+
error: error,
107+
debug: debug};
108+
},
109+
function($log) {
110+
$log.log();
111+
$log.warn();
112+
$log.info();
113+
$log.error();
114+
$log.debug();
115+
expect(logger).toEqual('log;warn;info;error;');
116+
}
117+
));
118+
119+
});
83120

84121
describe('$log.error', function() {
85122
var e, $log, errorArgs;

0 commit comments

Comments
 (0)