ag-logger
An AngularJS logger with configurable log levels. All logging messages are delegated to Angular's $log service.
Getting Started
We use bower for dependency management. Install ag-logger
into your project by running:
$ bower install ag-logger
After installing ag-logger
, load the script file in your application:
Then, add the ag-logger
module as a dependency to your application:
var appModule = angular;
Usage
Levels
There are five logging levels available:
ERROR > WARN > INFO > LOG > DEBUG
When a logger is set to a particular level, it will only log messages at that are logged at a level greater than or equal to the level it's configured at. For instance, if the logger is set to WARN
level, it will log ERROR
and WARN
messages. If the logger is set to ERROR
level, it will only log ERROR
messages.
Logging can be configured as a provider or a service (or a combination of both).
agLoggerProvider
Providerangular ;
agLogger
Serviceangular ; { ; { // sets the logger to WARNING level logging, within the application code agLogger; }}
Logging Messages
Messages can be logged via the agLogger service.
Note: By default, all logging levels are enabled.
angular ; { ; { // sets the logger to WARNING level logging, within the application code logger; logger; // will be displayed in the console. logger; // will be displayed in the console. logger; // will not be displayed in the console. logger; // will not be displayed in the console. logger; // will not be displayed in the console. }}
API Documentation
Note: By default, all logging levels are enabled.
agLoggerProvider
Provider -
property LOG_LEVELS -
[string]
Returns a dictionary of all supported logging levels.
{ var LOG_LEVELS = agLoggerProviderLOG_LEVELS; console; // off console; // error console; // warn console; // info console; // log console; // debug console; // all}
Note: LOG_LEVELS.OFF
and LOG_LEVELS.ALL
don't have corresponding log functions, they're just filter levels.
-
void
setLogLevel(logLevelName)Sets the log level.
{ // these two statements are functionally equivalent agLoggerProvider; agLoggerProvider;}
-
string
getLogLevel()Gets the current log level.
-
void
enableAll()Enables the logging of all messages.
{ // these three statements are functionally equivalent agLoggerProvider; agLoggerProvider; agLoggerProvider;}
-
void
disableAll()Disables the logging of all messages.
{ // these three statements are functionally equivalent agLoggerProvider; agLoggerProvider; agLoggerProvider;}
agLogger
Service -
property LOG_LEVELS -
[string]
Returns a dictionary of all supported logging levels.
{ var LOG_LEVELS = agLoggerLOG_LEVELS; console; // off console; // error console; // warn console; // info console; // log console; // debug console; // all }
-
void
setLogLevel(logLevelName)Sets the log level.
{ // these two statements are functionally equivalent agLogger; agLogger;}
-
string
getLogLevel()Gets the current log level.
-
void
enableAll()Enables the printing of all log levels.
{ // these three statements are functionally equivalent agLogger; agLogger; agLogger;}
-
void
disableAll()Disables the printing of all log levels.
{ // these three statements are functionally equivalent agLogger; agLogger; agLogger;}
-
boolean
willLog(logLevel)Returns a boolean representing whether the logger will log (print) messages at the provided level (depending on what the logger's current level is set at).
Logging functions
Messages will only be logged if they are logged at a level equal to or greater than the logger's configured level (see Usage).
-
void
error(message)Logs the provided message at the
error
level. -
void
warn(message)Logs the provided message at the
warn
level. -
void
info(message)Logs the provided message at the
info
level. -
void
log(message)Logs the provided message at the
log
level. -
void
debug(message)Logs the provided message at the
debug
level.