TypeScript icon, indicating that this package has built-in type declarations

0.1.6 • Public • Published

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.


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.info("test default log", [1,2,3], [4,5,6])();
log.info("test format %f is a float.", 1.1, [1,2,3])();
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.


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.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.message = prefix;

Add hook to default logger.



let logger = getLogger("App");

To clear all hook of default logger:


Or clear all hook of a named logger:

let logger = getLogger("App");

Set log level in browser console

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

import { exportContrlWithName } from "loghook"

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,



Package Sidebar


npm i loghook

Weekly Downloads






Unpacked Size

15.5 kB

Total Files


Last publish


  • brianjcj