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

@verycrazydog/level-logger

v1.0.0

Published

A flexible logger supports log level.

Downloads

4

Readme

@verycrazydog/level-logger

A flexible logger supports log level. Inpired by console-log-level, got and loglevel.

Version on npm Supported Node.js version

level-logger was created because of inability to perform certain tasks on existing propular logging modules:

  • Unable processing message parameters after logging level check to avoid unnecessary string processing.
  • Not convenient to alter message prefix.
  • ~~winston did not support multiple message parameters~~ it was supported via format.splat() but unfortunately unable to find out at the time of inventing level-logger.
  • console-log-level hardcoded message level to logging function mapping.

The design of level-logger was inpired by:

  • console-log-level, level-logger is designed to be simple to use.
  • .extend() of got, .extend() method was added for conveniently alter the behavior of logger.
  • methodFactory of loglevel, level-logger is highly customizable.

Install

npm install @verycrazydog/level-logger

Usage

Console logging

const { LevelLogger } = require('@verycrazydog/level-logger')
const logger = new LevelLogger({
	level: 'INFO'
})
// Print 'Hello World!'
logger.info('Hello World!')

Console logging with timestamp prefix

const { LevelLogger, LogTags } = require('@verycrazydog/level-logger')
const logger = new LevelLogger({
	level: 'INFO',
	prefixes: [ LogTags.TIMESTAMP ]
})
// Print '2020-10-13 20:02:11 Hello World!'
logger.info('Hello World!')

Custom timestamp format

const { LevelLogger, LogTags } = require('@verycrazydog/level-logger')
const logger = new LevelLogger({
	level: 'INFO',
	prefixes: [ LogTags.TIMESTAMP ],
	timestampFormatter: value => {
		const hour = value.getHours().toString().padStart(2, '0')
		const minute = value.getMinutes().toString().padStart(2, '0')
		const second = value.getSeconds().toString().padStart(2, '0')
		return `${hour}:${minute}:${second}`
	}
})
// Print '20:54:26 Hello World!'
logger.info('Hello World!')

Use .extend() to create per-request logger

const { LevelLogger, LogTags } = require('@verycrazydog/level-logger')
const express = require('express')

const logger = new LevelLogger({
	level: 'INFO',
	prefixes: [ LogTags.TIMESTAMP, 'SERVER' ]
})
const reqLoggerBase = logger.extend({
	prefixes: [ LogTags.TIMESTAMP, 'REQUEST' ]
})

const app = express()
const port = 3000

app.get('/', (req, res) => {
	const reqId = Math.round(Math.random() * 99999999).toString().padStart(8, '0')
	const reqLogger = reqLoggerBase.extend({
		prefixes: [
			...reqLoggerBase.prefixes,
			reqId
		]
	})
	// Print '2020-10-13 20:22:29 REQUEST 80419951 GET /'
	reqLogger.info(req.method, req.path)
	res.send('Hello World!')
})

app.listen(port, () => {
	// Print '2020-10-13 20:22:28 SERVER Server running at port 3000'
	logger.info('Server running at port', port)
})

Process message for AWS CloudWatch to display as single log entry with formatted JSON display

const { LevelLogger, LogTags, defaultMessageFormatter } = require('@verycrazydog/level-logger')
const logger = new LevelLogger({
	level: 'INFO',
	prefixes: [ LogTags.TIMESTAMP ],
	messageFormatter: (level, resolvedPrefixes, ...messageParams) => {
		messageParams = messageParams.map(p => {
			if (typeof p === 'object' && !(p instanceof Error)) {
				return JSON.stringify(p)
			} else {
				return p
			}
		})
		let message = defaultMessageFormatter(level, resolvedPrefixes, ...messageParams)
		// Reference https://stackoverflow.com/a/44272913/1131246
		message = message.replace(/\r?\n/g, '\r')
		return message
	}
})
logger.info({ message: 'Hello World!' })
logger.error(new Error('Test Error'))

Combine with other logger to log to file

const { LevelLogger, LogTags } = require('@verycrazydog/level-logger')
const winston = require('winston')

const winstonLogger = winston.createLogger({
	level: 'debug',
	format: winston.format.printf(({ message }) => {
		return message
	}),
	transports: [
		new winston.transports.Console(),
		new winston.transports.File({ filename: 'app.log' })
	]
})
const logger = new LevelLogger({
	level: 'DEBUG',
	prefixes: [ LogTags.TIMESTAMP, LogTags.MESSAGE_LEVEL ],
	logger: (level, message) => {
		winstonLogger.log(level, message)
	}
})

// Print '2020-10-13 20:51:07 INFO Hello World!' to both console and log file
logger.info('Hello World!')

License

This module is licensed under the MIT License.