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

loggol

v0.0.4

Published

Configurable logging module written in TypeScript

Downloads

1

Readme

Introduction

Simple logging module written in TypeScript.

Levels

There are 5 log levels:

const Logger = require("loggol")

Logger.Level.DEBUG
Logger.Level.INFO
Logger.Level.WARN
Logger.Level.ERROR
Logger.Level.OFF

Logging to the console

The following code creates a logger object that writes to stdout and stderr in the format [dd/mm/yyyy hh:mm:ss][LEVEL][TAG]: message]:

const Logger = require("loggol")

const logger = new Logger.Console({
	tag: "Logger1",
	level: Logger.Level.DEBUG,
	format: Logger.Format.standard
});

logger.debug("debug");
logger.info("info");
logger.warn("warn");
logger.error("error");

If you would like colours and your terminal supports it, replace Logger.Format.standard with Logger.Format.standardColoured

Logging to a file

The following code creates a logger object that writes to a file stream of your choice, in the same format as above

const fs = require("fs");
const Logger = require("loggol")

const logger = new Logger.File({
	tag: "Logger2",
	level: Logger.Level.DEBUG,
	format: Logger.Format.standard,
	file: fs.createWriteStream("output.log", {
		flags: "a",
		encoding: "utf-8"
	})
});

Chaining loggers

Often you could find yourself wanting to log the same data to two or more different output, you can do this by using the chain method of the Logger prototype

const Logger = require("loggol");

const logger1 = new Logger.Console(...);
const logger2 = new Logger.File(...);
const logger3 = new Logger.File(...);

logger1
	.chain(logger2)
	.chain(logger3)

logger1.info("hello!");
// logs to logger2 and logger3 as well

logger2.info("wow");
// logs to logger3, but not logger1 

Creating your own format

Format functions receive a LogData object:

type LogData = {
	date: Date;
	tag: string;
	level: number;
	items: any[]
};

Just to clarify, since the others are self explanatory, items is just an array of whatever the calling code wanted to log.

Your function should look something like this:

function myFormat({ date, tag, level, items }) {
	// create output here
	// return string
}

Then you can pass this function around to all your loggers that need to use it.

Creating your own Logger

If the included implemenations don't quite fit your needs you can always implement your own and keep the same public interface.

const Logger = require("loggol");

class OtherLogger extends Logger.Abstract {
	write(message) {
		// whatever needs to happen to log 'message'
	}
}

Logger factories

Sometimes you may not want to use a single logger with the same tag and level. A function that you can import everywhere a logger is needed is great for creating a bunch of similar logger objects.

For example, a lot of files I have only need Logger.Console and they should all have the same level and format, but a different tag.

const Logger = require("loggol");
const level = Logger.Level.INFO;
const format = Logger.Format.standardColoured;

function createLogger(tag) {
	return new Logger.Console({
		tag,
		level,
		format
	});
}

module.exports = createLogger;

You could also do something similar to return different types of Logger's