@larvit/log 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
package/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- declare type Metadata = {
1
+ export type Metadata = {
2
2
  [key: string]: string;
3
3
  };
4
- declare type LogShorthand = (msg: string, metadata?: Metadata) => void;
5
- interface LogInt {
4
+ export type LogShorthand = (msg: string, metadata?: Metadata) => void;
5
+ export interface LogInt {
6
6
  error: LogShorthand;
7
7
  warn: LogShorthand;
8
8
  info: LogShorthand;
@@ -10,13 +10,13 @@ interface LogInt {
10
10
  debug: LogShorthand;
11
11
  silly: LogShorthand;
12
12
  }
13
- declare type LogLevel = keyof LogInt;
14
- declare type EntryFormatterConf = {
13
+ export type LogLevel = keyof LogInt;
14
+ export type EntryFormatterConf = {
15
15
  logLevel: LogLevel;
16
16
  metadata?: Metadata;
17
17
  msg: string;
18
18
  };
19
- declare type LogConf = {
19
+ export type LogConf = {
20
20
  context?: Metadata;
21
21
  entryFormatter?: (conf: EntryFormatterConf) => string;
22
22
  format?: "text" | "json";
@@ -30,6 +30,7 @@ export declare class Log implements LogInt {
30
30
  #private;
31
31
  context: Metadata;
32
32
  constructor(conf?: LogConf | LogLevel | "none");
33
+ clone(conf?: LogConf | LogLevel | "none"): Log;
33
34
  error(msg: string, metadata?: Metadata): void;
34
35
  warn(msg: string, metadata?: Metadata): void;
35
36
  info(msg: string, metadata?: Metadata): void;
@@ -37,4 +38,3 @@ export declare class Log implements LogInt {
37
38
  debug(msg: string, metadata?: Metadata): void;
38
39
  silly(msg: string, metadata?: Metadata): void;
39
40
  }
40
- export {};
package/index.js CHANGED
@@ -1 +1 @@
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="sil"}else if(conf.logLevel==="debug"){levelOut="deb"}else if(conf.logLevel==="verbose"){levelOut="ver"}else if(conf.logLevel==="info"){levelOut="inf"}else if(conf.logLevel==="warn"){levelOut="war"}else if(conf.logLevel==="error"){levelOut="err"}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}))}}
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="sil"}else if(conf.logLevel==="debug"){levelOut="deb"}else if(conf.logLevel==="verbose"){levelOut="ver"}else if(conf.logLevel==="info"){levelOut="inf"}else if(conf.logLevel==="warn"){levelOut="war"}else if(conf.logLevel==="error"){levelOut="err"}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}))}}
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAkCA,MAAM,UAAU,gBAAgB,CAAC,IAAwB;IACxD,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE;QAC5C,QAAQ,EAAE,IAAI,CAAC,QAAQ;QACvB,GAAG,EAAE,IAAI,CAAC,GAAG;QACb,IAAI,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;KAC9B,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;AAChC,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,IAAwB;IACxD,IAAI,QAAQ,GAAG,EAAE,CAAC;IAElB,IAAI,IAAI,CAAC,QAAQ,KAAK,OAAO,EAAE;QAC9B,QAAQ,GAAG,sBAAsB,CAAC;KAClC;SAAM,IAAI,IAAI,CAAC,QAAQ,KAAK,OAAO,EAAE;QACrC,QAAQ,GAAG,sBAAsB,C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AC,SAAS,CAAC;YAAE,OAAO;QAC3F,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,eAAe,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,QAAQ,IAAI,EAAE,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;IACvH,CAAC;CACD"}
package/index.ts CHANGED
@@ -72,6 +72,7 @@ export function msgTextFormatter(conf: EntryFormatterConf) {
72
72
 
73
73
  export class Log implements LogInt {
74
74
  context: Metadata;
75
+ readonly #conf: LogConf; // Saved to be able to recreate instance
75
76
  readonly #logLevel: LogLevel | "none";
76
77
  readonly #entryFormatter: (conf: EntryFormatterConf) => string;
77
78
  readonly #stderr: (msg: string) => void;
@@ -102,6 +103,7 @@ export class Log implements LogInt {
102
103
  conf.stdout = console.log;
103
104
  }
104
105
 
106
+ this.#conf = conf;
105
107
  this.#logLevel = conf.logLevel;
106
108
  this.#entryFormatter = conf.entryFormatter;
107
109
  this.#stderr = conf.stderr;
@@ -109,6 +111,41 @@ export class Log implements LogInt {
109
111
  this.context = conf.context || {};
110
112
  }
111
113
 
114
+ // Create a new instance based on the current instance
115
+ // All options sent in will override the current instance settings
116
+ clone(conf?: LogConf | LogLevel | "none") {
117
+ if (conf === undefined) {
118
+ conf = {};
119
+ } else if (typeof conf === "string") {
120
+ conf = { logLevel: conf };
121
+ }
122
+
123
+ if (conf.logLevel === undefined) {
124
+ conf.logLevel = this.#logLevel;
125
+ }
126
+
127
+ if (this.#conf.format !== "json" && conf.format === "json") {
128
+ conf.entryFormatter = msgJsonFormatter;
129
+ } else {
130
+ conf.entryFormatter = this.#entryFormatter;
131
+ }
132
+
133
+ if (conf.stderr === undefined) {
134
+ conf.stderr = this.#conf.stderr;
135
+ }
136
+
137
+ if (conf.stdout === undefined) {
138
+ conf.stdout = this.#conf.stdout;
139
+ }
140
+
141
+ conf.context = {
142
+ ...this.context,
143
+ ...conf.context,
144
+ };
145
+
146
+ return new Log(conf);
147
+ }
148
+
112
149
  error(msg: string, metadata?: Metadata) {
113
150
  if (this.#logLevel === "none") return;
114
151
  this.#stderr(this.#entryFormatter({ logLevel: "error", metadata: Object.assign(metadata || {}, this.context), msg }));
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@larvit/log",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
- "packageManager": "yarn@3.2.4",
6
+ "packageManager": "yarn@3.3.1",
7
7
  "scripts": {
8
8
  "build": "rm -f index.js index.d.ts index.js.map && tsc && uglifyjs index.js -o index.js",
9
9
  "lint-fix": "eslint --fix index.ts test.ts",
@@ -12,15 +12,15 @@
12
12
  "test": "yarn test-unit && yarn lint"
13
13
  },
14
14
  "devDependencies": {
15
- "@larvit/eslint-config-typescript-esm": "1.0.1",
16
- "@randomgoods/tap-spec": "5.0.2",
17
- "@types/node": "18.7.20",
15
+ "@larvit/eslint-config-typescript-esm": "1.2.0",
16
+ "@randomgoods/tap-spec": "5.0.3",
17
+ "@types/node": "18.11.18",
18
18
  "@types/tape": "4.13.2",
19
- "eslint": "8.25.0",
19
+ "eslint": "8.31.0",
20
20
  "tape": "5.6.1",
21
21
  "ts-node": "10.9.1",
22
- "typescript": "4.8.4",
23
- "uglify-js": "3.17.3"
22
+ "typescript": "4.9.4",
23
+ "uglify-js": "3.17.4"
24
24
  },
25
25
  "publishConfig": {
26
26
  "access": "public",