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 🙏

© 2026 – Pkg Stats / Ryan Hefner

loghook

v0.1.6

Published

a log lib preserves (console) line number, supports set log level and supports plugins.

Readme

A log lib preserves right (console) line number, supports setting log level and supports plugins.

It preserves all the characters of consloe info(associate file and line number where call the log function, display logged object nicely in browser), and enable to add hooks to custom the log behaviors, such as changing the log format, adding prefix, recording logs to local memory or sending over networking.

The default enabled hook is to add timestamp and log level string to log message.

Usage

Use the default log

Please NOTE: there are two parentheses needed to call log functions. The first parenthese is to call the hooks and the second is to finally call the console log function. We don't wrap console info in function to make the right file and line number information associated with the log. If calling the console log function inside the wrapper function, the file and line number will associate with wrapper function. It is obviously not we expect.

hope it is not too disturbing.

import { log, LogLevel } from "loghook"

log.setLogLevel(LogLevel.Trace);

log.info("test default log", [1,2,3], [4,5,6])();
log.info("test format %f is a float.", 1.1, [1,2,3])();
log.warn([1,2,3,4])();
log.warn()();
log.info({a:1, b:2}, [1,2,3])();

Will output shomething like that:

9/14/2019, 15:11:11.002 Info test default log [ 1, 2, 3 ] [ 4, 5, 6 ]
9/14/2019, 15:11:11.123 Info test format 1.1 is a float. [ 1, 2, 3 ]
9/14/2019, 15:11:11.125 Warn [ 1, 2, 3, 4 ]
9/14/2019, 15:11:11.127 Warn
9/14/2019, 15:11:11.129 Info { a: 1, b: 2 } [ 1, 2, 3 ]

Default level of a logger is Info. You can change it via setLogLevel function.

getLogger

You can make a new logger, with a log name and new log level set. Given the same log name, the same logger will always return.

import { getLogger, LogLevel } from "loghook"

let logger = getLogger("App");
logger.setLogLevel(LogLevel.Debug);

logger.info("test logger", [1,2,3], [7,8,9])();

Will output shomething like that:

9/14/2019, 15:11:11.122 App: Info test logger [ 1, 2, 3 ] [ 7, 8, 9 ]

Add your hook (to add customize behaviors)

Hook function prototype:

export type PluginHookFnType = (logger: LogHook, level: LogLevel, logObject: LogObject) => void;

The default hook (to add time and log level string to log message) is implemented as below, for your referrence.

export function Hook_AddNameAndTime(logger: LogHook, level: LogLevel, logObject: LogObject): void {
    let prefix = getPrefix(logger.name) + " " + LogLevel[level];
    if (typeof logObject.message == 'string') {
        // message is string. it might contain format flags like %d, %s
        logObject.message = prefix +  " " + logObject.message;
    } else {
        if (logObject.message != undefined) {
            logObject.optionalParams.unshift(logObject.message);
        }
        logObject.message = prefix;
    }
}

Add hook to default logger.

log.addHook(your_hook_function);

or:

let logger = getLogger("App");
logger.addHook(your_hook_function);

To clear all hook of default logger:

log.clearAllHook()

Or clear all hook of a named logger:

let logger = getLogger("App");
logger.clearAllHook();

Set log level in browser console

You can export setLogLevel function to window object. e.g.,

import { exportContrlWithName } from "loghook"
exportContrlWithName("MY_LOG_HOOK_CTL");

Then you can set log level in browser control like that:

window.MY_LOG_HOOK_CTL.setLogLevel(1);  // set the default logger's level to debug
window.MY_LOG_HOOK_CTL.setLogLevel(0, "App");  // set the level of logger name "App" to trace

window.MY_LOG_HOOK_CTL.setLogLevelForAllLoggers(3); // set log level for all loggers to warn

You can use it to turn on/off logging on fly when running your app in the browser.

The loglevel definitions:

export enum LogLevel {
    Trace = 0,
    Debug = 1,
    Info = 2,
    Warn = 3,
    Error = 4,
    Silent = 5,
}