@larvit/log
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -1,5 +0,5 @@ | ||
export declare type Metadata = { | ||
export type Metadata = { | ||
[key: string]: string; | ||
}; | ||
export declare type LogShorthand = (msg: string, metadata?: Metadata) => void; | ||
export type LogShorthand = (msg: string, metadata?: Metadata) => void; | ||
export interface LogInt { | ||
@@ -13,4 +13,4 @@ error: LogShorthand; | ||
} | ||
export declare type LogLevel = keyof LogInt; | ||
export declare type EntryFormatterConf = { | ||
export type LogLevel = keyof LogInt; | ||
export type EntryFormatterConf = { | ||
logLevel: LogLevel; | ||
@@ -20,3 +20,3 @@ metadata?: Metadata; | ||
}; | ||
export declare type LogConf = { | ||
export type LogConf = { | ||
context?: Metadata; | ||
@@ -35,2 +35,3 @@ entryFormatter?: (conf: EntryFormatterConf) => string; | ||
constructor(conf?: LogConf | LogLevel | "none"); | ||
clone(conf?: LogConf | LogLevel | "none"): Log; | ||
error(msg: string, metadata?: Metadata): void; | ||
@@ -37,0 +38,0 @@ warn(msg: string, metadata?: Metadata): void; |
@@ -1,1 +0,1 @@ | ||
export function msgJsonFormatter(conf){const payload=Object.assign(conf.metadata,{logLevel:conf.logLevel,msg:conf.msg,time:(new Date).toISOString()});return JSON.stringify(payload)}export function msgTextFormatter(conf){let levelOut="";if(conf.logLevel==="silly"){levelOut="[1;37msil[0m"}else if(conf.logLevel==="debug"){levelOut="[1;35mdeb[0m"}else if(conf.logLevel==="verbose"){levelOut="[1;34mver[0m"}else if(conf.logLevel==="info"){levelOut="[1;32minf[0m"}else if(conf.logLevel==="warn"){levelOut="[1;33mwar[0m"}else if(conf.logLevel==="error"){levelOut="[1;31merr[0m"}else{throw new Error(`Invalid conf.logLevel: "${conf.logLevel}"`)}let str=`${(new Date).toISOString().substring(0,19)}Z [${levelOut}] ${conf.msg}`;const metadataStr=JSON.stringify(conf.metadata);if(metadataStr!=="{}"){str+=` ${JSON.stringify(conf.metadata)}`}return str}export class Log{context;#logLevel;#entryFormatter;#stderr;#stdout;constructor(conf){if(conf===undefined){conf={}}else if(typeof conf==="string"){conf={logLevel:conf}}if(conf.logLevel===undefined){conf.logLevel="info"}if(conf.entryFormatter===undefined&&conf.format==="json"){conf.entryFormatter=msgJsonFormatter}else if(conf.entryFormatter===undefined){conf.entryFormatter=msgTextFormatter}if(conf.stderr===undefined){conf.stderr=console.error}if(conf.stdout===undefined){conf.stdout=console.log}this.#logLevel=conf.logLevel;this.#entryFormatter=conf.entryFormatter;this.#stderr=conf.stderr;this.#stdout=conf.stdout;this.context=conf.context||{}}error(msg,metadata){if(this.#logLevel==="none")return;this.#stderr(this.#entryFormatter({logLevel:"error",metadata:Object.assign(metadata||{},this.context),msg:msg}))}warn(msg,metadata){if(["none","error"].includes(this.#logLevel))return;this.#stderr(this.#entryFormatter({logLevel:"warn",metadata:Object.assign(metadata||{},this.context),msg:msg}))}info(msg,metadata){if(["none","error","warn"].includes(this.#logLevel))return;this.#stdout(this.#entryFormatter({logLevel:"info",metadata:Object.assign(metadata||{},this.context),msg:msg}))}verbose(msg,metadata){if(["none","error","warn","info"].includes(this.#logLevel))return;this.#stdout(this.#entryFormatter({logLevel:"verbose",metadata:Object.assign(metadata||{},this.context),msg:msg}))}debug(msg,metadata){if(["none","error","warn","info","verbose"].includes(this.#logLevel))return;this.#stdout(this.#entryFormatter({logLevel:"debug",metadata:Object.assign(metadata||{},this.context),msg:msg}))}silly(msg,metadata){if(["none","error","warn","info","verbose","debug"].includes(this.#logLevel))return;this.#stdout(this.#entryFormatter({logLevel:"silly",metadata:Object.assign(metadata||{},this.context),msg:msg}))}} | ||
export function msgJsonFormatter(conf){const payload=Object.assign(conf.metadata,{logLevel:conf.logLevel,msg:conf.msg,time:(new Date).toISOString()});return JSON.stringify(payload)}export function msgTextFormatter(conf){let levelOut="";if(conf.logLevel==="silly"){levelOut="[1;37msil[0m"}else if(conf.logLevel==="debug"){levelOut="[1;35mdeb[0m"}else if(conf.logLevel==="verbose"){levelOut="[1;34mver[0m"}else if(conf.logLevel==="info"){levelOut="[1;32minf[0m"}else if(conf.logLevel==="warn"){levelOut="[1;33mwar[0m"}else if(conf.logLevel==="error"){levelOut="[1;31merr[0m"}else{throw new Error(`Invalid conf.logLevel: "${conf.logLevel}"`)}let str=`${(new Date).toISOString().substring(0,19)}Z [${levelOut}] ${conf.msg}`;const metadataStr=JSON.stringify(conf.metadata);if(metadataStr!=="{}"){str+=` ${JSON.stringify(conf.metadata)}`}return str}export class Log{context;#conf;#logLevel;#entryFormatter;#stderr;#stdout;constructor(conf){if(conf===undefined){conf={}}else if(typeof conf==="string"){conf={logLevel:conf}}if(conf.logLevel===undefined){conf.logLevel="info"}if(conf.entryFormatter===undefined&&conf.format==="json"){conf.entryFormatter=msgJsonFormatter}else if(conf.entryFormatter===undefined){conf.entryFormatter=msgTextFormatter}if(conf.stderr===undefined){conf.stderr=console.error}if(conf.stdout===undefined){conf.stdout=console.log}this.#conf=conf;this.#logLevel=conf.logLevel;this.#entryFormatter=conf.entryFormatter;this.#stderr=conf.stderr;this.#stdout=conf.stdout;this.context=conf.context||{}}clone(conf){if(conf===undefined){conf={}}else if(typeof conf==="string"){conf={logLevel:conf}}if(conf.logLevel===undefined){conf.logLevel=this.#logLevel}if(this.#conf.format!=="json"&&conf.format==="json"){conf.entryFormatter=msgJsonFormatter}else{conf.entryFormatter=this.#entryFormatter}if(conf.stderr===undefined){conf.stderr=this.#conf.stderr}if(conf.stdout===undefined){conf.stdout=this.#conf.stdout}conf.context={...this.context,...conf.context};return new Log(conf)}error(msg,metadata){if(this.#logLevel==="none")return;this.#stderr(this.#entryFormatter({logLevel:"error",metadata:Object.assign(metadata||{},this.context),msg:msg}))}warn(msg,metadata){if(["none","error"].includes(this.#logLevel))return;this.#stderr(this.#entryFormatter({logLevel:"warn",metadata:Object.assign(metadata||{},this.context),msg:msg}))}info(msg,metadata){if(["none","error","warn"].includes(this.#logLevel))return;this.#stdout(this.#entryFormatter({logLevel:"info",metadata:Object.assign(metadata||{},this.context),msg:msg}))}verbose(msg,metadata){if(["none","error","warn","info"].includes(this.#logLevel))return;this.#stdout(this.#entryFormatter({logLevel:"verbose",metadata:Object.assign(metadata||{},this.context),msg:msg}))}debug(msg,metadata){if(["none","error","warn","info","verbose"].includes(this.#logLevel))return;this.#stdout(this.#entryFormatter({logLevel:"debug",metadata:Object.assign(metadata||{},this.context),msg:msg}))}silly(msg,metadata){if(["none","error","warn","info","verbose","debug"].includes(this.#logLevel))return;this.#stdout(this.#entryFormatter({logLevel:"silly",metadata:Object.assign(metadata||{},this.context),msg:msg}))}} |
37
index.ts
@@ -75,2 +75,3 @@ export type Metadata = { | ||
context: Metadata; | ||
readonly #conf: LogConf; // Saved to be able to recreate instance | ||
readonly #logLevel: LogLevel | "none"; | ||
@@ -106,2 +107,3 @@ readonly #entryFormatter: (conf: EntryFormatterConf) => string; | ||
this.#conf = conf; | ||
this.#logLevel = conf.logLevel; | ||
@@ -114,2 +116,37 @@ this.#entryFormatter = conf.entryFormatter; | ||
// Create a new instance based on the current instance | ||
// All options sent in will override the current instance settings | ||
clone(conf?: LogConf | LogLevel | "none") { | ||
if (conf === undefined) { | ||
conf = {}; | ||
} else if (typeof conf === "string") { | ||
conf = { logLevel: conf }; | ||
} | ||
if (conf.logLevel === undefined) { | ||
conf.logLevel = this.#logLevel; | ||
} | ||
if (this.#conf.format !== "json" && conf.format === "json") { | ||
conf.entryFormatter = msgJsonFormatter; | ||
} else { | ||
conf.entryFormatter = this.#entryFormatter; | ||
} | ||
if (conf.stderr === undefined) { | ||
conf.stderr = this.#conf.stderr; | ||
} | ||
if (conf.stdout === undefined) { | ||
conf.stdout = this.#conf.stdout; | ||
} | ||
conf.context = { | ||
...this.context, | ||
...conf.context, | ||
}; | ||
return new Log(conf); | ||
} | ||
error(msg: string, metadata?: Metadata) { | ||
@@ -116,0 +153,0 @@ if (this.#logLevel === "none") return; |
{ | ||
"name": "@larvit/log", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"type": "module", | ||
"license": "MIT", | ||
"packageManager": "yarn@3.2.4", | ||
"packageManager": "yarn@3.3.1", | ||
"scripts": { | ||
@@ -15,11 +15,11 @@ "build": "rm -f index.js index.d.ts index.js.map && tsc && uglifyjs index.js -o index.js", | ||
"devDependencies": { | ||
"@larvit/eslint-config-typescript-esm": "1.0.1", | ||
"@randomgoods/tap-spec": "5.0.2", | ||
"@types/node": "18.7.20", | ||
"@larvit/eslint-config-typescript-esm": "1.2.0", | ||
"@randomgoods/tap-spec": "5.0.3", | ||
"@types/node": "18.11.18", | ||
"@types/tape": "4.13.2", | ||
"eslint": "8.25.0", | ||
"eslint": "8.31.0", | ||
"tape": "5.6.1", | ||
"ts-node": "10.9.1", | ||
"typescript": "4.8.4", | ||
"uglify-js": "3.17.3" | ||
"typescript": "4.9.4", | ||
"uglify-js": "3.17.4" | ||
}, | ||
@@ -26,0 +26,0 @@ "publishConfig": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18456
187