log-likho 2.0.1 → 3.0.0

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.
package/CONTRIBUTE.md CHANGED
@@ -12,5 +12,22 @@ Before creating any Pull requests please follow the guidelines.
12
12
 
13
13
  ## Steps to publish (For repo admins)
14
14
 
15
- 1. Update sem-ver in `package.json`
16
- 2. `npm publish` (2FA needed)
15
+ ```sh
16
+ # keep username, password and 2FA ready
17
+ npm login
18
+
19
+ # update Semver numbers: can user major or minor or patch
20
+ npm version minor
21
+ git push && git push --tags
22
+ npm publish
23
+
24
+ ```
25
+
26
+ ---
27
+ ## Testing
28
+
29
+ To test npm package install and compatibility, can run the bash script.
30
+ ```sh
31
+ chmod +x ./test/setup-log-likho-ts.sh
32
+ ./test/setup-log-likho-ts.sh
33
+ ```
package/README.md CHANGED
@@ -1,8 +1,11 @@
1
+
1
2
  # log-likho
2
- A console.log, that logs on console as well as on a file in `logs/` folder.
3
- Plus an advance logger with `INFO`, `WARN`, `ERROR` & `FATAL` modes.
3
+ A console.log, that logs on console as well as on a file in `logs/` folder.
4
+ Plus an advanced logger with `INFO`, `WARN`, `ERROR` & `FATAL` modes.
5
+
6
+ ---
4
7
 
5
- ### Simple Logger Usage
8
+ ## Simple Logger Usage
6
9
 
7
10
  ```sh
8
11
  npm install log-likho
@@ -16,17 +19,16 @@ console.log = require('log-likho')();
16
19
  console.log('testing 1');
17
20
  ```
18
21
 
19
- This should do normal log to console but also write logs real-time to the `logs/` folder
22
+ This should do normal log to console but also write logs real-time to the `logs/` folder.
20
23
 
21
24
  ##### Log in append mode
22
25
 
23
26
  ```js
24
- console.log = require('log-likho')({mode: 'a'});
27
+ console.log = require('log-likho')({ mode: 'a' });
25
28
 
26
29
  console.log('testing 1');
27
30
  ```
28
31
 
29
-
30
32
  ##### With all the options
31
33
 
32
34
  ```js
@@ -36,24 +38,24 @@ console.log = simple_logger({
36
38
  logs_folder: './my_folder',
37
39
  mode: "a",
38
40
  })
39
-
40
41
  ```
41
42
 
42
- This will create log files in `my_folder` and will not overwrite it with new logs on next executions.
43
-
43
+ This will create log files in `my_folder` and will not overwrite them with new logs on next executions.
44
44
 
45
+ ---
45
46
 
46
- ## Advance logger
47
+ ## Advanced Logger
47
48
 
48
49
  🚀 Features
49
50
 
50
- ✅ Supports info, warn, error, fatal, and log levels
51
-
52
- Colored terminal output for easy debugging
51
+ * ✅ Supports **info**, **warn**, **error**, **fatal**, and **log** levels
52
+ * ✅ **Multiple arguments & error objects** supported
53
+ * **Selective log levels to file** (choose which levels are written to disk)
54
+ * ✅ Colored terminal output for easy debugging
55
+ * ✅ Custom log folder and file mode (append/overwrite)
56
+ * ✅ No external dependencies
53
57
 
54
- ✅ Custom log folder and file mode (append/overwrite)
55
-
56
- ✅ No external dependencies
58
+ ---
57
59
 
58
60
  ### Usage
59
61
 
@@ -61,14 +63,14 @@ This will create log files in `my_folder` and will not overwrite it with new log
61
63
  npm install log-likho
62
64
  ```
63
65
 
64
- To use the logger.
66
+ To use the logger:
65
67
 
66
68
  ```js
67
69
  const { create_logger } = require("log-likho");
68
70
 
69
71
  const logger = create_logger({
70
72
  logs_folder: "./logs", // default: './logs'
71
- mode: "a" // append mode; use 'w' for overwrite
73
+ mode: "a", // append mode; use 'w' for overwrite
72
74
  });
73
75
 
74
76
  logger.info("This is an info message");
@@ -77,10 +79,47 @@ logger.error("This is an error");
77
79
  logger.fatal("This is a fatal error");
