loggych
v0.2.1
Published
log abstraction
Readme
loggych
Logger abstraction and console realization
ILoggych
//Universal logger abstraction
export interface LoggychConf {
debug: boolean;
info: boolean;
warn: boolean;
err: boolean;
}
export interface ILoggych
{
log(vals: Partial<Record<keyof LoggychConf, any>>): Promise<void>;
logCustom(vals: Record<string, any>): Promise<void>;
addContext(...any: any[]): ILoggych;
switchConf(keys: LoggychConf): ILoggych;
}
export const logErrConf = {err: true, debug: false, info: false, warn: false};
export const logWarnConf = {err: true, debug: false, info: false, warn: true};
export const logInfoConf = {err: true, debug: false, info: true, warn: true};
export const logDebugConf = {err: true, debug: true, info: true, warn: true};expo
ConsoleLoggych
//Console logger realization
export class ConsoleLoggych implements ILoggych
{
public constructor(conf: LoggychConf, context: any[] = []);
private consoleLog(v: any): void;
log(vals: Partial<LoggychConf>): Promise<void>;
logCustom(vals: Record<string, any>): Promise<void>;
public switchConf(keys: LoggychConf): ILoggych;
public addContext(...any: any[]): ILoggych;
}Test:
//This script:
const log = (new ConsoleLoggych(logDebugConf)).addContext("test run")
const val: any = {a: 12, b: 'asdasd"', c: [{a: 123}, BigInt(123124), false, null, undefined]}
val.d = val
await log.log({debug: ["Logging info", val]})
await log.addContext("Another context", 12).log({err: new Error("test error")})
//Will print his debug line:
//test run
//debug:
//Logging info {"a":12,"b":"asdasd\"","c":[{"a":123},"123124",false,null,null],"d":"[Circular]"}
//
//test run
//Another context 12
//err:
//{
// message: 'test error',
// name: 'Error',
// stack: 'Error: test error\n' +
// ' at D:\\js-projects\\loggych\\js\\scripts\\test-loggych.js:53:91\n' +
// ' at step (D:\\js-projects\\loggych\\js\\scripts\\test-loggych.js:33:23)\n' +
// ' at Object.next (D:\\js-projects\\loggych\\js\\scripts\\test-loggych.js:14:53)\n' +
// ' at fulfilled (D:\\js-projects\\loggych\\js\\scripts\\test-loggych.js:5:58)\n' +
// ' at processTicksAndRejections (internal/process/task_queues.js:97:5)'
//}HtmlElemLoggych
//Logger, logging into innerHtml of html elem
export declare class HtmlElemLoggych implements ILoggych
{
constructor(elem: HTMLElement, conf: LoggychConf, context: any[]);
log(vals: Partial<Record<keyof LoggychConf, any>>): Promise<void>;
logCustom(vals: Record<string, any>): Promise<void>;
switchConf(keys: LoggychConf): ILoggych;
addContext(...any: any[]): ILoggych;
static createDefault(elem: HTMLElement, debug?: boolean, context?: any[]): HtmlElemLoggych;
}
ComposeLoggych
Loggych, that logs message into mutiple other loggyches
export type ComposeLoggychConf = {
info: ILoggych[];
debig: ILoggych[];
err: Iloggych[];
warn: ILoggych[];
custom: Iloggych[];
}
/**
* Loggych, that logs message into mutiple other loggyches
*/
export declare class ComposeLoggych implements ILoggych
{
constructor(conf: ComposeLoggychConf, context?: any[]);
addContext(...any: any[]): ILoggych;
switchConf(keys: LoggychConf): ILoggych;
log(vals: Partial<Record<keyof LoggychConf, any>>): Promise<void>;
logCustom(vals: Record<string, any>): Promise<void>;
static makeAllLogsForAllMsgs(logs: ILoggych[], context?: any[]): ILoggych;
}Author
Anatoly Starodubtsev [email protected]
License
MIT
