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

@mojule/log-formatter

v0.3.0

Published

Simple formatting for logging

Downloads

74

Readme

log-formatter

Simple formatting for logging

npm install @mojule/log-formatter

usage

default logger

import { logger } from '@mojule/log-formatter'

or:

const { logger } = require( '@mojule/log-formatter' )
logger.time( 'My program' )

logger.trace( 'My program trace' )
logger.debug( 'Debug', { foo: { bar: { baz: [ 1, 2, 3 ] } } } )
logger.info( 'Hello, world!', 1, [ 1, 2, 3 ], 'etc' )

try {
  logger.warn( 'Uh oh' )

  throw Error( 'A bad thing happened' )
} catch( err ){
  logger.error( err )
} finally {
  logger.fatal( 'Exiting program because of the bad thing' )
  logger.time( 'My program' )
}
  • logger.trace, logger.debug, logger.time go to console.debug
  • logger.info goes to console.info
  • logger.warn goes to console.warn
  • logger.error and logger.fatal goes to console.error

Note that trace and time are handled internally for consistency of formatting rather than using console.trace or console.time

time    2019-08-08T00:40:02.752Z        Start   My program
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
trace   2019-08-08T00:40:02.758Z        My program trace
────────────────────────────────────────────────────────────────────────────────
Trace:
    at Object.<anonymous> (log-formatter/examples/default.js:5:8)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:279:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:752:3)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
debug   2019-08-08T00:40:02.759Z        Debug
────────────────────────────────────────────────────────────────────────────────
{
  "foo": {
    "bar": {
      "baz": [
        1,
        2,
        3
      ]
    }
  }
}
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
info    2019-08-08T00:40:02.760Z        Hello, world!   1       [ 1, 2, 3 ]     etc
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
warn    2019-08-08T00:40:02.760Z        Uh oh
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
error   2019-08-08T00:40:02.760Z        Error   A bad thing happened
────────────────────────────────────────────────────────────────────────────────
Error: A bad thing happened
    at Object.<anonymous> (log-formatter/examples/default.js:12:9)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:279:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:752:3)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
fatal   2019-08-08T00:40:02.761Z        Exiting program because of the bad thing
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
time    2019-08-08T00:40:02.761Z        End     My program      0s 9.330659ms
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

simple file logger

createLogger allows you to provide functions to call for each of the logging levels rather than using the defaults, which go to stdout and stderr

The functions you provide are called with a single string argument containing the message

All functions are optional, if you don't provide them, the output will go nowhere - you can use this to implement custom logging levels

import { createLogger } from '@mojule/log-formatter'
import { appendFile } from 'fs'

or:

const { createLogger } = require( '@mojule/log-formatter' )
const { appendFile } = require( 'fs' )
const stdOut = './examples/stdout.txt'
const stdErr = './examples/stderr.txt'

const append = ( path, data ) => appendFile(
  path, data + '\n', 'utf8', err => {
    if( err ) console.error( err )
  }
)

const writeOut = content => append( stdOut, content )
const writeErr = content => append( stdErr, content )

const trace = writeOut
const debug = writeOut
const time = writeOut
const info = writeOut
const warn = writeErr
const error = writeErr
const fatal = writeErr

const options = {
  trace, debug, time, info, warn, error, fatal
}

const logger = createLogger( options )

logger.time( 'My program' )

logger.trace( 'My program trace' )
logger.debug( 'Debug', { foo: { bar: { baz: [ 1, 2, 3 ] } } } )
logger.info( 'Hello, world!', 1, [ 1, 2, 3 ], 'etc' )

try {
  logger.warn( 'Uh oh' )

  throw Error( 'A bad thing happened' )
} catch( err ){
  logger.error( err )
} finally {
  logger.fatal( 'Exiting program because of the bad thing' )
  logger.time( 'My program' )
}