@warlock.js/logger 1.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.
Files changed (66) hide show
  1. package/README.md +381 -0
  2. package/cjs/LogChannel.d.ts +20 -0
  3. package/cjs/LogChannel.d.ts.map +1 -0
  4. package/cjs/LogChannel.js +14 -0
  5. package/cjs/LogChannel.js.map +1 -0
  6. package/cjs/channels/console-log.d.ts +17 -0
  7. package/cjs/channels/console-log.d.ts.map +1 -0
  8. package/cjs/channels/console-log.js +44 -0
  9. package/cjs/channels/console-log.js.map +1 -0
  10. package/cjs/channels/file-log.d.ts +202 -0
  11. package/cjs/channels/file-log.d.ts.map +1 -0
  12. package/cjs/channels/file-log.js +315 -0
  13. package/cjs/channels/file-log.js.map +1 -0
  14. package/cjs/channels/index.d.ts +4 -0
  15. package/cjs/channels/index.d.ts.map +1 -0
  16. package/cjs/channels/json-file-log.d.ts +25 -0
  17. package/cjs/channels/json-file-log.d.ts.map +1 -0
  18. package/cjs/channels/json-file-log.js +73 -0
  19. package/cjs/channels/json-file-log.js.map +1 -0
  20. package/cjs/index.d.ts +8 -0
  21. package/cjs/index.d.ts.map +1 -0
  22. package/cjs/index.js +1 -0
  23. package/cjs/index.js.map +1 -0
  24. package/cjs/logger.d.ts +74 -0
  25. package/cjs/logger.d.ts.map +1 -0
  26. package/cjs/logger.js +84 -0
  27. package/cjs/logger.js.map +1 -0
  28. package/cjs/types.d.ts +21 -0
  29. package/cjs/types.d.ts.map +1 -0
  30. package/cjs/utils.d.ts +6 -0
  31. package/cjs/utils.d.ts.map +1 -0
  32. package/cjs/utils.js +21 -0
  33. package/cjs/utils.js.map +1 -0
  34. package/esm/LogChannel.d.ts +20 -0
  35. package/esm/LogChannel.d.ts.map +1 -0
  36. package/esm/LogChannel.js +14 -0
  37. package/esm/LogChannel.js.map +1 -0
  38. package/esm/channels/console-log.d.ts +17 -0
  39. package/esm/channels/console-log.d.ts.map +1 -0
  40. package/esm/channels/console-log.js +44 -0
  41. package/esm/channels/console-log.js.map +1 -0
  42. package/esm/channels/file-log.d.ts +202 -0
  43. package/esm/channels/file-log.d.ts.map +1 -0
  44. package/esm/channels/file-log.js +315 -0
  45. package/esm/channels/file-log.js.map +1 -0
  46. package/esm/channels/index.d.ts +4 -0
  47. package/esm/channels/index.d.ts.map +1 -0
  48. package/esm/channels/json-file-log.d.ts +25 -0
  49. package/esm/channels/json-file-log.d.ts.map +1 -0
  50. package/esm/channels/json-file-log.js +73 -0
  51. package/esm/channels/json-file-log.js.map +1 -0
  52. package/esm/index.d.ts +8 -0
  53. package/esm/index.d.ts.map +1 -0
  54. package/esm/index.js +1 -0
  55. package/esm/index.js.map +1 -0
  56. package/esm/logger.d.ts +74 -0
  57. package/esm/logger.d.ts.map +1 -0
  58. package/esm/logger.js +84 -0
  59. package/esm/logger.js.map +1 -0
  60. package/esm/types.d.ts +21 -0
  61. package/esm/types.d.ts.map +1 -0
  62. package/esm/utils.d.ts +6 -0
  63. package/esm/utils.d.ts.map +1 -0
  64. package/esm/utils.js +21 -0
  65. package/esm/utils.js.map +1 -0
  66. package/package.json +39 -0
