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

@jlcarveth/log.js

v1.2.3

Published

A dead-simple JSON-formatted logging utility for Node.js applications.

Downloads

6

Readme

log.js

A dead-simple, lightweight (>8KB) logging utility for Node.js applications in a JSON format.


GitHub code size in bytes npm (scoped) npm GitHub issues

Installation

log.js is available on the npm registry, and can be installed with a single command:

npm i @jlcarveth/log.js --save

Otherwise, log.js consists of a single javascript file, so it can be downloaded and added to your project manually:

curl https://raw.githubusercontent.com/JLCarveth/log.js/master/log.js > log.js

Usage

The goal of log.js is to be dead simple to use. Simplest use of log.js simply involves replacing console.log calls with Logger.log calls:

import logger from 'log.js'; // Import the package

const Logger = new logger(); // Initialize the package

[...]
Logger.log('A request was made to /api/some/route.');

This will return a nice JSON-formatted string to be output to STDOUT:

{"timestamp":"2022-06-14T15:43:24.516Z","level":"info","message":"A request was made to /api/some/route.","hostname":"JohnC-pc"}

When a log level is not specified, info is used. To specify a log level, do the following:

Logger.log("This is a warning, peace and love.", Logger.LEVELS.WARNING);

The following log levels are available by default:

  • info
  • warning
  • error
  • fatal

Additional parameters can be logged easily. Simply store the parameters in an Object and pass it to log():

let data = {
    "headers" : { ... },
    "request" : { ... }
}
Logger.log("Incoming Request on /api/books", data);

Any parameters passed to log() are included in the log messages:

{"timestamp":"2022-06-14T15:31:37.273Z","level":"info","message":"Incoming request","hostname":"JohnC-pc","clientAddress":"::1","targetURL":"https://www.uuidtools.com/api/generate/v5/namespace/ns:url/name/JohnLCarveth","method":"GET","body":{},"headers":{"target-url":"https://www.uuidtools.com/api/generate/v5/namespace/ns:url/name/JohnLCarveth","user-agent":"PostmanRuntime/7.29.0","accept":"*/*","postman-token":"6f92736e-ec2c-4971-9dfe-070c6e448592","host":"localhost:3069","accept-encoding":"gzip, deflate, br","connection":"keep-alive"}}

Command-Line Interface

log.js also provides a command-line tool to allow for JSON logging in other environments, specifically shell scripts. Simply install the package globally:

npm i -g @jlcarveth/log.js

Once installed, the logger binary becomes available. Usage is quite familiar to the Javascript API:

# Basic Usage
$ logger "This is a log message"
{"timestamp":"2022-06-27T12:45:51.716Z","level":"info","message":"This is a log message"}

# Specify log level with -l OR --level
$ logger "This is a test message" -l warning
{"timestamp":"2022-06-27T12:39:06.999Z","level":"warning","message":"This is a test message"}

# Additional data is passed through with a key=value pattern:
$ logger "This is a test message" -l warning x=42 y=21
{"timestamp":"2022-06-27T12:39:15.204Z","level":"warning","message":"This is a test message","x":"42","y":"21"}

Output is JSON-formatted and can be piped to a file or another utility.