@larvit/log 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +8 -9
  2. package/index.ts +0 -178
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@larvit/log",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
- "packageManager": "yarn@3.3.1",
6
+ "packageManager": "yarn@3.6.3",
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,14 +12,14 @@
12
12
  "test": "yarn test-unit && yarn lint"
13
13
  },
14
14
  "devDependencies": {
15
- "@larvit/eslint-config-typescript-esm": "1.2.0",
15
+ "@larvit/eslint-config-typescript-esm": "1.2.1",
16
16
  "@randomgoods/tap-spec": "5.0.3",
17
- "@types/node": "18.11.18",
18
- "@types/tape": "4.13.2",
19
- "eslint": "8.31.0",
20
- "tape": "5.6.1",
17
+ "@types/node": "20.5.7",
18
+ "@types/tape": "5.6.0",
19
+ "eslint": "8.48.0",
20
+ "tape": "5.6.6",
21
21
  "ts-node": "10.9.1",
22
- "typescript": "4.9.4",
22
+ "typescript": "5.2.2",
23
23
  "uglify-js": "3.17.4"
24
24
  },
25
25
  "publishConfig": {
@@ -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,178 +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 #conf: LogConf; // Saved to be able to recreate instance
76
- readonly #logLevel: LogLevel | "none";
77
- readonly #entryFormatter: (conf: EntryFormatterConf) => string;
78
- readonly #stderr: (msg: string) => void;
79
- readonly #stdout: (msg: string) => void;
80
-
81
- constructor(conf?: LogConf | LogLevel | "none") {
82
- if (conf === undefined) {
83
- conf = {};
84
- } else if (typeof conf === "string") {
85
- conf = { logLevel: conf };
86
- }
87
-
88
- if (conf.logLevel === undefined) {
89
- conf.logLevel = "info";
90
- }
91
-
92
- if (conf.entryFormatter === undefined && conf.format === "json") {
93
- conf.entryFormatter = msgJsonFormatter;
94
- } else if (conf.entryFormatter === undefined) {
95
- conf.entryFormatter = msgTextFormatter;
96
- }
97
-
98
- if (conf.stderr === undefined) {
99
- conf.stderr = console.error;
100
- }
101
-
102
- if (conf.stdout === undefined) {
103
- conf.stdout = console.log;
104
- }
105
-
106
- this.#conf = conf;
107
- this.#logLevel = conf.logLevel;
108
- this.#entryFormatter = conf.entryFormatter;
109
- this.#stderr = conf.stderr;
110
- this.#stdout = conf.stdout;
111
- this.context = conf.context || {};
112
- }
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
-
149
- error(msg: string, metadata?: Metadata) {
150
- if (this.#logLevel === "none") return;
151
- this.#stderr(this.#entryFormatter({ logLevel: "error", metadata: Object.assign(metadata || {}, this.context), msg }));
152
- }
153
-
154
- warn(msg: string, metadata?: Metadata) {
155
- if (["none", "error"].includes(this.#logLevel)) return;
156
- this.#stderr(this.#entryFormatter({ logLevel: "warn", metadata: Object.assign(metadata || {}, this.context), msg }));
157
- }
158
-
159
- info(msg: string, metadata?: Metadata) {
160
- if (["none", "error", "warn"].includes(this.#logLevel)) return;
161
- this.#stdout(this.#entryFormatter({ logLevel: "info", metadata: Object.assign(metadata || {}, this.context), msg }));
162
- }
163
-
164
- verbose(msg: string, metadata?: Metadata) {
165
- if (["none", "error", "warn", "info"].includes(this.#logLevel)) return;
166
- this.#stdout(this.#entryFormatter({ logLevel: "verbose", metadata: Object.assign(metadata || {}, this.context), msg }));
167
- }
168
-
169
- debug(msg: string, metadata?: Metadata) {
170
- if (["none", "error", "warn", "info", "verbose"].includes(this.#logLevel)) return;
171
- this.#stdout(this.#entryFormatter({ logLevel: "debug", metadata: Object.assign(metadata || {}, this.context), msg }));
172
- }
173
-
174
- silly(msg: string, metadata?: Metadata) {
175
- if (["none", "error", "warn", "info", "verbose", "debug"].includes(this.#logLevel)) return;
176
- this.#stdout(this.#entryFormatter({ logLevel: "silly", metadata: Object.assign(metadata || {}, this.context), msg }));
177
- }
178
- }