Skip to content

hoodiehq/hoodie-log

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hoodie-log

log API for the browser

Build Status Coverage Status Dependency Status devDependency Status

hoodie-log is a standalone JavaScript library for logging to the browser console. If available, it takes advantage of CSS-based styling of console log outputs.

Example

var log = new Log('hoodie')

log('ohaj!')
// (hoodie) ohaj!
log.debug('This will help with debugging.')
// (hoodie:debug) This will help with debugging.
log.info('This might be of interest. Or not.')
// (hoodie:info) This might be of interest. Or not.
log.warn('Something is fishy here!')
// (hoodie:warn) Something is fishy here!
log.error('oooops')
// (hoodie:error) oooops

var fooLog = log.scoped('foo')
fooLog('baz!')
// (hoodie:foo) baz!

API

Constructor

new Log(prefix)
// or
new Log(options)
Argument Type Description Required
prefix String Prefix for log messages Yes
options.prefix String Prefix for log messages Yes
options.level String Defaults to warn. One of debug, info, warn or error. debug is the lowest level, and everything will be logged to the console. error is the highest level and nothing but errors will be logged. No
styles Boolean or Object Defaults to true. If set to false, all log messages are prefixed by (<prefix>:<log type>), e.g. (fooprefix:warn) bar is not available.. If set to true, styles are applied to the prefix. The styles can be customised, see below No
styles.default String Defaults to color: white; padding: .2em .4em; border-radius: 1em. Base CSS styles for all log types No
styles.reset String Defaults to background: inherit; color: inherit. Reset CSS styles, applied for message after prefix No
styles.log String Defaults to background: gray. CSS Styles for default log calls without log level No
styles.debug String Defaults to background: green. CSS Styles for debug logs No
styles.info String Defaults to background: blue. CSS Styles for info logs No
styles.warn String Defaults to background: orange. CSS Styles for warn logs No
styles.error String Defaults to background: red. CSS Styles for error logs No

Example

var log = new Log({
  prefix: 'hoodie',
  level: 'warn',
  styles: {
    default: 'color: white; padding: .2em .4em; border-radius: 1em',
    debug: 'background: green',
    log: 'background: gray',
    info: 'background: blue',
    warn: 'background: orange',
    error: 'background: red',
    reset: 'background: inherit; color: inherit'
  }
}

log.prefix

Read-only

log.prefix

Prefix used in log messages

Example

log = new Log('hoodie')
log.prefix // hoodie
log.warn("Something is fishy here!")
// (hoodie:warn) Something is fishy here!


log.level

One of debug, info, warn or error. debug is the lowest level, and everything will be logged to the console. error is the highest level and nothing but errors will be logged.

log.level

Example

log.level = 'debug'
log.debug('This will help with debugging.')
// (hoodie:debug) This will help with debugging.
log.level = 'info'
log.debug('This will help with debugging.')
// <no log>
log.level = 'foo'
// throws InvalidValue error

log()

Logs message to browser console. Accepts same arguments as console.log

log("ohaj!")

log.debug()

Logs debug message to browser console if level is set to debug. Accepts same arguments as console.log

log.debug('This will help with debugging.')

log.info()

Logs info message to browser console if level is set to debug or info Accepts same arguments as console.log

log.info('This might be of interest. Or not.')

log.warn()

Logs warning to browser console unless level is set to error Accepts same arguments as console.log

log.warn('Something is fishy here!')

log.error()

Logs error message to browser console. Accepts same arguments as console.log

log.error('oooops')

log.scoped()

log.scoped(prefix)
Argument Type Description Required
prefix String Prefix for log messages Yes

Returns log API with extended prefix

Example

var log = new Log('hoodie')
log('ohaj!')
// (hoodie) ohaj!
var fooLog = log.scoped('foo')
fooLog('baz!')
// (hoodie:foo) baz!

Using in production

You might restrict logging in production to only error logs. You can safely do this by setting log.level to error.

For example, this could be configured in your application like this:

if (isProduction) {
  log.level = 'error';
}

Testing

Local setup

git clone [email protected]:hoodiehq/hoodie-log.git
cd hoodie-log
npm install

Run all tests and code style checks

npm test

Run all tests on file change

npm run test:watch

Run specific tests only

node tests/specs/debug.js # run .debug() unit tests

PROTIP™: pipe output through a pretty reporter

Contributing

Have a look at the Hoodie project's contribution guidelines. If you want to hang out you can join our Hoodie Community Chat.

License

Apache 2.0