jQuery.Logger
A simple but powerful logging plugin for jQuery with namespaces.
Examples
Create a (global) logger and log something:
var logger = $.Logger();
logger.debug("my first log");
logger.disable();
logger.debug("another log");
logger.enable();
logger.level("info");
logger.debug("foo bar");
logger.info("but this time");
logger.name();
logger.namespace();
Create a child logger:
var myLogger = $.Logger("foo");
myLogger.info("foo's first log");
myLogger.parent() == logger;
Configuration
$.Logger({
global: "global",
delimitter: ".",
useConsoleMethods: true,
showNamespace: true,
showTimestamp: true,
showOrigin: true,
cutQuery: true
});
API
-
$.Logger([namespace|options])
If options
are passed, the logger options are extended and the $.Logger
object is returned. If a namespace
is passed, a new logger instance with that namespace is created and returned. When no argument is given, the global logger is returned. Logger parentage is built automatically. A namespace consists of a number of logger names seperated by a delimitter. Example: namespace "foo.bar"
=> logger "global"
-> logger "foo"
-> logger "bar"
. Note that the global namespace ("global"
in this example) is always prepended.
-
namespace()
Returns the namespace
.
-
name()
Returns the name
.
-
parent()
Returns the parent logger, or null
when invoked on the global logger.
-
children()
Return all child loggers mapped to their names.
-
child(name)
Return a child logger given by name
.
-
enabled()
Returns the state of the logger.
-
enable()
Enable the logger and all its child loggers.
-
disable()
Disable this and all its child loggers.
-
level([level])
When level
is given, the log level is set to that value. Possible values: "all"
, "debug"
, "info"
, "warning"
, "error"
, "fatal"
. When no argument is passed, the current log level is returned.
-
levels()
Returns all valid log levels mapped to their numerical representation.
-
options()
Returns the current options.
-
log(level, ...)
Log arguments
with a given level
.
-
all | debug | info | warning | error | fatal(...)
Shorthands that wrap around log
for specific levels.
-
originOffset([offset])
, experimental
The context that invoked a log (the origin) is determined by parsing the stack trace of a newly created error instance. The first few lines in that stack describe the internal invocation queue of this plugin. In the current implementation, the 5th line is the one that caused the actual log. However, in some cases (e.g. when logging a deprecation warning) it is useful to see the origin of an earlier invocation in the queue. This can be achieved by setting originOffset
to a numerical value that is added to the stack line index (5th line -> 4
) for the next log. After that, the offset is reset to 0
again.
When offset
is given, the origin offset is set to that value. When no argument is passed, the current origin offset is returned.
Development
License
MIT.
Authors
Marcel R. (riga)