Logging support for muffin apps
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
LICENSE
README.md
component.json
index.coffee

README.md

muffin/Logger

A super simple logger for the browser, written in 66 lines of CoffeeScript.

It's included in all Muffin apps by default. You can also use it without Muffin, as long as you have a way to load CommonJS modules.

Usage

The recommended approach is to create a logging object in the global namespace.

Logger = require 'muffin/Logger'
window.logging = new Logger()

Then you can use the logger from anywhere inside your app:

logging.info(msg)
logging.debug(msg)

You can pass in a few options when creating the logger, including logLevel, writer, formatter, maxEntries:

window.logging = new Logger(logLevel='ERROR', writer='LocalStorageWriter', formatter='JSONFormatter', maxEntries=50)

In the case above, the logger will only output messages with a log level higher or equal to 'ERROR', the logs will be written to local storage, formatted in JSON, and no more than 50 entries are kept in the log history.

The log history can be accessed at logging.history. You can specify the maximum of entries to be kept in the history (maxEntries) when creating the logger. The default maxEntries is 20. When the log history is full, the oldest messages will be dropped to make space for the new ones.

Design

The design of this logger loosely follows that of Log4j.

Supported log levels:

  1. TRACE
  2. DEBUG
  3. INFO
  4. WARN
  5. ERROR
  6. FATAL

Supported writers:

  1. ConsoleWriter (default): writes to the JavaScript console
  2. LocalStorageWriter: writes to localStorage['app:log']

Supported formatters:

  1. SimpleFormatter (default):
Wed Jul 17 2013 20:06:16 GMT-0400 (EDT) [INFO]: test message
  1. JSONFormatter:
{"msg":"test message","datetime":"2013-07-18T00:12:06.782Z","level":"INFO"}

Compatibility

This logger has been tested under all major browsers including IE6+, Chrome, Firefox, Safari.

In older browers that don't support console.log (e.g., IE6), a dummy console implementation is put in so no JavaScript errors will be thrown.

LICENSE

Released under the MIT license.