Skip to content

Commit

Permalink
Add an alias log for debug, for easier migration from console.log.
Browse files Browse the repository at this point in the history
Connects-To: #64
  • Loading branch information
pimterry committed Sep 11, 2017
1 parent d029710 commit 25ef8ed
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ The loglevel API is extremely minimal. All methods are available on the root log
* `log.warn(msg)`
* `log.error(msg)`

`log.log(msg)` is also available, as an alias for `log.debug(msg)`, to improve compatibility with `console`, and make migration easier.

Exact output formatting of these will depend on the console available in the current context of your application. For example, many environments will include a full stack trace with all trace() calls, and icons or similar to highlight other calls.

These methods should never fail in any environment, even if no console object is currently available, and should always fall back to an available log method even if the specific method called (e.g. warn) isn't available.
Expand Down
3 changes: 3 additions & 0 deletions lib/loglevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
noop :
this.methodFactory(methodName, level, loggerName);
}

// Define log.log as an alias for log.debug
this.log = this.debug;
}

// In old IE versions, the console isn't present until you first open it.
Expand Down
2 changes: 2 additions & 0 deletions test/integration-smoke-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ define(['../lib/loglevel', 'test/test-helpers'], function(log, testHelpers) {
log.setLevel(log.levels.SILENT);
log.trace("trace");
log.debug("debug");
log.log("log");
log.info("info");
log.warn("warn");
log.error("error");
Expand All @@ -24,6 +25,7 @@ define(['../lib/loglevel', 'test/test-helpers'], function(log, testHelpers) {

log.trace("trace");
log.debug("debug");
log.log("log");
log.info("info");
log.warn("warn");
log.error("error");
Expand Down
30 changes: 30 additions & 0 deletions test/level-setting-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,36 @@ define(['../lib/loglevel'], function(log) {
});
});

describe("log.log", function() {
it("is enabled at trace level", function() {
log.setLevel(log.levels.TRACE);

log.log("a log message");
expect(console.log).toHaveBeenCalled();
});

it("is enabled at debug level", function() {
log.setLevel(log.levels.DEBUG);

log.log("a log message");
expect(console.log).toHaveBeenCalled();
});

it("is disabled at info level", function() {
log.setLevel(log.levels.INFO);

log.log("a log message");
expect(console.log).not.toHaveBeenCalled();
});

it("is disabled at silent level", function() {
log.setLevel(log.levels.SILENT);

log.log("a log message");
expect(console.log).not.toHaveBeenCalled();
});
});

describe("log.info", function() {
it("is enabled at debug level", function() {
log.setLevel(log.levels.DEBUG);
Expand Down

0 comments on commit 25ef8ed

Please sign in to comment.