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

ts-tiny-log

v1.1.1

Published

Tiny TypeScript Logger

Downloads

1,511

Readme

ts-tiny-log

Installation

npm i ts-tiny-log

import { log } from 'ts-tiny-log/default';

log.info('Test!');

Basic Usage

There are 5 log-levels, fatal, error, warn, info, and debug. Each one accepts any number of arguments, and also accepts any type of variable, object, array, error, etc.

import { log } from 'ts-tiny-log/default';

// info level with a string variable
log.info('Hello, ', env.APP_TITLE);

// debug level with an object variable
log.debug({ foo: 'bar' });

try {
	something();
}
catch (e) {
	// error level with an Error variable
	log.error('Something broke!', e);
}

Settings

Configure the log by creating a new instance. Export this so you can import your custom logger in your application.

src/log.ts

import { Log } from 'ts-tiny-log';

export const log: Log = new Log({
	shouldWriteTimestamp: false
});

level

Set the level that will be logged. Any level below this will also be logged. For example, setting the level to warn means that only fatal, error, and warn entries will be displayed. info and debug will be ignored.

Default Value: LogLevel.info

Example:

import { Log } from 'ts-tiny-log';
import { LogLevel } from 'ts-tiny-log/levels';

export const log: Log = new Log({
	// Set the level to warn
	level: LogLevel.warn
});

log.fatal('will be displayed');
log.error('will be displayed');
log.warn('will be displayed');
log.info('will be ignored');
log.debug('will be ignored');

Output:

2022-01-17T12:59:24.115Z | fatal | will be displayed
2022-01-17T12:59:24.118Z | error | will be displayed
2022-01-17T12:59:24.118Z | warn  | will be displayed

shouldWriteTimestamp

Turn on/off timestamp column per log entry

Default Value: true

Example:

src/log.ts

import { Log } from '../src';

export const log: Log = new Log({
	// Don't write timestamps
	shouldWriteTimestamp: false
});

log.info('without timestamps');

Output:

info  | without timestamps

shouldWriteLogLevel

Description: Turn on/off log-level column per log entry

Default Value: true

Example:

import { Log } from '../src';

export const log: Log = new Log({
	// Don't write log-level
	shouldWriteLogLevel: false
});

log.info('without log-level');

Output:

2022-01-17T13:16:50.157Z | without log-level

shouldWriteThreadId & threadId

Description: Enable a threadId column for each log entry. Use the optional threadId option if you would like to specify a custom thread id, otherwise the threadId from node worker_threads is used.

Default Value: false

Example:

import { Log } from '../src';

export const log: Log = new Log({
	// Add thread id
	shouldWriteThreadId: true,
});

log.info('with thread id');

Output:

2022-01-17T13:16:50.157Z | info  | main	| with thread id

metadataFormat

Determines how metadata columns (such as log-level and timestamp) are displayed.

Default Value: Piped columns:

(str) => `${str} |`

Example:

import { Log } from '../src';

export const log: Log = new Log({
	// Use square brackets for columns
	metadataFormat: str => `[${str.trim().toUpperCase()}]`
});

log.info('uppercase with square brackets');

Output:

[2022-01-17T13:21:15.224Z] [INFO] uppercase with square brackets

standardOut & standardError

Stream log output to a custom function.

Default value: stdout (standardOut) and stderr (standardError)

Example In this example, output will be streamed to a file via a custom streamToFile() function:

import { Log } from '../src';
import { appendFileSync } from 'fs';

/**
 * Our custom streamToFile function
 * 
 * @param ...data Array of data. Each element in the array is a column of the line.
 */
function streamToFile(...data: any[]): void {
	appendFileSync('test.log', data.join(' ') + '\n');
}

// Set our custom Log
export const log: Log = new Log({
	standardOut: streamToFile,
	standardError: streamToFile,
});

log.info('with custom output stream');

test.log:

2022-01-17T15:34:26.807Z | info  | with custom output stream