Skip to content

Scoped & colorful console logging

Notifications You must be signed in to change notification settings

Teachiq/log-scope

 
 

Repository files navigation

Usage

Installation

npm install log-scope

Create new log scope

import logScope from 'log-scope'
const log = logScope('scopeName')

log.success('We have a new scope!')
// scopeName [SUCCESS] (0): We have a new scope!

log.success('It has a counter per type & scope')
// scopeName [SUCCESS] (1): It has a counter per type & scope

log.debug('Use another type for debugging')
// scopeName [DEBUG] (0): Use another type for debugging

Types can be paused in a scope when you don't need e.g. debug-loggs

log.pause('debug')
log.debug('Disable logs for a type in a scope')

Create other scopes to clarify where the logs comes from in the console

// Create scopes, it's super convenient for e.g. different modules
const anotherLog = logScope('anotherScopeName')
anotherLog.success('Another scope!')
// anotherScopeName [SUCCESS] (0): Another scope!

If you only want logs from one scope, use the .only() method

// Only print from this scope
anotherLog.only()

log.info('This will not print since another log scope has the only-flag active')

Include data and other params in the logs. You can have as many parameters as you want. The types don't matter, they are printed to the console via the regular console.log-function

log.success('Logging some data', 1, 'a string', { key: 'Value' })
// scopeName [SUCCESS] (2): Logging some data
// 1  'a string'  { key: "Value" }

Log types

The types logs different colors in the console.

const message = 'Some log message'
const data = null // Optional, can be any loggable type

log.debug(message, ...data)     // blue
log.info(message, ...data)      // blue
log.success(message, ...data)   // green
log.todo(message, ...data)      // purple
log.fixme(message, ...data)     // red
log.sequence(message, ...data)  // black
log.danger(message, ...data)    // red
log.warning(message, ...data)   // orange
log.error(message, ...data)     // red

Print only from one scope

// Select scope to only print for
log.only('scopeName')

// OR

// No input scope name defaults to active scope
log.only()

Pause/resume scope type

const type = 'debug' // Or any other type
log.pause(type)
log.resume(type)

// Pause all logs for the log scope (i.e. don't pass type input)
log.pause()

Initialize log scope

import logScope, { init } from 'log-scope'
// OR: import { init } from 'log-scope'

const onErrorCallback = (err, args) => {
  // args = { message, data }

  ... // do something
}

init({
  print: true, // Print to console
  store: true, // Stores all logs in log.history
  conter: true, // Include log counter in print
  onError: onErrorCallback
})

If you're using bugsnag (or other error tracker), use the error callback to send the error-log.

init({
  onError: err => {
    bugsnagClient.notify(err)
  }
})

About

Scoped & colorful console logging

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • JavaScript 63.1%
  • TypeScript 36.9%