@larvit/log 1.1.1 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
package/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- export declare type Metadata = {
1
+ export type Metadata = {
2
2
  [key: string]: string;
3
3
  };
4
- export declare type LogShorthand = (msg: string, metadata?: Metadata) => void;
4
+ export type LogShorthand = (msg: string, metadata?: Metadata) => void;
5
5
  export interface LogInt {
6
6
  error: LogShorthand;
7
7
  warn: LogShorthand;
@@ -10,13 +10,13 @@ export interface LogInt {
10
10
  debug: LogShorthand;
11
11
  silly: LogShorthand;
12
12
  }
13
- export declare type LogLevel = keyof LogInt;
14
- export 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
- export 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;
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,CAAC;KAClC;SAAM,IAAI,IAAI,CAAC,QAAQ,KAAK,SAAS,EAAE;QACvC,QAAQ,GAAG,sBAAsB,CAAC;KAClC;SAAM,IAAI,IAAI,CAAC,QAAQ,KAAK,M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,MAAM,CAAC,QAAQ,IAAI,EAAE,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;IACvH,CAAC;CACD"}
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@larvit/log",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
- "packageManager": "yarn@3.2.4",
6
+ "packageManager": "yarn@3.6.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",
18
- "@types/tape": "4.13.2",
19
- "eslint": "8.25.0",
20
- "tape": "5.6.1",
15
+ "@larvit/eslint-config-typescript-esm": "1.2.0",
16
+ "@randomgoods/tap-spec": "5.0.3",
17
+ "@types/node": "18.16.19",
18
+ "@types/tape": "5.6.0",
19
+ "eslint": "8.44.0",
20
+ "tape": "5.6.3",
21
21
  "ts-node": "10.9.1",
22
- "typescript": "4.8.4",
23
- "uglify-js": "3.17.3"
22
+ "typescript": "5.1.6",
23
+ "uglify-js": "3.17.4"
24
24
  },
25
25
  "publishConfig": {
26
26
  "access": "public",
@@ -35,7 +35,6 @@
35
35
  "index.d.ts",
36
36
  "index.js.map",
37
37
  "index.js",
38
- "index.ts",
39
38
  "LICENSE",
40
39
  "package.json",
41
40
  "README.md"
package/index.ts DELETED
@@ -1,141 +0,0 @@
1
- export type Metadata = {
2
- [key: string]: string;
3
- }
4
-
5
- export type LogShorthand = (msg: string, metadata?: Metadata) => void;
6
-
7
- export interface LogInt {
8
- /* eslint-disable typescript-sort-keys/interface */
9
- error: LogShorthand;
10
- warn: LogShorthand;
11
- info: LogShorthand;
12
- verbose: LogShorthand;
13
- debug: LogShorthand;
14
- silly: LogShorthand
15
- /* eslint-enable typescript-sort-keys/interface */
16
- }
17
-
18
- export type LogLevel = keyof LogInt;
19
-
20
- export type EntryFormatterConf = {
21
- logLevel: LogLevel;
22
- metadata?: Metadata;
23
- msg: string;
24
- }
25
-
26
- export type LogConf = {
27
- context?: Metadata;
28
- entryFormatter?: (conf: EntryFormatterConf) => string;
29
- format?: "text" | "json";
30
- logLevel?: LogLevel | "none";
31
- stderr?: (msg: string) => void;
32
- stdout?: (msg: string) => void;
33
- }
34
-
35
- export function msgJsonFormatter(conf: EntryFormatterConf) {
36
- const payload = Object.assign(conf.metadata, {
37
- logLevel: conf.logLevel,
38
- msg: conf.msg,
39
- time: new Date().toISOString(),
40
- });
41
-
42
- return JSON.stringify(payload);
43
- }
44
-
45
- export function msgTextFormatter(conf: EntryFormatterConf) {
46
- let levelOut = "";
47
-
48
- if (conf.logLevel === "silly") {
49
- levelOut = "\x1b[1;37msil\x1b[0m";
50
- } else if (conf.logLevel === "debug") {
51
- levelOut = "\x1b[1;35mdeb\x1b[0m";
52
- } else if (conf.logLevel === "verbose") {
53
- levelOut = "\x1b[1;34mver\x1b[0m";
54
- } else if (conf.logLevel === "info") {
55
- levelOut = "\x1b[1;32minf\x1b[0m";
56
- } else if (conf.logLevel === "warn") {
57
- levelOut = "\x1b[1;33mwar\x1b[0m";
58
- } else if (conf.logLevel === "error") {
59
- levelOut = "\x1b[1;31merr\x1b[0m";
60
- } else {
61
- throw new Error(`Invalid conf.logLevel: "${conf.logLevel}"`);
62
- }
63
-
64
- let str = `${new Date().toISOString().substring(0, 19)}Z [${levelOut}] ${conf.msg}`;
65
- const metadataStr = JSON.stringify(conf.metadata);
66
- if (metadataStr !== "{}") {
67
- str += ` ${JSON.stringify(conf.metadata)}`;
68
- }
69
-
70
- return str;
71
- }
72
-
73
- export class Log implements LogInt {
74
- context: Metadata;
75
- readonly #logLevel: LogLevel | "none";
76
- readonly #entryFormatter: (conf: EntryFormatterConf) => string;
77
- readonly #stderr: (msg: string) => void;
78
- readonly #stdout: (msg: string) => void;
79
-
80
- constructor(conf?: LogConf | LogLevel | "none") {
81
- if (conf === undefined) {
82
- conf = {};
83
- } else if (typeof conf === "string") {
84
- conf = { logLevel: conf };
85
- }
86
-
87
- if (conf.logLevel === undefined) {
88
- conf.logLevel = "info";
89
- }
90
-
91
- if (conf.entryFormatter === undefined && conf.format === "json") {
92
- conf.entryFormatter = msgJsonFormatter;
93
- } else if (conf.entryFormatter === undefined) {
94
- conf.entryFormatter = msgTextFormatter;
95
- }
96
-
97
- if (conf.stderr === undefined) {
98
- conf.stderr = console.error;
99
- }
100
-
101
- if (conf.stdout === undefined) {
102
- conf.stdout = console.log;
103
- }
104
-
105
- this.#logLevel = conf.logLevel;
106
- this.#entryFormatter = conf.entryFormatter;
107
- this.#stderr = conf.stderr;
108
- this.#stdout = conf.stdout;
109
- this.context = conf.context || {};
110
- }
111
-
112
- error(msg: string, metadata?: Metadata) {
113
- if (this.#logLevel === "none") return;
114
- this.#stderr(this.#entryFormatter({ logLevel: "error", metadata: Object.assign(metadata || {}, this.context), msg }));
115
- }
116
-
117
- warn(msg: string, metadata?: Metadata) {
118
- if (["none", "error"].includes(this.#logLevel)) return;
119
- this.#stderr(this.#entryFormatter({ logLevel: "warn", metadata: Object.assign(metadata || {}, this.context), msg }));
120
- }
121
-
122
- info(msg: string, metadata?: Metadata) {
123
- if (["none", "error", "warn"].includes(this.#logLevel)) return;
124
- this.#stdout(this.#entryFormatter({ logLevel: "info", metadata: Object.assign(metadata || {}, this.context), msg }));
125
- }
126
-
127
- verbose(msg: string, metadata?: Metadata) {
128
- if (["none", "error", "warn", "info"].includes(this.#logLevel)) return;
129
- this.#stdout(this.#entryFormatter({ logLevel: "verbose", metadata: Object.assign(metadata || {}, this.context), msg }));
130
- }
131
-
132
- debug(msg: string, metadata?: Metadata) {
133
- if (["none", "error", "warn", "info", "verbose"].includes(this.#logLevel)) return;
134
- this.#stdout(this.#entryFormatter({ logLevel: "debug", metadata: Object.assign(metadata || {}, this.context), msg }));
135
- }
136
-
137
- silly(msg: string, metadata?: Metadata) {
138
- if (["none", "error", "warn", "info", "verbose", "debug"].includes(this.#logLevel)) return;
139
- this.#stdout(this.#entryFormatter({ logLevel: "silly", metadata: Object.assign(metadata || {}, this.context), msg }));
140
- }
141
- }