package/README.md ADDED
@@ -0,0 +1,381 @@
1
+ # Warlock Logger
2
+
3
+ A powerful yet simple logger for Node.js
4
+
5
+ ## Features
6
+
7
+ - Fully async and non-blocking which doesn't affect the performance of your application.
8
+ - Easy to use and configure.
9
+ - Has multiple channels to log the messages to.
10
+ - You can add your own custom channels for logging.
11
+
12
+ ## Installation
13
+
14
+ `yarn add @warlock.js/logger`
15
+
16
+ Or
17
+
18
+ `npm i @warlock.js/logger`
19
+
20
+ ## Usage
21
+
22
+ At an early point of the application, you need to initialize the logger:
23
+
24
+ ```ts
25
+ import logger, { FileLog } from "@warlock.js/logger";
26
+
27
+ logger.configure({
28
+ channels: [new FileLog(), new ConsoleLog()],
29
+ });
30
+ ```
31
+
32
+ Here we declared our logger configurations to use the `FileLog` and `ConsoleLog` channels, the file log channel will log all the logs to a file, and the console log channel will log all the logs to the console.
33
+
34
+ ## Logging Strategy
35
+
36
+ To go any value simple use `log` function, it mainly receives 4 parameters:
37
+
38
+ - `module`: the module name, it's used to group the logs, for example, if you have a module called `request`, all the logs related to the request module will be grouped under the `request` module.
39
+ - `action`: the action name, it's used to group the logs, for example, if you have an action called `create`, all the logs related to the `create` action will be grouped under the `create` action.
40
+ - `message`: the message to log.
41
+ - `level`: there are 4 types of logging `warn`, `info`, `error`, `debug`, the default is `info`.
42
+
43
+ ## Examples
44
+
45
+ ```ts
46
+ import { log } from "@warlock.js/logger";
47
+
48
+ log("request", "create", "user created successfully", "info");
49
+ ```
50
+
51
+ You can also use `log.info` `log.warn` `log.error` `log.debug` functions to log the message.
52
+
53
+ ```ts
54
+ import { log } from "@warlock.js/logger";
55
+
56
+ log.info("request", "create", "user created successfully");
57
+
58
+ if (somethingWentWrong) {
59
+ log.error("request", "create", "something went wrong");
60
+ }
61
+
62
+ database.on("connection", () => {
63
+ log.success("database", "connection", "database connected successfully");
64
+ });
65
+ ```
66
+
67
+ ## Console Log Channel
68
+
69
+ The console log channel will log all the logs to the console, the message appears in the console will be colored based on the log level using [chalk](https://www.npmjs.com/package/chalk).
70
+
71
+ ```ts
72
+ import logger, { ConsoleLog } from "@warlock.js/logger";
73
+
74
+ logger.configure({
75
+ channels: [new ConsoleLog()],
76
+ });
77
+ ```
78
+
79
+ ## File Log Channel
80
+
81
+ The file log channel will log all the logs to a **single file**, the file will be created in the `logs` directory in `/storage` directory with name `app` by default, and extension is set to `log` however, you can change the file name and the directory path.
82
+
83
+ ```ts
84
+ import logger, { FileLog } from "@warlock.js/logger";
85
+
86
+ logger.configure({
87
+ channels: [
88
+ new FileLog({
89
+ storageDirectory: process.cwd() + "/logs",
90
+ fileName: "app",
91
+ extension: "log",
92
+ }),
93
+ ],
94
+ });
95
+ ```
96
+
97
+ The message time is stored by default prefixed with current date/time in this format `YYYY-MM-DD HH:mm:ss`, however, you can change the format by passing the `dateFormat` option.
98
+
99
+ ```ts
100
+ import logger, { FileLog } from "@warlock.js/logger";
101
+
102
+ logger.configure({
103
+ channels: [
104
+ new FileLog({
105
+ dateFormat: {
106
+ date: "DD-MM-YYYY",
107
+ time: "HH:mm:ss",
108
+ },
109
+ }),
110
+ ],
111
+ });
112
+ ```
113
+
114
+ > You can see the available date/time formats in [dayjs](https://day.js.org/docs/en/display/format) documentation.
115
+
116
+ This could be useful with small projects, but it's not recommended to use it if the application is large, because the file will be very large and it will affect the performance of the application, you can use the following channels to solve this problem.
117
+
118
+ ## Chunk mode Log Channel
119
+
120
+ In the file log channel, there are three types of chunk modes:
121
+
122
+ 1. `single`: this is the default mode, all the logs will be stored in a single file.
123
+ 2. `daily`: the logs will be stored in a file based on the date, for example, if the date is `2021-01-01`, the file name will be `2021-01-01.log`.
124
+ 3. `hourly`: the logs will be stored in a file based on the date and hour, for example, if the hour is `14`, the file name will be `2021-01-01-14.log`.
125
+
126
+ > Please note the hourly mode is set to 24 hours mode.
127
+
128
+ ```ts
129
+ import logger, { ChunkFileLog } from "@warlock.js/logger";
130
+
131
+ logger.configure({
132
+ channels: [
133
+ new ChunkFileLog({
134
+ chunk: "daily", // default is single
135
+ }),
136
+ ],
137
+ });
138
+ ```
139
+
140
+ For better performance, the file will be created in the `logs` directory in `/storage`, each file will be named based on the date, for example, if the date is `2021-01-01`, the file name will be `2021-01-01.log`, and the message time is stored by default prefixed with current date/time in this format `YYYY-MM-DD HH:mm:ss`, however, you can change the format by passing the `dateFormat` option.
141
+
142
+ ## JSON File Log Channel
143
+
144
+ Works exactly in the same sense of [File Log Channel](#file-log-channel), but the difference is that the logs will be stored in JSON format.
145
+
146
+ ```ts
147
+ import logger, { JSONFileLog } from "@warlock.js/logger";
148
+
149
+ logger.configure({
150
+ channels: [new JSONFileLog()],
151
+ });
152
+ ```
153
+
154
+ Example of output log file
155
+
156
+ `/storage/logs/01-04-2023.json`
157
+
158
+ ```json
159
+ {
160
+ "messages": [
161
+ {
162
+ "module": "request",
163
+ "action": "create",
164
+ "message": "user created successfully",
165
+ "level": "info",
166
+ "date": "01-04-2023",
167
+ "time": "12:00:00"
168
+ }
169
+ ]
170
+ }
171
+ ```
172
+
173
+ If the log is an `error` log, the trace will also be included:
174
+
175
+ `/storage/logs/01-04-2023.json`
176
+
177
+ ```json
178
+ {
179
+ "date": "01-04-2023",
180
+ "logs": [
181
+ {
182
+ "module": "request",
183
+ "action": "create",
184
+ "message": "user created successfully",
185
+ "level": "error",
186
+ "date": "01-04-2023",
187
+ "time": "12:00:00",
188
+ "trace": "Error: something went wrong...."
189
+ }
190
+ ]
191
+ }
192
+ ```
193
+
194
+ ## Group Log Channel by level, module or action
195
+
196
+ Another way to reduce file sizes is to group the logs by level, module, or action, you can use the `groupBy` option to group the logs.
197
+
198
+ The files in this case will be added in folders with the grouped names, for example, if the group is `level`, the files will be added in folders with the names `info`, `warn`, `error`, and `debug`.
199
+
200
+ ```ts
201
+ import logger, { FileLog } from "@warlock.js/logger";
202
+
203
+ logger.configure({
204
+ channels: [
205
+ new FileLog({
206
+ groupBy: ["level"],
207
+ }),
208
+ ],
209
+ });
210
+ ```
211
+
212
+ This will create the following structure:
213
+
214
+ ```
215
+ logs
216
+ ├── info
217
+ │ └── app.log
218
+ ├── warn
219
+ │ └── app.log
220
+ ├── error
221
+ │ └── app.log
222
+ ├── debug
223
+ │ └── app.log
224
+ ├── success
225
+ │ └── app.log
226
+ ```
227
+
228
+ If the group is `module`, the files will be added in folders with the module names.
229
+
230
+ ```ts
231
+ import logger, { FileLog } from "@warlock.js/logger";
232
+
233
+ logger.configure({
234
+ channels: [
235
+ new FileLog({
236
+ groupBy: ["module"],
237
+ }),
238
+ ],
239
+ });
240
+ ```
241
+
242
+ This will create the following structure:
243
+
244
+ ```
245
+ logs
246
+ ├── request
247
+ │ └── app.log
248
+ ├── database
249
+ │ └── app.log
250
+ ```
251
+
252
+ ### Group by multiple options
253
+
254
+ You can group the logs by multiple options, for example, if you want to group the logs by `level` and `module`, you can pass the options
255
+
256
+ ```ts
257
+ import logger, { FileLog } from "@warlock.js/logger";
258
+
259
+ logger.configure({
260
+ channels: [
261
+ new FileLog({
262
+ groupBy: ["level", "module"],
263
+ }),
264
+ ],
265
+ });
266
+ ```
267
+
268
+ This will create the following structure:
269
+
270
+ ```
271
+ logs
272
+ ├── info
273
+ │ ├── request
274
+ │ │ └── app.log
275
+ ...
276
+ ```
277
+
278
+ > The order you set in the groupBy array will be the order of the folders.
279
+
280
+ ## Create Custom Log Channel
281
+
282
+ You can create your own log channel by extending the `LogChannel` class
283
+
284
+ ```ts
285
+ import { type LogContract, LogChannel, LogLevel } from "@warlock.js/logger";
286
+
287
+ export default class CustomLogChannel
288
+ extends LogChannel
289
+ implements LogContract
290
+ {
291
+ /**
292
+ * Channel name
293
+ */
294
+ public name = "custom";
295
+
296
+ /**
297
+ * Log the message
298
+ *
299
+ * @param module
300
+ * @param action
301
+ * @param message
302
+ * @param level
303
+ */
304
+ public async log(
305
+ module: string,
306
+ action: string,
307
+ message: string,
308
+ level: LogLevel
309
+ ) {
310
+ // log the message
311
+ }
312
+ }
313
+ ```
314
+
315
+ If the log channel will output something in the terminal, mark the `terminal` property as `true`.
316
+
317
+ ```ts
318
+ import { type LogContract, LogChannel, LogLevel } from "@warlock.js/logger";
319
+
320
+ export default class CustomLogChannel
321
+ extends LogChannel
322
+ implements LogContract
323
+ {
324
+ /**
325
+ * Whether the log channel will output something in the terminal
326
+ */
327
+ public terminal = true;
328
+
329
+ /**
330
+ * Channel name
331
+ */
332
+ public name = "custom";
333
+
334
+ /**
335
+ * Log the message
336
+ *
337
+ * @param module
338
+ * @param action
339
+ * @param message
340
+ * @param level
341
+ */
342
+ public async log(
343
+ module: string,
344
+ action: string,
345
+ message: string,
346
+ level: LogLevel
347
+ ) {
348
+ // log the message
349
+ }
350
+ }
351
+ ```
352
+
353
+ This will automatically parse and remove the ANSI color codes from the message.
354
+
355
+ Now you can use the custom log channel in your application.
356
+
357
+ ```ts
358
+ import logger, { CustomLogChannel } from "@warlock.js/logger";
359
+
360
+ logger.configure({
361
+ channels: [new CustomLogChannel()],
362
+ });
363
+ ```
364
+
365
+ ## Capture Uncaught Errors
366
+
367
+ If you want automatically capture any unhandled errors, you can import `captureAnyUnhandledRejection` helper function and call it in your application entry point.
368
+
369
+ ```ts
370
+ import { captureAnyUnhandledRejection } from "@warlock.js/logger";
371
+
372
+ captureAnyUnhandledRejection();
373
+ ```
374
+
375
+ ## Tests
376
+
377
+ To run the tests, you need to run the following command:
378
+
379
+ ```bash
380
+ yarn test
381
+ ```
@@ -0,0 +1,20 @@
1
+ import { LogContract, LogLevel } from "./types";
2
+ export declare abstract class LogChannel implements LogContract {
3
+ /**
4
+ * Channel name
5
+ */
6
+ name: string;
7
+ /**
8
+ * Channel description
9
+ */
10
+ description?: string;
11
+ /**
12
+ * Determine if channel is logging in terminal
13
+ */
14
+ terminal: boolean;
15
+ /**
16
+ * Log the given message
17
+ */
18
+ abstract log(module: string, action: string, message: any, level: LogLevel): void | Promise<void>;
19
+ }
20
+ //# sourceMappingURL=LogChannel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LogChannel.d.ts","sourceRoot":"","sources":["../src/LogChannel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEhD,8BAAsB,UAAW,YAAW,WAAW;IACrD;;OAEG;IACI,IAAI,EAAG,MAAM,CAAC;IAErB;;OAEG;IACI,WAAW,CAAC,EAAE,MAAM,CAAC;IAE5B;;OAEG;IACI,QAAQ,UAAS;IAExB;;OAEG;aACa,GAAG,CACjB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,GAAG,EACZ,KAAK,EAAE,QAAQ,GACd,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;CACxB"}
@@ -0,0 +1,14 @@
1
+ 'use strict';class LogChannel {
2
+ /**
3
+ * Channel name
4
+ */
5
+ name;
6
+ /**
7
+ * Channel description
8
+ */
9
+ description;
10
+ /**
11
+ * Determine if channel is logging in terminal
12
+ */
13
+ terminal = false;
14
+ }exports.LogChannel=LogChannel;//# sourceMappingURL=LogChannel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LogChannel.js","sources":["../src/LogChannel.ts"],"sourcesContent":[null],"names":[],"mappings":"mBAEsB,UAAU,CAAA;AAC9B;;AAEG;AACI,IAAA,IAAI,CAAU;AAErB;;AAEG;AACI,IAAA,WAAW,CAAU;AAE5B;;AAEG;IACI,QAAQ,GAAG,KAAK,CAAC;AAWzB"}
@@ -0,0 +1,17 @@
1
+ import { LogChannel } from "../LogChannel";
2
+ import { LogLevel } from "../types";
3
+ export declare class ConsoleLog extends LogChannel {
4
+ /**
5
+ * {@inheritdoc}
6
+ */
7
+ name: string;
8
+ /**
9
+ * Determine if channel is logging in terminal
10
+ */
11
+ terminal: boolean;
12
+ /**
13
+ * {@inheritdoc}
14
+ */
15
+ log(module: string, action: string, message: any, level: LogLevel): void;
16
+ }
17
+ //# sourceMappingURL=console-log.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"console-log.d.ts","sourceRoot":"","sources":["../../src/channels/console-log.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC,qBAAa,UAAW,SAAQ,UAAU;IACxC;;OAEG;IACI,IAAI,SAAa;IAExB;;OAEG;IACI,QAAQ,UAAQ;IAEvB;;OAEG;IACI,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,QAAQ;CAsEzE"}
@@ -0,0 +1,44 @@
1
+ 'use strict';var copper=require('@mongez/copper'),LogChannel=require('../LogChannel.js');class ConsoleLog extends LogChannel.LogChannel {
2
+ /**
3
+ * {@inheritdoc}
4
+ */
5
+ name = "console";
6
+ /**
7
+ * Determine if channel is logging in terminal
8
+ */
9
+ terminal = true;
10
+ /**
11
+ * {@inheritdoc}
12
+ */
13
+ log(module, action, message, level) {
14
+ // display date and time with milliseconds
15
+ const date = new Date().toISOString(); // i.e 2021-01-01T00:00:00.000Z
16
+ switch (level) {
17
+ case "debug":
18
+ // add a debug icon
19
+ console.log(copper.colors.magentaBright("⚙"), copper.colors.yellow(`(${date})`), copper.colors.cyan(`[${module}]`), copper.colors.magenta(`[${action}]`), copper.colors.magentaBright(message));
20
+ break;
21
+ case "info":
22
+ // add an info icon
23
+ console.log(copper.colors.blueBright("ℹ"), copper.colors.yellow(`(${date})`), copper.colors.cyan(`[${module}]`), copper.colors.magenta(`[${action}]`), copper.colors.blueBright(message));
24
+ break;
25
+ case "warn":
26
+ // add a warning icon
27
+ console.log(copper.colors.yellow("⚠"), copper.colors.green(`(${date})`), copper.colors.cyan(`[${module}]`), copper.colors.magenta(`[${action}]`), copper.colors.yellowBright(message));
28
+ break;
29
+ case "error":
30
+ // add an error icon
31
+ console.log(copper.colors.red("✗"), copper.colors.yellow(`(${date})`), copper.colors.cyan(`[${module}]`), copper.colors.magenta(`[${action}]`), copper.colors.redBright(message));
32
+ break;
33
+ case "success":
34
+ // add a success icon
35
+ console.log(copper.colors.green("✓"), copper.colors.yellow(`(${date})`), copper.colors.cyan(`[${module}]`), copper.colors.magenta(`[${action}]`), copper.colors.greenBright(message));
36
+ break;
37
+ default:
38
+ console.log("[log]", copper.colors.yellow(`(${date})`), copper.colors.cyan(`[${module}]`), copper.colors.magenta(`[${action}]`), message);
39
+ }
40
+ if (typeof message === "object") {
41
+ console.log(message);
42
+ }
43
+ }
44
+ }exports.ConsoleLog=ConsoleLog;//# sourceMappingURL=console-log.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"console-log.js","sources":["../../src/channels/console-log.ts"],"sourcesContent":[null],"names":["LogChannel","colors"],"mappings":"yFAIM,MAAO,UAAW,SAAQA,qBAAU,CAAA;AACxC;;AAEG;IACI,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,CAAC,OAAO,CAAC,CAAC;AACtB,SAAA;KACF;AACF"}
@@ -0,0 +1,202 @@
1
+ import { LogChannel } from "../LogChannel";
2
+ import { LogContract, LogLevel } from "../types";
3
+ export type FilteringOptions = {
4
+ level: LogLevel;
5
+ module: string;
6
+ action: string;
7
+ };
8
+ export type FileLogConfig = {
9
+ storagePath?: string;
10
+ /**
11
+ * File name, without extension
12
+ */
13
+ name?: string;
14
+ /**
15
+ * chunk mode
16
+ * If set to `single`, the logs will be created in a single file, unless the rotate is set to true
17
+ * If set to `daily`, the logs will be created in a daily file, unless the rotate is set to true
18
+ * If set to `hourly`, the logs will be created in an hourly file, unless the rotate is set to true
19
+ * @default single
20
+ */
21
+ chunk?: "single" | "daily" | "hourly";
22
+ /**
23
+ * Whether to rotate the file
24
+ *
25
+ * @default true
26
+ */
27
+ rotate?: boolean;
28
+ /**
29
+ * File Extension
30
+ *
31
+ * @default log
32
+ */
33
+ extension?: string;
34
+ /**
35
+ * If rotate is set, the rotate name will be added to the file name suffixed with `-`
36
+ *
37
+ * @default DD-MM-YYYY
38
+ */
39
+ rotateFileName?: string;
40
+ /**
41
+ * Max file size before rotating the file
42
+ *
43
+ * @default 10MB
44
+ */
45
+ maxFileSize?: number;
46
+ /**
47
+ * Set the max messages that needs to be added before writing to the file
48
+ *
49
+ * @default 100
50
+ */
51
+ maxMessagesToWrite?: number;
52
+ /**
53
+ * Group logs by
54
+ * Please note that the order matters here
55
+ * For example, if you set `groupBy: ['level', 'module']`, the logs will be added in level name first, then by module
56
+ *
57
+ * @default none
58
+ */
59
+ groupBy?: ("level" | "module" | "action")[];
60
+ /**
61
+ * Define what levels should be logged
62
+ *
63
+ * @default all
64
+ */
65
+ levels?: LogLevel[];
66
+ /**
67
+ * Filter what logs should be logged
68
+ *
69
+ * @default all
70
+ */
71
+ filter?: (options: FilteringOptions) => boolean;
72
+ /**
73
+ * Date and time format
74
+ */
75
+ dateFormat?: {
76
+ date?: string;
77
+ time?: string;
78
+ };
79
+ };
80
+ export type LogMessage = {
81
+ content: string;
82
+ level: LogLevel;
83
+ date: string;
84
+ module: string;
85
+ action: string;
86
+ stack: string;
87
+ };
88
+ export declare class FileLog extends LogChannel implements LogContract {
89
+ /**
90
+ * {@inheritdoc}
91
+ */
92
+ name: string;
93
+ /**
94
+ * Messages buffer
95
+ */
96
+ protected messages: LogMessage[];
97
+ /**
98
+ * Grouped messages
99
+ */
100
+ protected groupedMessages: Record<string, LogMessage[]>;
101
+ /**
102
+ * Default channel configurations
103
+ */
104
+ protected defaultConfigurations: FileLogConfig;
105
+ /**
106
+ * Last write time
107
+ */
108
+ protected lastWriteTime: number;
109
+ /**
110
+ * Channel configurations
111
+ */
112
+ protected channelConfigurations: FileLogConfig;
113
+ /**
114
+ * A flag to determine if the file is being written
115
+ */
116
+ protected isWriting: boolean;
117
+ /**
118
+ * Get config value
119
+ */
120
+ protected config<K extends keyof FileLogConfig>(key: K): FileLogConfig[K];
121
+ /**
122
+ * Constructor
123
+ */
124
+ constructor(configurations?: FileLogConfig);
125
+ /**
126
+ * Check file size for file rotation
127
+ */
128
+ protected checkAndRotateFile(filePath?: string): Promise<void>;
129
+ /**
130
+ * Rotate log file
131
+ */
132
+ protected rotateLogFile(): Promise<void>;
133
+ /**
134
+ * Flush messages
135
+ */
136
+ protected initMessageFlush(): void;
137
+ /**
138
+ * Get file path
139
+ */
140
+ get filePath(): string;
141
+ /**
142
+ * Get max messages
143
+ */
144
+ protected get maxMessagesToWrite(): number;
145
+ /**
146
+ * Get file name
147
+ */
148
+ get fileName(): string;
149
+ /**
150
+ * Get file extension
151
+ */
152
+ get extension(): string;
153
+ /**
154
+ * Get content
155
+ */
156
+ protected get content(): string;
157
+ /**
158
+ * Get storage path
159
+ */
160
+ get storagePath(): string;
161
+ /**
162
+ * {@inheritdoc}
163
+ */
164
+ protected init(): Promise<void>;
165
+ /**
166
+ * Set configurations
167
+ */
168
+ configurations(configurations: FileLogConfig): this;
169
+ /**
170
+ * {@inheritdoc}
171
+ */
172
+ log(module: string, action: string, message: any, level: LogLevel): Promise<void>;
173
+ /**
174
+ * Check if messages should be written
175
+ */
176
+ protected checkIfMessagesShouldBeWritten(): Promise<void>;
177
+ /**
178
+ * Should be called after messages are saved
179
+ */
180
+ protected onSave(): void;
181
+ /**
182
+ * Check if messages should be grouped
183
+ */
184
+ protected get messagedShouldBeGrouped(): boolean;
185
+ /**
186
+ * Write messages to the file
187
+ */
188
+ protected writeMessagesToFile(): Promise<void>;
189
+ /**
190
+ * Write grouped messages to the file
191
+ */
192
+ protected writeGroupedMessagesToFile(): Promise<void>;
193
+ /**
194
+ * Prepare grouped messages
195
+ */
196
+ protected prepareGroupedMessages(): void;
197
+ /**
198
+ * Start writing to the file
199
+ */
200
+ protected write(filePath: string, content: string): Promise<unknown>;
201
+ }
202
+ //# sourceMappingURL=file-log.d.ts.map