Yet another logger library
Find a file
2023-08-15 09:54:46 +01:00
.github/workflows Create workflow 2023-08-15 09:25:14 +01:00
.gitignore Initial Commit 2023-08-14 17:05:12 +01:00
.npmignore Initial Commit 2023-08-14 17:05:12 +01:00
index.ts Initial Commit 2023-08-14 17:05:12 +01:00
LICENSE Initial commit 2023-08-14 16:54:11 +01:00
package-lock.json 1.0.2 2023-08-15 09:54:46 +01:00
package.json 1.0.2 2023-08-15 09:54:46 +01:00
README.md 1.0.2 2023-08-15 09:54:46 +01:00
tsconfig.json Initial Commit 2023-08-14 17:05:12 +01:00

hsconsole   CodeFactor   Node.js CI   npm

I've been using this logger code since I started the move of all my applications to TypeScript with some modifications. It's based on an even earlier version initially started in Revolution. Since it's essentially the same every time I decided to make it an NPM package so I can reuse it.

What does this do

hsconsole logs to the console in a clean, formatted and readable way while also writing to log files on disk. All logs are kept until you decide to delete them.

Usage

Import the Console class from hsconsole

const { Console } = require("hsconsole");
import { Console } from "hsconsole";

and use it by calling either printInfo, printWarn or printError.

You can also specify a custom header to prepend to the start of the log file using customHeader as follows:

Console.customHeader(`xyz started at ${new Date()}`);

This must be called this before anything is logged to the console!

Cleaning up

It is important that you tell hsconsole to clean up before your application closes (i.e in the event of a CTRL+C) so it can be ensured that all log text is flushed to disk and to allow hsconsole to rename the latest log to the date your application was started. You can do this by calling cleanup in, for example, a SIGINT event handler:

process.on("SIGINT", signal => {
	Console.cleanup();
});

The above should work on both *nix and Windows systems.