@larvit/log 1.0.2 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -58,4 +58,4 @@ const log = new Log({
58
58
 
59
59
  ### Metadata
60
60
 
61
- `log.info("foo", { hey: "luring" });` --> 2022-09-24T23:40:39Z [info] foo {"hey":"luring"}
61
+ `log.info("foo", { hey: "luring" });` --> 2022-09-24T23:40:39Z [info] foo {"hey":"luring"}
package/index.d.ts ADDED
@@ -0,0 +1,40 @@
1
+ declare type Metadata = {
2
+ [key: string]: string;
3
+ };
4
+ declare type LogShorthand = (msg: string, metadata?: Metadata) => void;
5
+ interface LogInt {
6
+ error: LogShorthand;
7
+ warn: LogShorthand;
8
+ info: LogShorthand;
9
+ verbose: LogShorthand;
10
+ debug: LogShorthand;
11
+ silly: LogShorthand;
12
+ }
13
+ declare type LogLevel = keyof LogInt;
14
+ declare type EntryFormatterConf = {
15
+ logLevel: LogLevel;
16
+ metadata?: Metadata;
17
+ msg: string;
18
+ };
19
+ declare type LogConf = {
20
+ context?: Metadata;
21
+ entryFormatter?: (conf: EntryFormatterConf) => string;
22
+ format?: "text" | "json";
23
+ logLevel?: LogLevel | "none";
24
+ stderr?: (msg: string) => void;
25
+ stdout?: (msg: string) => void;
26
+ };
27
+ export declare function msgJsonFormatter(conf: EntryFormatterConf): string;
28
+ export declare function msgTextFormatter(conf: EntryFormatterConf): string;
29
+ export declare class Log implements LogInt {
30
+ #private;
31
+ context: Metadata;
32
+ constructor(conf?: LogConf | LogLevel | "none");
33
+ error(msg: string, metadata?: Metadata): void;
34
+ warn(msg: string, metadata?: Metadata): void;
35
+ info(msg: string, metadata?: Metadata): void;
36
+ verbose(msg: string, metadata?: Metadata): void;
37
+ debug(msg: string, metadata?: Metadata): void;
38
+ silly(msg: string, metadata?: Metadata): void;
39
+ }
40
+ export {};
package/index.js ADDED
@@ -0,0 +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}))}}
package/index.js.map ADDED
@@ -0,0 +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,MAAM,EAAE;QACpC,QAAQ,GAAG,sBAAsB,CAAC;KAClC;SAAM,IAAI,IAAI,CAAC,QAAQ,KAAK,MAAM,EAAE;QACpC,QAAQ,GAAG,sBAAsB,CAAC;KAClC;SAAM,IAAI,IAAI,CAAC,QAAQ,KAAK,OAAO,EAAE;QACrC,QAAQ,GAAG,sBAAsB,CAAC;KAClC;SAAM;QACN,MAAM,IAAI,KAAK,C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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 ADDED
@@ -0,0 +1,141 @@
1
+ type Metadata = {
2
+ [key: string]: string;
3
+ }
4
+
5
+ type LogShorthand = (msg: string, metadata?: Metadata) => void;
6
+
7
+ 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
+ type LogLevel = keyof LogInt;
19
+
20
+ type EntryFormatterConf = {
21
+ logLevel: LogLevel;
22
+ metadata?: Metadata;
23
+ msg: string;
24
+ }
25
+
26
+ 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
+ }
package/package.json CHANGED
@@ -1,21 +1,26 @@
1
1
  {
2
2
  "name": "@larvit/log",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
- "packageManager": "yarn@3.2.3",
6
+ "packageManager": "yarn@3.2.4",
7
7
  "scripts": {
8
8
  "build": "rm -f index.js index.d.ts index.js.map && tsc && uglifyjs index.js -o index.js",
9
- "test": "node --loader ts-node/esm test.ts | tap-spec"
9
+ "lint-fix": "eslint --fix index.ts test.ts",
10
+ "lint": "eslint index.ts test.ts",
11
+ "test-unit": "node --loader ts-node/esm test.ts | tap-spec",
12
+ "test": "yarn test-unit && yarn lint"
10
13
  },
11
14
  "devDependencies": {
15
+ "@larvit/eslint-config-typescript-esm": "1.0.1",
12
16
  "@randomgoods/tap-spec": "5.0.2",
13
17
  "@types/node": "18.7.20",
14
18
  "@types/tape": "4.13.2",
19
+ "eslint": "8.25.0",
15
20
  "tape": "5.6.1",
16
21
  "ts-node": "10.9.1",
17
22
  "typescript": "4.8.4",
18
- "uglify-js": "3.17.2"
23
+ "uglify-js": "3.17.3"
19
24
  },
20
25
  "publishConfig": {
21
26
  "access": "public",
@@ -24,5 +29,15 @@
24
29
  "repository": {
25
30
  "type": "git",
26
31
  "url": "git+https://github.com/larvit/log.git"
27
- }
32
+ },
33
+ "main": "index.js",
34
+ "files": [
35
+ "index.d.ts",
36
+ "index.js.map",
37
+ "index.js",
38
+ "index.ts",
39
+ "LICENSE",
40
+ "package.json",
41
+ "README.md"
42
+ ]
28
43
  }