Skip to content

Commit

Permalink
Merge branch 'master' of github.com:int32at/log.js
Browse files Browse the repository at this point in the history
  • Loading branch information
int32at committed Nov 10, 2013
2 parents 8a008b5 + 3a7706f commit dad886a
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ you can start using the `log` object like this:
log.warn("this is a warning message");
```

The following log levels are supported:
The following log methods are supported:
- `log.log`: log message
- `log.info`: info message
- `log.debug`: debug message
Expand All @@ -53,6 +53,27 @@ log.debug(console);
//}
```

####Log Levels
`log.js` also provides a method to make your logging more specific. Following levels are supported:
- **OFF**: disables all logging
- **DEBUG**: debug messages, usefull for tracing issues
- **LOG**: normal log message
- **INFO**: informational message, usefull for displaying user info
- **WARN**: warning message, something is not working correctly
- **ERROR**: argh, something went totally wrong!


For example, you only want to log methods of level `WARN` and above, you could use the following example:
```js
//set the log level to WARN and above
log.level("WARN");

log.info("this message will not be sent to the log");

log.warn("this one will, however.");
log.error("this one too!");
```

####Formatting

It is possible to use formatting within all log methods aswell. Example:
Expand Down Expand Up @@ -195,3 +216,57 @@ Now you just have to initialize `log.js` with your newly created appender:
log.init(myCustomAppender);
log.warn("this is a warning message");
```

####Advanced Logging Scenarios
Of course, with `log.js` it is also possible to create your own logger instance, so you can use multiple loggers
across your application. This might be usefull when you want to create a logger for each level (with different appenders)
so you can log debug messages to a web service and display warning messages to the user in a neat way! Consider
the following example:

```js
//set up service appender logger
var config = { url : "https://site/service.php" };
var myWebServiceLogger = log.get("MyWebServiceLogger");
myWebServiceLogger.init(log.appender.serviceAppender, config);

var myToastrLogger = log.get("MyToastrLogger");
myToastrLogger.init(log.appender.toastrAppender, null, function() {

try {
alerrrrrt("Argh this didnt work!");
}
catch(err) {
//sent the error to the web server
myWebServiceLogger.error(err);

//display a nice message to the user
myToastrLogger.error("Something went wrong. Please try again!");
}

});
```
Or with different levels:
```js
//set up alert logger
var myAlertLogger = log.get("MyAlertLogger");
myAlertLogger.init(log.appender.alertAppender);

//set up console logger
var myConsoleLogger = log.get("MyConsoleLogger");
myConsoleLogger.init(log.appender.consoleAppender);

//will display all logs > WARN level as alerts
myAlertLogger.level("ERROR");

//will log all logs > DEBUG level in console
myConsoleLogger.level("DEBUG");

//will be logged
myConsoleLogger.info("this is a info message");

//will not be logged
myAlertLogger.warn("will not be displayed");

//will be logged
myAlertLogger.error("will be displayed");
```

0 comments on commit dad886a

Please sign in to comment.