8000 GitHub - peak-dev/driftwood: A namespaced stylish logger, primarily for the browser
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

peak-dev/driftwood

 
 

Repository files navigation

logger Codeship Status for qubitdigital/logger

A namespaced stylish logger for the browser and node.

Example

var createLogger = require('driftwood')

var log = createLogger('mymodule-main')

log.trace('It supports node and the browser!')
log.debug('You can', { also: 'send some arbitrary metadata!' })

var subModuleLog = log('a-sub-module')

subModuleLog.info('You can create loggers off loggers')
subModuleLog.warn('So that your logs remain under the same top namespace')
subModuleLog.error('Isn\'t this cool?')

Enabling log output:

createLogger.enable() // defaults to { '*': 'info' }
createLogger.enable({
  'foo': 'info',
  'bar:*': 'debug'
}, { persist: true }) // pass `persist: true` when in the browser to keep logging enabled across pages

API

createLogger(name, [additionalLoggers])

Create a new named log instance, optionally supplying additional loggers (e.g. sentry or devtools). additonalLoggers should be an array of functions accepting 3 arguments:

function (name, level, now, { message, error, metadata }) { ... }

log.{LEVEL}(message, [message], [metadata/Error])

Log a message at a level, optionally with a metadata object or error instance. Available levels:

  • trace
  • debug
  • info
  • warn
  • error

The last argument of the log call can be an object or an instance of Error, which Driftwood will attempt to present in a more readable fashion. All preceding arguments will be concatenated together into a string.

log(name)

Create a sub logger. This new logger will inherit the namespace of its parent.

var parentLog = createLogger('foo') // namespace will be foo
var childLog = createLogger('bar') // namespace will be foo:bar

createLogger.enable(config, options)

Enable logging using the optional log level config. The config is a map of name patterns to log level, defaulting to { '*': 'info' }. See below for more pattern examples. You can also pass an options object to the enable function. Currently it only supports the persist option, which lets keep logging enabled across page views (defaults to false, only supports the browser).

createLogger.disable()

Clears the log config, disabling logging.

Enabling logging

By default the logger will not output anything. You need to enable it first, as specified above. Below are some examples of log configs you can pass (you can use * as a wildcard:

  • { '*': null } - will log everything at the default level (info)
  • { 'foo': 'trace' } - will log anything from the logger with the name foo at the trace level
  • { 'foo': 'trace', 'bar*': 'warn' } - will log foo at trace and bar* at warn
  • { 'foo*': 'error', '*': 'info' } - will only log up to error from foo* and up to info from everything else

When running in the browser, you can pass a persist flag to persist the log configuration into localStorage:

createLogger.enable({ '*': 'info' }, { persist: true })

When running in node, it is kept in memory. Therefore if you want log output in node you need to enable it before creating your log instance.

Best practices

Create a main logger.js file in your module/app:

// logger.js
var createLogger = require('driftwood')
module.exports = createLogger('my-app')

In the main part of your app, use the main logger:

// index.js
var log = require('./logger')
log.debug('We are in the entry of the app!')

In each of your submodules, create a sub logger:

// subModuleA.js
var log = require('./logger')('sub-module-a')
log.debug('We are in a submodule of the app!')

When running in node and you want log out, enable logging before creating your loggers:

createLogger.enable()
var log = createLogger('my-app')

Want to work on this for your day job?

This project was created by the Engineering team at Qubit. As we use open source libraries, we make our projects public where possible.

We’re currently looking to grow our team, so if you’re a JavaScript engineer and keen on ES2016 React+Redux applications and Node micro services, why not get in touch? Work with like minded engineers in an environment that has fantastic perks, including an annual ski trip, yoga, a competitive foosball league, and copious amounts of yogurt.

Find more details on our Engineering site. Don’t have an up to date CV? Just link us your Github profile! Better yet, send us a pull request that improves this project.

About

A namespaced stylish logger, primarily for the browser

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 97.0%
  • Makefile 3.0%
2AB7
0