78
80
  logger.log("This is a plain log message");
79
81
  ```
80
-
81
- ##### Output
82
+ **Output**
82
83
 
83
84
  ![LoggerImage](./assets/image.png)
85
+ ---
86
+
87
+ ### 1. Multiple Arguments & Error Object Support
88
+
89
+ You can now log multiple values or an `Error` object in a single call:
90
+
91
+ ```js
92
+ const err = new Error("Database connection failed");
93
+
94
+ logger.error("Something went wrong:", err);
95
+ logger.info("User logged in:", { id: 42, name: "John Doe" });
96
+ logger.warn("Low disk space:", 512, "MB remaining");
97
+ ```
98
+
99
+ This works for **all log levels**.
100
+
101
+ ---
102
+
103
+ ### 2. Selective Log Levels to File
104
+
105
+ You can now configure **which log levels should be written to file** using the `log_levels_to_file` option.
106
+ All levels will still print to the console unless you handle them differently.
107
+
108
+ ```js
109
+ const logger = create_logger({
110
+ logs_folder: "./logs",
111
+ mode: "a",
112
+ log_levels_to_file: ["ERROR", "FATAL"], // Only write these to file
113
+ });
114
+
115
+ logger.info("This will appear in console only");
116
+ logger.error("This will appear in console + file");
117
+ logger.fatal("This will appear in console + file");
118
+ ```
119
+
120
+ ---
121
+
122
+
84
123
 
85
124
  | Level | Terminal Color |
86
125
  | ----- | ------------------ |
@@ -90,10 +129,37 @@ logger.log("This is a plain log message");
90
129
  | FATAL | Magenta |
91
130
  | LOG | Default (no color) |
92
131
 
132
+ ---
133
+
134
+ ## Default Options
135
+
136
+ | Option | Type | Default | Description |
137
+ | -------------------- | --------- | ------------------------------------------- | ------------------------------------------------------ |
138
+ | `logs_folder` | String | `./logs` | Directory to store log files |
139
+ | `mode` | String | `"a"` | File write mode: `"a"` for append, `"w"` for overwrite |
140
+ | `log_levels_to_file` | String\[] | `["INFO", "WARN", "ERROR", "FATAL", "LOG"]` | Which log levels should be written to file |
141
+
142
+ ---
143
+
144
+ ## TypeScript Compatible
93
145
 
94
- ## Default options
146
+ `log-likho` supports TypeScript out of the box.
95
147
 
96
- | Option | Type | Default | Description |
97
- | ------------- | ------ | -------- | ------------------------------------------------------ |
98
- | `logs_folder` | String | `./logs` | Directory to store log files |
99
- | `mode` | String | `"a"` | File write mode: `"a"` for append, `"w"` for overwrite |
148
+ * Includes `index.d.ts` for type safety.
149
+ * Works seamlessly with both JavaScript and TypeScript projects.
150
+ * No extra setup needed just import and use!
151
+
152
+ ```ts
153
+ import log, { create_logger } from 'log-likho';
154
+
155
+ const logger = create_logger({
156
+ logs_folder: "./logs",
157
+ log_levels_to_file: ["ERROR", "FATAL"]
158
+ });
159
+
160
+ logger.info("This is an info message");
161
+ logger.warn("This is a warning");
162
+ logger.error("This is an error");
163
+ logger.fatal("This is a fatal error");
164
+ logger.log("This is a plain log message");
165
+ ```
package/index.d.ts ADDED
@@ -0,0 +1,68 @@
1
+ // index.d.ts
2
+
3
+ declare module "log-likho" {
4
+ interface LoggerOptions {
5
+ /**
6
+ * Directory to store log files.
7
+ * @default "./logs"
8
+ */
9
+ logs_folder?: string;
10
+
11
+ /**
12
+ * File write mode.
13
+ * "a" = append, "w" = overwrite
14
+ * @default "a"
15
+ */
16
+ mode?: string;
17
+
18
+ /**
19
+ * Array of log levels that should be written to file.
20
+ * All levels will still print to console.
21
+ * @default ["INFO", "WARN", "ERROR", "FATAL", "LOG"]
22
+ */
23
+ log_levels_to_file?: Array<"INFO" | "WARN" | "ERROR" | "FATAL" | "LOG">;
24
+
25
+ [key: string]: any;
26
+ }
27
+
28
+ interface AdvancedLogger {
29
+ /**
30
+ * Info level logging
31
+ */
32
+ info: (...args: any[]) => void;
33
+
34
+ /**
35
+ * Warning level logging
36
+ */
37
+ warn: (...args: any[]) => void;
38
+
39
+ /**
40
+ * Error level logging
41
+ */
42
+ error: (...args: any[]) => void;
43
+
44
+ /**
45
+ * Fatal level logging
46
+ */
47
+ fatal: (...args: any[]) => void;
48
+
49
+ /**
50
+ * Generic logging (no color)
51
+ */
52
+ log: (...args: any[]) => void;
53
+ }
54
+
55
+ /**
56
+ * Basic logger that writes to stdout and a log file.
57
+ * Returns a simple log function.
58
+ */
59
+ function simple_logger(options?: LoggerOptions): (...args: any[]) => void;
60
+
61
+ /**
62
+ * Advanced logger with log levels (info, warn, error, fatal, log).
63
+ */
64
+ function create_logger(options?: LoggerOptions): AdvancedLogger;
65
+
66
+ export = simple_logger;
67
+ export { create_logger, LoggerOptions, AdvancedLogger };
68
+ }
@@ -0,0 +1,17 @@
1
+ [00:03:28] [INFO] This is an info message.
2
+ [00:03:28] [WARN] This is a warning.
3
+ [00:03:28] [ERROR] This is an error.
4
+ [00:03:28] [FATAL] This is a fatal error.
5
+ [00:03:28] [LOG] This is a generic log.
6
+ [00:03:28] [ERROR] Error occurred while connecting to DB: Error: Something went wrong
7
+ at Object.<anonymous> (/home/neet/work/open-source/log-likho/test/logger.spec.js:21:13)
8
+ at Module._compile (node:internal/modules/cjs/loader:1730:14)
9
+ at Object..js (node:internal/modules/cjs/loader:1895:10)
10
+ at Module.load (node:internal/modules/cjs/loader:1465:32)
11
+ at Function._load (node:internal/modules/cjs/loader:1282:12)
12
+ at TracingChannel.traceSync (node:diagnostics_channel:322:14)
13
+ at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)
14
+ at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5)
15
+ at node:internal/main/run_main_module:36:49
16
+ [00:03:28] [INFO] User login event { userId: 42, name: 'John Doe' }
17
+ [00:03:28] [WARN] Low disk space: 512 MB remaining
@@ -0,0 +1,2 @@
1
+ [00:03:28] [ERROR] This error SHOULD go to file
2
+ [00:03:28] [FATAL] This fatal error SHOULD go to file
@@ -0,0 +1,334 @@
1
+ [00:03:25]: testing 1
2
+ [00:03:25]: Process ID is : 1923736
3
+ [00:03:25]: Logging error TypeError: Assignment to constant variable.
4
+ at Object.<anonymous> (/home/neet/work/open-source/log-likho/test/default.spec.js:13:5)
5
+ at Module._compile (node:internal/modules/cjs/loader:1730:14)
6
+ at Object..js (node:internal/modules/cjs/loader:1895:10)
7
+ at Module.load (node:internal/modules/cjs/loader:1465:32)
8
+ at Function._load (node:internal/modules/cjs/loader:1282:12)
9
+ at TracingChannel.traceSync (node:diagnostics_channel:322:14)
10
+ at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)
11
+ at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5)
12
+ at node:internal/main/run_main_module:36:49
13
+ [00:03:25]: quick logs 1
14
+ [00:03:25]: quick logs 2
15
+ [00:03:25]: quick logs 3
16
+ [00:03:25]: quick logs 4
17
+ [00:03:25]: quick logs 5
18
+ [00:03:25]: quick logs 6
19
+ [00:03:25]: quick logs 7
20
+ [00:03:25]: quick logs 8
21
+ [00:03:25]: quick logs 9
22
+ [00:03:25]: quick logs 10
23
+ [00:03:25]: quick logs 11
24
+ [00:03:25]: quick logs 12
25
+ [00:03:25]: quick logs 13
26
+ [00:03:25]: quick logs 14
27
+ [00:03:25]: quick logs 15
28
+ [00:03:25]: quick logs 16
29
+ [00:03:25]: quick logs 17
30
+ [00:03:25]: quick logs 18
31
+ [00:03:25]: quick logs 19
32
+ [00:03:25]: quick logs 20
33
+ [00:03:25]: quick logs 21
34
+ [00:03:25]: quick logs 22
35
+ [00:03:25]: quick logs 23
36
+ [00:03:25]: quick logs 24
37
+ [00:03:25]: quick logs 25
38
+ [00:03:25]: quick logs 26
39
+ [00:03:25]: quick logs 27
40
+ [00:03:25]: quick logs 28
41
+ [00:03:25]: quick logs 29
42
+ [00:03:25]: quick logs 30
43
+ [00:03:25]: quick logs 31
44
+ [00:03:25]: quick logs 32
45
+ [00:03:25]: quick logs 33
46
+ [00:03:25]: quick logs 34
47
+ [00:03:25]: quick logs 35
48
+ [00:03:25]: quick logs 36
49
+ [00:03:25]: quick logs 37
50
+ [00:03:25]: quick logs 38
51
+ [00:03:25]: quick logs 39
52
+ [00:03:25]: quick logs 40
53
+ [00:03:25]: quick logs 41
54
+ [00:03:25]: quick logs 42
55
+ [00:03:25]: quick logs 43
56
+ [00:03:25]: quick logs 44
57
+ [00:03:25]: quick logs 45
58
+ [00:03:25]: quick logs 46
59
+ [00:03:25]: quick logs 47
60
+ [00:03:25]: quick logs 48
61
+ [00:03:25]: quick logs 49
62
+ [00:03:25]: quick logs 50
63
+ [00:03:25]: quick logs 51
64
+ [00:03:25]: quick logs 52
65
+ [00:03:25]: quick logs 53
66
+ [00:03:25]: quick logs 54
67
+ [00:03:25]: quick logs 55
68
+ [00:03:25]: quick logs 56
69
+ [00:03:25]: quick logs 57
70
+ [00:03:25]: quick logs 58
71
+ [00:03:25]: quick logs 59
72
+ [00:03:25]: quick logs 60
73
+ [00:03:26]: quick logs 61
74
+ [00:03:26]: quick logs 62
75
+ [00:03:26]: quick logs 63
76
+ [00:03:26]: quick logs 64
77
+ [00:03:26]: quick logs 65
78
+ [00:03:26]: quick logs 66
79
+ [00:03:26]: quick logs 67
80
+ [00:03:26]: quick logs 68
81
+ [00:03:26]: quick logs 69
82
+ [00:03:26]: quick logs 70
83
+ [00:03:26]: quick logs 71
84
+ [00:03:26]: quick logs 72
85
+ [00:03:26]: quick logs 73
86
+ [00:03:26]: quick logs 74
87
+ [00:03:26]: quick logs 75
88
+ [00:03:26]: quick logs 76
89
+ [00:03:26]: quick logs 77
90
+ [00:03:26]: quick logs 78
91
+ [00:03:26]: quick logs 79
92
+ [00:03:26]: quick logs 80
93
+ [00:03:26]: quick logs 81
94
+ [00:03:26]: quick logs 82
95
+ [00:03:26]: quick logs 83
96
+ [00:03:26]: quick logs 84
97
+ [00:03:26]: quick logs 85
98
+ [00:03:26]: quick logs 86
99
+ [00:03:26]: quick logs 87
100
+ [00:03:26]: quick logs 88
101
+ [00:03:26]: quick logs 89
102
+ [00:03:26]: quick logs 90
103
+ [00:03:26]: quick logs 91
104
+ [00:03:26]: quick logs 92
105
+ [00:03:26]: quick logs 93
106
+ [00:03:26]: quick logs 94
107
+ [00:03:26]: quick logs 95
108
+ [00:03:26]: quick logs 96
109
+ [00:03:26]: quick logs 97
110
+ [00:03:26]: quick logs 98
111
+ [00:03:26]: quick logs 99
112
+ [00:03:26]: log after 3 seconds
113
+ [00:03:26]: quick logs 100
114
+ [00:03:26]: quick logs 101
115
+ [00:03:26]: quick logs 102
116
+ [00:03:26]: quick logs 103
117
+ [00:03:26]: quick logs 104
118
+ [00:03:26]: quick logs 105
119
+ [00:03:26]: quick logs 106
120
+ [00:03:26]: quick logs 107
121
+ [00:03:26]: quick logs 108
122
+ [00:03:26]: quick logs 109
123
+ [00:03:26]: quick logs 110
124
+ [00:03:26]: quick logs 111
125
+ [00:03:26]: quick logs 112
126
+ [00:03:26]: quick logs 113
127
+ [00:03:26]: quick logs 114
128
+ [00:03:26]: quick logs 115
129
+ [00:03:26]: quick logs 116
130
+ [00:03:26]: quick logs 117
131
+ [00:03:26]: quick logs 118
132
+ [00:03:26]: quick logs 119
133
+ [00:03:26]: quick logs 120
134
+ [00:03:26]: quick logs 121
135
+ [00:03:26]: quick logs 122
136
+ [00:03:26]: quick logs 123
137
+ [00:03:26]: quick logs 124
138
+ [00:03:26]: quick logs 125
139
+ [00:03:26]: quick logs 126
140
+ [00:03:26]: quick logs 127
141
+ [00:03:26]: quick logs 128
142
+ [00:03:26]: quick logs 129
143
+ [00:03:26]: quick logs 130
144
+ [00:03:26]: quick logs 131
145
+ [00:03:26]: quick logs 132
146
+ [00:03:26]: quick logs 133
147
+ [00:03:26]: quick logs 134
148
+ [00:03:26]: quick logs 135
149
+ [00:03:26]: quick logs 136
150
+ [00:03:26]: quick logs 137
151
+ [00:03:26]: quick logs 138
152
+ [00:03:26]: quick logs 139
153
+ [00:03:26]: quick logs 140
154
+ [00:03:26]: quick logs 141
155
+ [00:03:26]: quick logs 142
156
+ [00:03:26]: quick logs 143
157
+ [00:03:26]: quick logs 144
158
+ [00:03:26]: quick logs 145
159
+ [00:03:26]: quick logs 146
160
+ [00:03:26]: quick logs 147
161
+ [00:03:26]: quick logs 148
162
+ [00:03:26]: quick logs 149
163
+ [00:03:26]: quick logs 150
164
+ [00:03:26]: quick logs 151
165
+ [00:03:26]: quick logs 152
166
+ [00:03:26]: quick logs 153
167
+ [00:03:26]: quick logs 154
168
+ [00:03:26]: quick logs 155
169
+ [00:03:26]: quick logs 156
170
+ [00:03:26]: quick logs 157
171
+ [00:03:26]: quick logs 158
172
+ [00:03:26]: quick logs 159
173
+ [00:03:26]: quick logs 160
174
+ [00:03:27]: quick logs 161
175
+ [00:03:27]: quick logs 162
176
+ [00:03:27]: quick logs 163
177
+ [00:03:27]: quick logs 164
178
+ [00:03:27]: quick logs 165
179
+ [00:03:27]: quick logs 166
180
+ [00:03:27]: quick logs 167
181
+ [00:03:27]: quick logs 168
182
+ [00:03:27]: quick logs 169
183
+ [00:03:27]: quick logs 170
184
+ [00:03:27]: quick logs 171
185
+ [00:03:27]: quick logs 172
186
+ [00:03:27]: quick logs 173
187
+ [00:03:27]: quick logs 174
188
+ [00:03:27]: quick logs 175
189
+ [00:03:27]: quick logs 176
190
+ [00:03:27]: quick logs 177
191
+ [00:03:27]: quick logs 178
192
+ [00:03:27]: quick logs 179
193
+ [00:03:27]: quick logs 180
194
+ [00:03:27]: quick logs 181
195
+ [00:03:27]: quick logs 182
196
+ [00:03:27]: quick logs 183
197
+ [00:03:27]: quick logs 184
198
+ [00:03:27]: quick logs 185
199
+ [00:03:27]: quick logs 186
200
+ [00:03:27]: quick logs 187
201
+ [00:03:27]: quick logs 188
202
+ [00:03:27]: quick logs 189
203
+ [00:03:27]: quick logs 190
204
+ [00:03:27]: quick logs 191
205
+ [00:03:27]: quick logs 192
206
+ [00:03:27]: quick logs 193
207
+ [00:03:27]: quick logs 194
208
+ [00:03:27]: quick logs 195
209
+ [00:03:27]: quick logs 196
210
+ [00:03:27]: quick logs 197
211
+ [00:03:27]: quick logs 198
212
+ [00:03:27]: quick logs 199
213
+ [00:03:27]: quick logs 200
214
+ [00:03:27]: quick logs 201
215
+ [00:03:27]: quick logs 202
216
+ [00:03:27]: quick logs 203
217
+ [00:03:27]: quick logs 204
218
+ [00:03:27]: quick logs 205
219
+ [00:03:27]: quick logs 206
220
+ [00:03:27]: quick logs 207
221
+ [00:03:27]: quick logs 208
222
+ [00:03:27]: quick logs 209
223
+ [00:03:27]: quick logs 210
224
+ [00:03:27]: quick logs 211
225
+ [00:03:27]: quick logs 212
226
+ [00:03:27]: quick logs 213
227
+ [00:03:27]: quick logs 214
228
+ [00:03:27]: quick logs 215
229
+ [00:03:27]: quick logs 216
230
+ [00:03:27]: quick logs 217
231
+ [00:03:27]: quick logs 218
232
+ [00:03:27]: quick logs 219
233
+ [00:03:27]: quick logs 220
234
+ [00:03:27]: quick logs 221
235
+ [00:03:27]: quick logs 222
236
+ [00:03:27]: quick logs 223
237
+ [00:03:27]: quick logs 224
238
+ [00:03:27]: quick logs 225
239
+ [00:03:27]: quick logs 226
240
+ [00:03:27]: quick logs 227
241
+ [00:03:27]: quick logs 228
242
+ [00:03:27]: quick logs 229
243
+ [00:03:27]: quick logs 230
244
+ [00:03:27]: quick logs 231
245
+ [00:03:27]: quick logs 232
246
+ [00:03:27]: quick logs 233
247
+ [00:03:27]: quick logs 234
248
+ [00:03:27]: quick logs 235
249
+ [00:03:27]: quick logs 236
250
+ [00:03:27]: quick logs 237
251
+ [00:03:27]: quick logs 238
252
+ [00:03:27]: quick logs 239
253
+ [00:03:27]: quick logs 240
254
+ [00:03:27]: quick logs 241
255
+ [00:03:27]: quick logs 242
256
+ [00:03:27]: quick logs 243
257
+ [00:03:27]: quick logs 244
258
+ [00:03:27]: quick logs 245
259
+ [00:03:27]: quick logs 246
260
+ [00:03:27]: quick logs 247
261
+ [00:03:27]: quick logs 248
262
+ [00:03:27]: quick logs 249
263
+ [00:03:27]: quick logs 250
264
+ [00:03:27]: quick logs 251
265
+ [00:03:27]: quick logs 252
266
+ [00:03:27]: quick logs 253
267
+ [00:03:27]: quick logs 254
268
+ [00:03:27]: quick logs 255
269
+ [00:03:27]: quick logs 256
270
+ [00:03:27]: quick logs 257
271
+ [00:03:27]: quick logs 258
272
+ [00:03:27]: quick logs 259
273
+ [00:03:28]: quick logs 260
274
+ [00:03:28]: quick logs 261
275
+ [00:03:28]: quick logs 262
276
+ [00:03:28]: quick logs 263
277
+ [00:03:28]: quick logs 264
278
+ [00:03:28]: quick logs 265
279
+ [00:03:28]: quick logs 266
280
+ [00:03:28]: quick logs 267
281
+ [00:03:28]: quick logs 268
282
+ [00:03:28]: quick logs 269
283
+ [00:03:28]: quick logs 270
284
+ [00:03:28]: quick logs 271
285
+ [00:03:28]: quick logs 272
286
+ [00:03:28]: quick logs 273
287
+ [00:03:28]: quick logs 274
288
+ [00:03:28]: quick logs 275
289
+ [00:03:28]: quick logs 276
290
+ [00:03:28]: quick logs 277
291
+ [00:03:28]: quick logs 278
292
+ [00:03:28]: quick logs 279
293
+ [00:03:28]: quick logs 280
294
+ [00:03:28]: quick logs 281
295
+ [00:03:28]: quick logs 282
296
+ [00:03:28]: quick logs 283
297
+ [00:03:28]: quick logs 284
298
+ [00:03:28]: quick logs 285
299
+ [00:03:28]: quick logs 286
300
+ [00:03:28]: quick logs 287
301
+ [00:03:28]: quick logs 288
302
+ [00:03:28]: quick logs 289
303
+ [00:03:28]: quick logs 290
304
+ [00:03:28]: quick logs 291
305
+ [00:03:28]: quick logs 292
306
+ [00:03:28]: quick logs 293
307
+ [00:03:28]: quick logs 294
308
+ [00:03:28]: quick logs 295
309
+ [00:03:28]: quick logs 296
310
+ [00:03:28]: quick logs 297
311
+ [00:03:28]: quick logs 298
312
+ [00:03:28]: quick logs 299
313
+ [00:03:28]: quick logs 300
314
+ [00:03:28]: quick logs 301
315
+ [00:03:28]: quick logs 302
316
+ [00:03:28]: quick logs 303
317
+ [00:03:28]: quick logs 304
318
+ [00:03:28]: quick logs 305
319
+ [00:03:28]: quick logs 306
320
+ [00:03:28]: quick logs 307
321
+ [00:03:28]: quick logs 308
322
+ [00:03:28]: quick logs 309
323
+ [00:03:28]: quick logs 310
324
+ [00:03:28]: quick logs 311
325
+ [00:03:28]: quick logs 312
326
+ [00:03:28]: quick logs 313
327
+ [00:03:28]: quick logs 314
328
+ [00:03:28]: quick logs 315
329
+ [00:03:28]: quick logs 316
330
+ [00:03:28]: quick logs 317
331
+ [00:03:28]: quick logs 318
332
+ [00:03:28]: quick logs 319
333
+ [00:03:28]: quick logs 320
334
+ [00:03:28]: quick logs 321
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "log-likho",
3
- "version": "2.0.1",
3
+ "version": "3.0.0",
4
4
  "description": "A simple file + colored stdout logger for Node.js apps.",
5
5
  "main": "index.js",
6
+ "types": "index.d.ts",
6
7
  "scripts": {
7
8
  "test": "node ./test/default.spec.js && node ./test/options.spec.js && node ./test/logger.spec.js",
8
- "clean": "rm -rf logs simple-logs logger-logs"
9
+ "pretest": "npm run clean",
10
+ "clean": "rm -rf logs simple-logs logger-logs logger-logs-selective log-likho-ts-test"
9
11
  },
10
12
  "repository": {
11
13
  "type": "git",
@@ -16,6 +18,8 @@
16
18
  "file",
17
19
  "logger",
18
20
  "log",
21
+ "typescript",
22
+ "ts-compatible",
19
23
  "nodejs",
20
24
  "simple",
21
25
  "logger",
@@ -0,0 +1,2 @@
1
+ [00:03:28]: testing log in new folder with append mode 1755110008642
2
+ [00:03:28]: Process ID is : 1923774
@@ -3,4 +3,5 @@
3
3
  module.exports = {
4
4
  logs_folder: `${process.cwd()}/logs`,
5
5
  mode: "w",
6
+ log_levels_to_file: ["INFO", "WARN", "ERROR", "FATAL", "LOG"]
6
7
  };
package/src/logger.js CHANGED
@@ -13,7 +13,7 @@ const create_logger = (options = {}) => {
13
13
  ...options,
14
14
  };
15
15
 
16
- const { logs_folder, mode } = final_options;
16
+ const { logs_folder, mode, log_levels_to_file } = final_options;
17
17
 
18
18
  if (!fs.existsSync(logs_folder)) {
19
19
  fs.mkdirSync(logs_folder);
@@ -23,37 +23,30 @@ const create_logger = (options = {}) => {
23
23
  const log_output_path = `${logs_folder}/${dd_month_yyyy_day()}.log`;
24
24
  const log_file = fs.createWriteStream(log_output_path, { flags: mode });
25
25
 
26
- function formatMessage(level, message) {
26
+ function format_message(level, args) {
27
27
  const timestamp = new Date().toTimeString().substring(0, 8);
28
- return `[${timestamp}] [${level}] ${util.format(message)}\n`;
28
+ // util.format applies formatting for multiple args
29
+ return `[${timestamp}] [${level}] ${util.format(...args)}\n`;
29
30
  }
30
31
 
31
- return {
32
- info: (msg) => {
33
- const formatted = formatMessage("INFO", msg);
34
- log_file.write(formatted);
35
- log_stdout.write(COLORS.INFO + formatted + COLORS.RESET);
36
- },
37
- warn: (msg) => {
38
- const formatted = formatMessage("WARN", msg);
39
- log_file.write(formatted);
40
- log_stdout.write(COLORS.WARN + formatted + COLORS.RESET);
41
- },
42
- error: (msg) => {
43
- const formatted = formatMessage("ERROR", msg);
44
- log_file.write(formatted);
45
- log_stdout.write(COLORS.ERROR + formatted + COLORS.RESET);
46
- },
47
- fatal: (msg) => {
48
- const formatted = formatMessage("FATAL", msg);
49
- log_file.write(formatted);
50
- log_stdout.write(COLORS.FATAL + formatted + COLORS.RESET);
51
- },
52
- log: (msg) => {
53
- const formatted = formatMessage("LOG", msg);
32
+ function write_log(level, color, args) {
33
+ const formatted = format_message(level, args);
34
+
35
+ // Always write to console
36
+ log_stdout.write(color + formatted + COLORS.RESET);
37
+
38
+ // Conditionally write to file based on config
39
+ if (log_levels_to_file.includes(level)) {
54
40
  log_file.write(formatted);
55
- log_stdout.write(formatted);
56
- },
41
+ }
42
+ }
43
+
44
+ return {
45
+ info: (...args) => write_log("INFO", COLORS.INFO, args),
46
+ warn: (...args) => write_log("WARN", COLORS.WARN, args),
47
+ error: (...args) => write_log("ERROR", COLORS.ERROR, args),
48
+ fatal: (...args) => write_log("FATAL", COLORS.FATAL, args),
49
+ log: (...args) => write_log("LOG", "", args),
57
50
  };
58
51
  };
59
52
 
@@ -21,13 +21,14 @@ const simple_logger = (options = {}) => {
21
21
  }
22
22
 
23
23
  const log_output_path = `${logs_folder}/${dd_month_yyyy_day()}.log`;
24
-
25
24
  const log_file = fs.createWriteStream(log_output_path, { flags: mode });
26
25
 
27
- return function (d) {
26
+ return function (...args) {
27
+ // util.format will handle multiple arguments & error objects nicely
28
28
  const text = `[${new Date().toTimeString().substring(0, 8)}]: ${util.format(
29
- d
29
+ ...args
30
30
  )}\n`;
31
+
31
32
  log_file.write(text);
32
33
  log_stdout.write(text);
33
34
  };
@@ -1,27 +0,0 @@
1
- console.log = require('../index')();
2
-
3
- console.log('testing 1');
4
-
5
- setTimeout(() => {
6
- console.log('log after 3 seconds');
7
- }, 1000);
8
-
9
- console.log(`Process ID is : ${process.pid}`);
10
-
11
- try {
12
- const a = 8;
13
- a = 89;
14
- } catch (error) {
15
- console.log("Logging error");
16
- console.log(error);
17
- }
18
-
19
-
20
- let i = 0
21
- const interval = setInterval(() => {
22
- console.log(`quick logs ${++i}`);
23
-
24
- if(i > 320) {
25
- clearInterval(interval);
26
- }
27
- }, 10);
@@ -1,16 +0,0 @@
1
- const path = require('path');
2
-
3
- const { create_logger } = require('../index');
4
-
5
- const logger = create_logger({
6
- logs_folder: `${process.cwd()}/logger-logs`,
7
- mode: "a" // append mode
8
- });
9
-
10
- console.log("=== Testing log levels ===");
11
-
12
- logger.info("This is an info message.");
13
- logger.warn("This is a warning.");
14
- logger.error("This is an error.");
15
- logger.fatal("This is a fatal error.");
16
- logger.log("This is a generic log.");
@@ -1,9 +0,0 @@
1
- const simple_logger = require("../index");
2
-
3
- console.log = simple_logger({
4
- logs_folder: `${process.cwd()}/simple-logs`,
5
- mode: "a",
6
- });
7
-
8
- console.log(`testing log in new folder with append mode ${Date.now()}`);
9
- console.log(`Process ID is : ${process.pid}`);
@@ -1,4 +0,0 @@
1
- const { dd_month_yyyy_day } = require('../../src/utils/date-formats');
2
-
3
-
4
- console.log(dd_month_yyyy_day());