npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

if-logger

v0.12.5

Published

`if-logger` can set whether to log or not dynamically

Downloads

21

Readme

if-logger

if-logger is a logger that can be set whether to log or not dynamically

Feachers

Install

npm i if-logger

Usage

dynamically log or not

import createLogger from 'if-logger'

const logger = createLogger()

logger.info('some log') // print '[info] some log'
logger.if(true).info('some log') // print '[info] some log'
logger.if(false).info('some log') // do not print

// predicate function is usable
logger.if(() => false).info('some log') // do not print

.if method return new logger

import createLogger from 'if-logger'

const logger = createLogger()
let cnt = 0
const only3times = () => {
  cnt++
  return cnt < 4
}
const ifLogger = logger.if(only3times) // or `createLogger({pred: only3times})`
ifLogger.info('some text 1') // print
ifLogger.info('some text 2') // print
ifLogger.info('some text 3') // print
ifLogger.info('some text 4') // do not print
ifLogger.info('some text 5') // do not print

Log level

{
  off: 0,
  error: 1,
  warn: 2,
  log: 3,
  info: 4,
  verbose: 5,
  debug: 6,
  all: 7,   // default
}

import createLogger from 'if-logger'

const logger = createLogger({ level: 'info' })

logger.log('log-text') // will be printed '[log] log-text'
logger.info('info-text') // will be printed '[info] info-text'
logger.verbose('verbose-text') // do not print

Dynamic level is also usable

import createLogger from 'if-logger'

const logger = createLogger({ level: () => 'info' }) // level function is evaluated when print log

logger.log('log-text') // will be printed '[log] log-text'
logger.info('info-text') // will be printed '[info] info-text'
logger.verbose('verbose-text') // do not print

Dynamic change of option

import createLogger from 'if-logger'

const logger = createLogger({ level: 'info' })

logger.info('hello 1') // print
logger.verbose('hello 2') // do not print
const logger2 = logger.new({ level: 'verbose' })
logger2.info('hello 3') // print
logger2.verbose('hello 4') // print

Tagging

import createLogger from 'if-logger'

const logger = createLogger({ tags: ['AA', 'BB'] }) // default tags is set

logger.info('some log') // print '[info][AA][BB] some log'

// tags can be changed dynamically
logger.tags(['CC', 'DD']).info('some log') // print '[info][CC][DD] some log'

// parameter can be spreaded
logger.tags('CC', 'DD').info('some log') // print '[info][CC][DD] some log'

// tags can be added dynamically
logger.addTags(['CC', 'DD']).info('some log') // print '[info][AA][BB][CC][DD] some log'

// parameter can be spreaded
logger.addTags(['CC', 'DD']).info('some log') // print '[info][AA][BB][CC][DD] some log'

dynamic tag usable

import createLogger from 'if-logger'

const time = () => String(new Date()).substr(16, 8)
const logger = createLogger({ tags: [time] })

logger.info('some log') // print '[info][12:40:57] some log'
// some biz logic
logger.info('some log') // print '[info][12:41:12] some log'
// some biz logic
logger.info('some log') // print '[info][12:44:36] some log'

Log level filter

import createLogger from 'if-logger'

const logger = createLogger({ level: 'all', levelFilter: ['error', 'info'] })

logger.error('some text') // print
logger.info('some text') // print
logger.log('some text') // do not print

Tag filter

import createLogger from 'if-logger'

const logger = createLogger({ level: 'all', tagFilter: ['AB', 'BB'] })

logger.tags(['AA', 'BB']).error('some text') // print
logger.tags(['CC', 'DD']).info('some text') // do not print
logger.tags(['EE']).log('some text') // do not print
logger.tags(['AA']).log('some text') // print
logger.tags(['AA', 'ZZ']).log('some text') // print

Performance check

time, timeEnd usable

scope: logger object

import createLogger from 'if-logger'

const logger = createLogger()

logger.info.time('performance test')
// .. some biz logic excuted
logger.info.timeEnd('performance test') // print '[info] performance test 12ms'

Time logging is supported

scope: stopwatch object

import createLogger from 'if-logger'

const logger = createLogger()
const sw = logger.info.stopwatch
sw.start('test')
// some biz logic
sw.check('aa')
// some biz logic
sw.check('bb')
// some biz logic
sw.check('cc')
// some biz logic
sw.end()

/* output is
[info] [test] start
[info] [test] aa (7ms / 7ms)
[info] [test] bb (3ms / 10ms)
[info] [test] cc (2ms / 12ms)
[info] [test] end (total: 12ms)
*/

Custom format

import createLogger from 'if-logger'

function format(level, tags, message) {
  const tagstr = tags.join(',')
  return `(${level})(${tagstr}) ${message}`
}
const logger = createLogger({ format })

logger.tags(['AA', 'BB']).verbose('some text') // print '(verbose)(AA,BB) some text'

simpleFormat supported

import createLogger, { simpleFormat } from 'if-logger'

const logger = createLogger({ format: simpleFormat })

logger.tags(['AA', 'BB']).verbose('some text') // print 'V AA BB | some text'

Plain Object & Multiple arguments

Plain objects and multiple arguments are loggable. But the option of format, transport and the method of time, timeEnd are not available at this time. (only console output available)

import createLogger from 'if-logger'
const logger = createLogger()

logger.verbose({ a: 1 }) // print [verbose] {a:1}
logger.verbose({ a: 1 }, { b: 2 }) // print [verbose] {a:1} {b:2}
logger.verbose('aa', 'bb', 11) // print [verbose] aa bb 11

function argument

function parameter is possible

import createLogger from 'if-logger'
const logger = createLogger()

logger.addTags('AA').verbose((format) => {
  console.log(...format('some text'))
}) // print '[verbose][AA] some text'

logger.addTags('AA').verbose((format) => {
  console.log(...format('some text1', 'some text2'))
}) // print '[verbose][AA] some text1 some text2'

Custom transport

Log message is not be trasfered to customTransport when multi arguments or time, timeEnd used (Only one string arguments is transfered to customTransport)

import createLogger, { consoleTransport } from 'if-logger'
// consoleTransport is default transport
const logger = createLogger({ transports: [consoleTransport, customTransport] })
logger.debug('some text')
function customTransport(level, message, formatMessage) {
  /*
   * level: 'debug'
   * message: 'some text'
   * formatMessage: '[debug] some text'
   */
  // your transport action
}