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

local-iso-logger

v1.3.0

Published

Simple logger with local timestamps in ISO8601 format, and clean stack traces on errors

Downloads

31

Readme

local-iso-logger — human-readable, clean timestamped output

Dependency Status devDependency Status

Simple and to-the-point logger class outputting to the console and optionally to a file, with cleaned error stack traces. Uses four standard log levels: debug, info, WARN, ERROR.

screenshot

Rationale

I wrote this minimalistic module because none of the existing logging packages on NPM provides the features below. Most of my efforts to use an existing library went to Bristol, which ended up being abandoned.

Features

  • prefixes each line with the local time in RFC3339 YYYY-MM-DD HH:MM:SS format (via local-iso-dt)

    [2020-09-06 17:00:00] It's tea time
  • outputs Error objects to file (via serialize-error)

  • cleans up Error stack traces (via clean-stack)

  • makes absolute error paths relative to the home directory

  • uses the native Node console with colorization, plus yellow for WARNs and red for ERRORs

    • the downside is that objects beyond 3 levels deep will be displayed as [Object]. Refer to the same timestamp in the log file to see the full JSON dump.
  • exposes a writable stream

  • you can use the familiar variable-arity console format, with arguments of any type:

    logger.warn('Got', results.length, 'results, but also an error:', results, new Error('oops'));
  • arrays are logged in JSON format, with newlines for readability

    logger.error([error1, error2]);  // smart indented display

Install

npm i local-iso-logger

Importing

  • TypeScript: import { Logger } from 'local-iso-logger';
  • ES modules .mjs files: import { Logger } from 'local-iso-logger/index.mjs';
  • Old school CommonJS: const { Logger } = require('local-iso-logger/index.js');

This is a hybrid npm package (created using variation 2.4.1 described on that page), with conditional exports that enable named imports even from TypeScript code generating ES Modules, which would otherwise only support default (not named) imports from the CommonJS target of this package (TypeScript doesn't support .mjs input files).

Examples

import { Logger } from 'local-iso-logger';
const logger = new Logger('file.log');

// Timestamp log messages in the YYYY-MM-DDTHH:MM:SS format and the local timezone
logger.debug('Greyed out timestamp to de-emphasize');
logger.info('Variable number of arguments, not just', 1);
logger.warn('Yellow for warnings');
logger.error('Error with clean stack trace', new Error('Oops'));

For more examples, see examples.ts.

Methods

new Logger(filename?: string)

Constructor that takes an optional filename argument. If passed, it will create a .stream instance member, which is an append writable stream. You can pass the stream to other modules, for example to set up debugging with Mongoose:

const logger = new Logger('file.log');
mongoose.set('debug', logger.stream);

All methods log to the console, and if a filename was passed to the constructor, to that file as well. The file will contain full JSON object dumps, while the console output will only introspect objects 3 levels deep. Both the console and the file output start with the local time in RFC3339 [YYYY-MM-DD HH:MM:SS] format (via local-iso-dt)

debug(...messages)

Log in grey color to the console, and with the debug prefix to the file.

info(...messages)

Log in normal color to the console (via console.info), and with the info prefix to the file.

warn(...messages)

Log to the console via console.warn, and with the WARN prefix to both the console and the file.

error(...messages)

Log to the console via console.error, and with the ERROR prefix to both the console and the file.

write(message)

Write to the stream directly, with the debug prefix. Also passes the message to console.debug, in normal color.

Logger.timestamp(datetime?): string

Static method that returns the timestamp prefix in RFC3339 [YYYY-MM-DD HH:MM:SS] format. The datetime parameter is optional and defaults to the current time. It is passed unchanged to local-iso-dt.

localISOdt(datetime?): string

Re-export of local-iso-dt.

teardown()

Closes the stream.

Known issues

Logging something right before calling process.exit() won't flush the output to the file. This is a problem with all loggers (e.g. Winston, Bristol). As a workaround, try delaying the exit:

setTimeout(() => process.exit(1),  1000);

Author

Dan Dascalescu

License

MIT