@link-intersystems/console-proxy
v1.1.1
Published
Easy to use and flexible console redirection library.
Downloads
14
Readme
Console Proxy Library
A library to intercept console function calls.
Install
npm i "@link-intersystems/console-proxy"
Use
import {
createConsoleProxy,
createConsoleTemplate,
createLogEnablementInterceptor,
} from "@link-intersystems/console-proxy";
const logEnablement = createLogEnablementInterceptor();
const consoleProxy = createConsoleProxy(console, logEnablement);
logEnablement.setLevelEnabled("debug", false)
consoleProxy.info("INFO", "Hello", "World");
consoleProxy.debug("DEBUG", "Hellow", "World");
// Since debug is disabled only info is logged.
// OUTPUT:
// INFO Hello World
A consoleProxy does not change the default console. If you want to change the default console you can either use the console template
function codeThatLogs() {
console.log("Hello");
console.info("World");
}
logEnablement.setLevelEnabled("info", false)
consoleTemplate.execFn(codeThatLogs)
// OUTPUT:
// Hello
or manually enable/disable the console proxy.
const disableConsoleProxy = consoleProxy.enable();
// The target console's functions will be redirected through the proxy.
console.log("Hello", "World"); // is proxied
disableConsoleProxy();
// The target console's functions are restored.
console.log("Hello", "World"); // is not proxied
Console Template Module
The console template module provides template methods that ensure that the console is properly proxies when the target function executes. Read more
logEnablementHandler.setLevelEnabled("info", false);
consoleTemplate.execFn(codeThatLogs)
Console Proxy Module
The consoleProxy module provides support for intercepting a console, usually the default console. Read more
let lastInfoLog: string;
const consoleProxy = createConsoleProxy();
consoleProxy.setInterceptorFunction("info", (...args) => {
lastInfoLog = args.join(" ");
})
consoleProxy.setInterceptorFunction("log", (...args) => {
// log diabled
})
Console Proxy Interceptors
This module contains interceptors for the console proxy. Read more
const logEnablementInterceptor = createLogEnablementInterceptor();
logEnablementInterceptor.setLevelEnabled("log", false);
consoleProxy.setInterceptor(logEnablementInterceptor);