Skip to content

Commit

Permalink
0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
eclifford committed Sep 23, 2014
1 parent 617f817 commit 8d34155
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bower_components
node_modules
test/coverage
.DS_Store
105 changes: 76 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
# Logr [![Build Status](https://travis-ci.org/eclifford/logr.svg?branch=master)](https://travis-ci.org/eclifford/logr) [![Coverage Status](https://img.shields.io/coveralls/eclifford/logr.svg)](https://coveralls.io/r/eclifford/logr?branch=master)

> Logr.js is a JavaScript console logging replacement with support for dynamic object logging injection.
> Logr is a JavaScript console logging replacement with support for dynamic object logging injection.
## Why Logr?

Logr is a simple contextual JavaScript logging utility for making sense of all the console logging statements
in you application. Logr supports some innovative features for reducing the amount of boilerplate logging
that you need to write and lets you access it only when you need it.
Logr is a contextual JavaScript logging utility for making sense of all the console logging statements
in your application. Logr supports some innovative features for reducing the amount of boilerplate logging
that you need to write and makes this logging available to you only when you need it.

**Example**

Here is an example of automatic object logging on another framework of mine [responsify](https://github.com/eclifford/responsify).

![responsify-example](http://f.cl.ly/items/200U1Z0v092j371g3k0o/Image%202014-09-23%20at%2012.51.22.png)

Here is how easy it is.

```js
Logr.log('responsify').attach(Responsify)
```

### Features

- Automatically attach console.debug statements to all the methods of any object with `log.attach`
- Contextually organize your logging statements with **Logr** logs
- Automatically attach logging to all the methods of any object with `log.attach`
- Contextually organize your logging statements named **Logr** logs
- Programaticaly set logging levels globally or per log
- Logging levels are stored per session allowing you to turn logging on and off in QA and Staging environments and have them stored for the
entire browsing session
- AMD and CommonJS compatible
- **AMD** and **CommonJS** compatible
- Fully supported in **Webkit** and **Gecko** won't break in IE

## Quick Start

### Installation with Bower

```bash
bower install logr-js
bower install logr.js
```

### Standard integration
Expand All @@ -40,8 +53,7 @@ bower install logr-js
**Logr** may also be used via AMD

```js
define(['logr'], function(Logr){
})
define(['logr'], function(Logr){})
```

### Creating a log
Expand Down Expand Up @@ -73,12 +85,12 @@ return an existing logging instance of the same name or create you one.

### Dynamic Object logs

The reason I created **Logr** was I wanted to avoid having to write the same boilerplate
The reason I created **Logr** was that I wanted to avoid having to write the same boilerplate
object method logging over and over again. It's verbose, ugly and difficult to manage. What I
wanted was a way for all the methods on my object to have wrapping console statements
baked into them for a visual timeline of the my call stack.
wanted was a way for all the methods on my objects to be automatically wrapped in console statements
outputting their inputs, outputs, order of execution and so on.

#### Attaching to an object
Take for example this simple dog API.

```js
var dog = {
Expand All @@ -98,37 +110,40 @@ baked into them for a visual timeline of the my call stack.
}
}
```

Take for example this simple dog API. If I wanted to listen for all the events on this object
complete with order of operation, nested grouping, parameters operated passed in.
We start by creating a log and then attaching the `dog` object to our logger.

```js
var dogLog = Logr.log('dog');

dogLog.attach(dog);
```

Then you simply execute your methods as you normally would.
Finally we call a method on the `dog` object.

```js
dog.bark(); // bark() is executed wrapped in a console grouping statement
```

![simple-example](http://cl.ly/image/040o2U1b3U09)
Which in your **webkit/gecko** console will output the following.

![simple-example](http://f.cl.ly/items/1o3f1p2v310E2U0O283z/Image%202014-09-22%20at%2019.52.10.png)


Calling `today` which in turns calls all the other methods showcases our nested
callstack.
To see a more sophisticated example we can call the `today` method which in turns
calls several other methods on the `dog` object.

```js
dog.today();
```

![detailed-example](http://cl.ly/image/0I2l2Q423g2V)
![detailed-example](http://f.cl.ly/items/3F0n3E3E3o0K111h2J3A/Image%202014-09-22%20at%2019.52.47.png)

#### Dynamic logging levels

By default all **Logr** logging instances are created with a logging level of 1 or `DEBUG` meaning all
logging messages will reach your browsers console. Changing your logging levels is simple.
By default all **Logr** logging instances are created with a logging level of `DEBUG` meaning all
logging messages will reach your browsers console.

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

##### Setting logging level globally

Expand All @@ -154,12 +169,44 @@ Calling `Logr.setLevel` will apply the supplied level to each stored logging ins
log1.setLevel(Logr.levels.ERROR); // all messages
```

##### Logging levels and LocalStorage SessionState
##### Logging levels and SessionStorage

All log levels are stored in your browsers **sessionStorage** meaning changes to
**Logr**'s levels will persist until the end of the browser session. This is useful in cases
where you want to turn on and off logging levels in QA/Integration environment or simply filter
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.

**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);

foo.attach(dog); // not seen as objects are always on DEBUG level
foo.warn('foo is getting angry'); // not seen
foo.error('foo is blowing up'); // seen
```

If at some point in the future I needed to see `INFO`, `WARN` or `DEBUG` messages
I could set that globally or per log.

All log levels are stored in your browsers session state meaning page refreshes will
return any stored logging level. This is useful in cases where you want to turn on and off
logging levels in QA/Integration environment or simply filter your debugging logs down while
working.
In Chrome/Firefox's javascript console I could type the following.

```js
Logr.log('foo').setLevel(Logr.levels.DEBUG); // sets foo log to DEBUG level
```
or

```js
Logr.setLevel(Logr.levels.DEBUG); // sets all logs to DEBUG level
```

### Contributing

Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logr-js",
"version": "0.0.1",
"name": "logr.js",
"version": "0.0.2",
"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
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<head>
<title>Logger</title>
<script src="logr.js"></script>
<script src="bower_components/stacktrace-js/stacktrace.js"></script>
</head>
<body>

Expand Down Expand Up @@ -32,6 +31,7 @@
dog.run('rocket');

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

</script>
</body>
</html>
12 changes: 6 additions & 6 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.1',
version: '0.0.2',

logs: {},

Expand Down Expand Up @@ -136,10 +136,10 @@
var prop, fn;
var self = this;
for (prop in obj) {
if (obj[prop] !== null && typeof obj[prop] === 'object') {
if (obj !== null && Object.prototype.toString.call(obj[prop]) === "[object Object]") {
this.attach(obj[prop]);
}
if(typeof obj[prop] === 'function') {
else if(typeof obj[prop] === 'function') {
/*jshint -W083 */
fn = obj[prop];
obj[prop] = (function(prop, fn) {
Expand All @@ -148,9 +148,9 @@
console.groupCollapsed("[" + self.logname + "] " + prop + "()", [].slice.call(arguments));
console.time("time");
var value = fn.apply(this, arguments);
// console.trace('trace');
if(value)
console.debug("return: " + value);
if(value) {
console.debug("return: ", value);
}
console.timeEnd("time");
console.groupEnd();
return value;
Expand Down
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.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logr-js",
"version": "0.0.1",
"name": "logr.js",
"version": "0.0.2",
"description": "Logr is a JavaScript console logging replacement with support for dynamic object logging injection",
"author": "Eric Clifford <[email protected]>",
"main": "Gruntfile.js",
Expand All @@ -22,7 +22,7 @@
"devDependencies": {
"chai": "^1.9.1",
"grunt": "^0.4.5",
"grunt-bump": "0.0.14",
"grunt-bump": "^0.0.14",
"grunt-contrib-jshint": "^0.10.0",
"grunt-contrib-uglify": "^0.5.0",
"grunt-contrib-watch": "^0.6.1",
Expand Down

0 comments on commit 8d34155

Please sign in to comment.