2011-07-17 08:05:43 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
2011-02-15 06:12:45 +00:00
|
|
|
/**
|
|
|
|
|
* @ngdoc service
|
|
|
|
|
* @name angular.service.$log
|
|
|
|
|
* @requires $window
|
|
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Simple service for logging. Default implementation writes the message
|
|
|
|
|
* into the browser's console (if present).
|
|
|
|
|
*
|
|
|
|
|
* The main purpose of this service is to simplify debugging and troubleshooting.
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
<doc:example>
|
|
|
|
|
<doc:source>
|
2011-09-29 01:20:31 +00:00
|
|
|
<script>
|
|
|
|
|
function LogCtrl($log) {
|
|
|
|
|
this.$log = $log;
|
2011-09-08 20:56:29 +00:00
|
|
|
this.message = 'Hello World!';
|
2011-09-29 01:20:31 +00:00
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
<div ng:controller="LogCtrl">
|
|
|
|
|
<p>Reload this page with open console, enter text and hit the log button...</p>
|
|
|
|
|
Message:
|
2011-09-08 20:56:29 +00:00
|
|
|
<input type="text" ng:model="message"/>
|
2011-09-29 01:20:31 +00:00
|
|
|
<button ng:click="$log.log(message)">log</button>
|
|
|
|
|
<button ng:click="$log.warn(message)">warn</button>
|
|
|
|
|
<button ng:click="$log.info(message)">info</button>
|
|
|
|
|
<button ng:click="$log.error(message)">error</button>
|
|
|
|
|
</div>
|
2011-02-15 06:12:45 +00:00
|
|
|
</doc:source>
|
|
|
|
|
<doc:scenario>
|
|
|
|
|
</doc:scenario>
|
|
|
|
|
</doc:example>
|
|
|
|
|
*/
|
|
|
|
|
var $logFactory; //reference to be used only in tests
|
|
|
|
|
angularServiceInject("$log", $logFactory = function($window){
|
|
|
|
|
return {
|
|
|
|
|
/**
|
|
|
|
|
* @ngdoc method
|
|
|
|
|
* @name angular.service.$log#log
|
|
|
|
|
* @methodOf angular.service.$log
|
|
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Write a log message
|
|
|
|
|
*/
|
|
|
|
|
log: consoleLog('log'),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @ngdoc method
|
|
|
|
|
* @name angular.service.$log#warn
|
|
|
|
|
* @methodOf angular.service.$log
|
|
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Write a warning message
|
|
|
|
|
*/
|
|
|
|
|
warn: consoleLog('warn'),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @ngdoc method
|
|
|
|
|
* @name angular.service.$log#info
|
|
|
|
|
* @methodOf angular.service.$log
|
|
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Write an information message
|
|
|
|
|
*/
|
|
|
|
|
info: consoleLog('info'),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @ngdoc method
|
|
|
|
|
* @name angular.service.$log#error
|
|
|
|
|
* @methodOf angular.service.$log
|
|
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
* Write an error message
|
|
|
|
|
*/
|
|
|
|
|
error: consoleLog('error')
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function consoleLog(type) {
|
|
|
|
|
var console = $window.console || {};
|
|
|
|
|
var logFn = console[type] || console.log || noop;
|
|
|
|
|
if (logFn.apply) {
|
2011-10-07 18:27:49 +00:00
|
|
|
return function() {
|
2011-02-15 06:12:45 +00:00
|
|
|
var args = [];
|
|
|
|
|
forEach(arguments, function(arg){
|
|
|
|
|
args.push(formatError(arg));
|
|
|
|
|
});
|
|
|
|
|
return logFn.apply(console, args);
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
// we are IE, in which case there is nothing we can do
|
|
|
|
|
return logFn;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-17 01:15:06 +00:00
|
|
|
}, ['$window']);
|