Skip to content

Commit

Permalink
0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
eclifford committed Sep 25, 2014
1 parent cb8535a commit 271b1b9
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 25 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,19 @@ calls several other methods on the `dog` object.
#### Dynamic logging levels

By default all **Logr** logging instances are created with a logging level of `DEBUG` meaning all
logging messages will reach your browsers console.
logging messages will reach your browsers console. You can override this by passing a level in the options
during log instance creation.

```js
var log = Logr.log('foo', { level: Logr.levels.ERROR }); // set the logger level to error and above
```

**Note:** all object logging is automatically set the debug level.

##### Setting logging level globally

Calling `Logr.setLevel` will apply the supplied level to each stored logging instance.
Calling `Logr.setLevel` will set the session based logging level for all stored instances. You should only call this from
your browser to overwrite the initial level state.

```js
Logr.setLevel(Logr.levels.NONE); // no messages get through
Expand All @@ -159,6 +165,8 @@ Calling `Logr.setLevel` will apply the supplied level to each stored logging ins

##### Setting logging level per instance

To set the session based logging level for a single instance simply call `setLevel` on that instance.

```js
var log1 = Logr.log('log1');

Expand All @@ -177,17 +185,17 @@ where you want to turn on and off logging levels in QA/Integration environment o
your debugging logs down while working.

You can for example set your all of your logs to `ERROR` by default and lower their levels
in real time to avoid log overload.
from your browser to avoid log overload.

**Example:**

Say that I create three logs foo, baz, and bar. By default I set all their
levels to `ERROR` only.

```js
var foo = Logr.log('foo', Logr.levels.ERROR);
var baz = Logr.log('baz', Logr.levels.ERROR);
var bar = Logr.log('bar', Logr.levels.ERROR);
var foo = Logr.log('foo', { level: Logr.levels.ERROR });
var baz = Logr.log('baz', { level: Logr.levels.ERROR });
var bar = Logr.log('bar', { level: Logr.levels.ERROR });

foo.attach(dog); // not seen as objects are always on DEBUG level
foo.warn('foo is getting angry'); // not seen
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logr.js",
"version": "0.0.2",
"version": "0.1.0",
"main": "logr.js",
"description": "Logr is a JavaScript console logging replacement with support for dynamic object logging injection",
"homepage": "https://github.com/eclifford/logr",
Expand Down
8 changes: 7 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
}
};

var dogLog = Logr.log('dog');
var dogLog = Logr.log('dog', {
level: Logr.levels.DEBUG
});
dogLog.attach(dog);

dog.bark();
Expand All @@ -32,6 +34,10 @@

dog.eat('rocket', 'chicken');

dogLog.info('info test');
dogLog.warn('warn test');
dogLog.error('error test');

</script>
</body>
</html>
46 changes: 31 additions & 15 deletions logr.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'use strict';

var Logr = {
version: '0.0.2',
version: '0.1.0',

logs: {},

Expand All @@ -30,11 +30,11 @@
},

levels: {
NONE: 0,
DEBUG: 1,
INFO: 2,
WARN: 3,
ERROR: 4
ERROR: 4,
NONE: 9
},

// set the level for all stored logs
Expand All @@ -54,30 +54,29 @@
//
// @param [String] logname - the name of the log to get or create
//
log: function(logname) {
log: function(logname, options) {
if(!logname) {
throw Error("Logr.log: provide the name of the log to create or get");
}
if(Logr.logs[logname]) {
return Logr.logs[logname];
} else {
var l = new Log(logname);
Logr.logs[logname] = l;
return l;
if(!Logr.logs[logname]) {
Logr.logs[logname] = new Log(logname, options);
}
return Logr.logs[logname];
}
};

// constructor for log object
//
// @param [String] logname - the name of the log to create
//
var Log = function(logname, level) {
var Log = function(logname, options) {
if(!logname) {
throw new Error("Logr.Log: logname is required");
}
this.logname = logname;
this.setLevel(level || Logr.defaults.level);
if(options) {
extend(this, Logr.defaults, options);
}
};

// wrap console.debug
Expand Down Expand Up @@ -108,18 +107,17 @@
}
};

// sets the level for the log
// set the session level for the log instance
//
// @param [Number] level - the level to set the log to
//
Log.prototype.setLevel = function(level) {
if(sessionStorage) {
sessionStorage.setItem("logr:" + this.logname + ":level", level);
}
this.level = level;
};

// gets the level for the log
// get the session level or initial level for the log instance
//
Log.prototype.getLevel = function() {
if(sessionStorage) {
Expand Down Expand Up @@ -164,5 +162,23 @@
}
};

// Simple extend
//
// @param [Object] target - the target object to extend
// @param [Array] source - an array of object to extend the target with
//
function extend(obj) {
var source, prop;
for (var i = 1, length = arguments.length; i < length; i++) {
source = arguments[i];
for (prop in source) {
if (hasOwnProperty.call(source, prop)) {
obj[prop] = source[prop];
}
}
}
return obj;
}

return Logr;
}));
2 changes: 1 addition & 1 deletion logr.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logr.js",
"version": "0.0.2",
"version": "0.1.0",
"description": "Logr is a JavaScript console logging replacement with support for dynamic object logging injection",
"author": "Eric Clifford <[email protected]>",
"main": "Gruntfile.js",
Expand Down
Binary file removed test/.DS_Store
Binary file not shown.
10 changes: 10 additions & 0 deletions test/unit/logrSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,15 @@ describe("logr", function() {
expect(spy).to.have.been.called;
});
});
describe("Log()", function() {
it("should be able to extend options", function() {
Logr.log('log4', {
level: 2
});
expect(Logr.log('log4').level).to.equal(2);
delete Logr.logs.log4;
});
});

});
});

0 comments on commit 271b1b9

Please sign in to comment.