node-logy 0.1.1 → 0.1.3

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/dist/index.d.ts CHANGED
@@ -1,99 +1,256 @@
1
- import type types = require("./types");
1
+ import { LogLevelType } from "./protocol";
2
2
  /**
3
- * Represents a logger used to log to node's stdout console and also save logs to log files.
4
- * Uses a seperate process that writes to log file while the main logger in your application process is non blocking
3
+ * Timestamp format options
5
4
  */
6
- declare class NodeLogger {
5
+ export type TimestampType = "iso" | "locale" | "utc" | "unix" | "unix_ms" | "date" | "time" | "datetime" | "short" | "custom";
6
+ /**
7
+ * Options to change the logger
8
+ */
9
+ export type LoggerOptions = {
10
+ /**
11
+ * Where to store the log files output
12
+ */
13
+ basePath: string;
14
+ /**
15
+ * If this logger should save logs to log files
16
+ */
17
+ saveToLogFiles: boolean;
18
+ /**
19
+ * If this logger log's should be printed to the stdout of the process
20
+ */
21
+ outputToConsole: boolean;
22
+ /**
23
+ * If the output to console should be colored
24
+ */
25
+ useColoredOutput: boolean;
26
+ /**
27
+ * Map of specific log level and what color to use
28
+ */
29
+ colorMap: Record<LogLevelType, string>;
7
30
  /**
8
- * Holds the options passed to the logger
31
+ * If it should add timestamps to logs
32
+ */
33
+ showTimeStamps: boolean;
34
+ /**
35
+ * Which timestamp format to use (only applies when showTimeStamps is true)
36
+ */
37
+ timestampType: TimestampType;
38
+ /**
39
+ * Custom timestamp format string (only used when timestampType is "custom")
40
+ * Use tokens: YYYY=year, MM=month, DD=day, HH=hour, mm=minute, ss=second, ms=millisecond
41
+ */
42
+ customTimestampFormat?: string;
43
+ /**
44
+ * If it should show log level
45
+ */
46
+ showLogLevel: boolean;
47
+ /**
48
+ * Map a specific log level with a string value use for it
49
+ */
50
+ logLevelMap: Record<LogLevelType, string>;
51
+ /**
52
+ * Minimum log level to process (filters out lower levels)
53
+ */
54
+ minLevel?: LogLevelType;
55
+ /**
56
+ * Maximum log level to process (filters out higher levels)
57
+ */
58
+ maxLevel?: LogLevelType;
59
+ /**
60
+ * Specific levels to include (whitelist)
61
+ */
62
+ includeLevels?: LogLevelType[];
63
+ /**
64
+ * Specific levels to exclude (blacklist)
65
+ */
66
+ excludeLevels?: LogLevelType[];
67
+ /**
68
+ * Filter function to determine if a log should be processed
69
+ */
70
+ filter?: (level: LogLevelType, message: any) => boolean;
71
+ /**
72
+ * Include process ID in logs
73
+ */
74
+ showProcessId?: boolean;
75
+ /**
76
+ * Include hostname in logs
77
+ */
78
+ showHostname?: boolean;
79
+ /**
80
+ * Include environment name (dev/prod) in logs
81
+ */
82
+ environment?: string;
83
+ /**
84
+ * Show where it was called at (file:line:column)
85
+ */
86
+ showCallSite?: boolean;
87
+ };
88
+ /**
89
+ * Custom error for logger initialization failures
90
+ */
91
+ export declare class LoggerInitializationError extends Error {
92
+ constructor(message: string);
93
+ }
94
+ /**
95
+ * Used to log to console and also the log files
96
+ */
97
+ export declare class Logger {
98
+ /**
99
+ * Local refrence to options passed
9
100
  */
10
101
  private _options;
11
102
  /**
12
- * Holds ref to the go binary that we write to the stdin of
103
+ * Holds the spawned side car process
104
+ */
105
+ private _process;
106
+ /**
107
+ * Holds the process stdout buffer content
108
+ */
109
+ private _processStdoutBuffer;
110
+ /**
111
+ * Used to encode request to / from binary protocol
112
+ */
113
+ private _requestEncoder;
114
+ /**
115
+ * Used to encode responses to / from binary protocol
116
+ */
117
+ private _responseEncoder;
118
+ /**
119
+ * A requests id
120
+ */
121
+ private _id;
122
+ /**
123
+ * Gets the next ID if it exceeds 1 million then rolls back to zero
124
+ */
125
+ private _getNextId;
126
+ /**
127
+ * Holds pending requests that expect a response
128
+ */
129
+ private _pending;
130
+ constructor(options?: Partial<LoggerOptions>);
131
+ /**
132
+ * Get the path to the server / sidecar
133
+ * @returns Path to the server
134
+ */
135
+ private _getServerFilePath;
136
+ /**
137
+ * Inits the side car process
138
+ */
139
+ private _initProcess;
140
+ /**
141
+ * Writes to the stdin of the process
142
+ */
143
+ private _writeToStdin;
144
+ /**
145
+ * Clears pending requests on process exit/error
146
+ */
147
+ private _clearPending;
148
+ /**
149
+ * Parses the stdout buffer produced by the spawned process
150
+ * Uses ResponseEncoder for all protocol operations
151
+ */
152
+ private _parseProcessStdout;
153
+ /**
154
+ * Handle a decoded response
155
+ */
156
+ private _handleResponse;
157
+ /**
158
+ * Resolve any pending requests that expected a response
159
+ * @param response The response
160
+ */
161
+ private _resolvePending;
162
+ /**
163
+ * Send a request that expects a response to the sidecar process
164
+ * @param request The request to send that expects a response
165
+ */
166
+ private _sendRequest;
167
+ /**
168
+ * Validates the basePath option
169
+ */
170
+ private _validateBasePath;
171
+ /**
172
+ * Creates the log directory if file logging is enabled
13
173
  */
14
- private _spawnRef;
174
+ private _initializeDirectory;
15
175
  /**
16
- * Indicates if flush has been sent
176
+ * Extract call site information (file:line:column) from stack trace
17
177
  */
18
- private _isFlushing;
178
+ private _getCallSite;
19
179
  /**
20
- * Tracks the UTC date (YYYY-MM-DD) to handle daily file rotation
180
+ * Check if a log level should be processed based on filtering rules
21
181
  */
22
- private _currentDateStr;
182
+ private _shouldLog;
23
183
  /**
24
- * Pass additional options on initialization to change the logger's behaviour
25
- * @param options Change the behaviour of the logger
184
+ * Verifies the log directory is writable
26
185
  */
27
- constructor(options?: Partial<types.NodeLoggerOptions>);
186
+ private _verifyWritable;
28
187
  /**
29
- * Helper to format a date into a UTC YYYY-MM-DD string
188
+ * Get the string representation of a log level
30
189
  */
31
- private getUtcDateString;
190
+ private _getLevelString;
32
191
  /**
33
- * Checks if the UTC day has changed
192
+ * Format timestamp based on the configured timestampType
34
193
  */
35
- private checkDateRotation;
194
+ private _formatTimestamp;
36
195
  /**
37
- * Trys to find the `node_process` file which spawn the protcol handler
38
- * @returns The path to the file or undefined
196
+ * Apply custom format string to date
197
+ * Tokens: YYYY=year, MM=month, DD=day, HH=hour, mm=minute, ss=second, ms=millisecond
39
198
  */
40
- private findNodeProcessFile;
199
+ private _applyCustomFormat;
41
200
  /**
42
- * Log information
43
- * @param level The specific level of log message
44
- * @param content A message or error object or object
45
- * @param contents Any other messages or error objects
201
+ * Convert any value to string representation
46
202
  */
47
- private log;
203
+ private _stringify;
48
204
  /**
49
- * Logs an informational message to the console
50
- * @param message The message to log
51
- * @param messages Any other message objects or error objects
205
+ * Format a log message with optional fields
52
206
  */
53
- info(message: unknown, ...messages: unknown[]): void;
207
+ private _formatMessage;
54
208
  /**
55
- * Logs a warning message to the console
56
- * @param message The message to log
57
- * @param messages Any other message objects or error objects
209
+ * Apply color to the entire message if colored output is enabled
58
210
  */
59
- warn(message: unknown, ...messages: unknown[]): void;
211
+ private _colorize;
60
212
  /**
61
- * Logs an error message to the console.
62
- * It is recommended you pass the actual error object so we can print as much information as possible.
63
- *
64
- * For example:
65
- *
66
- * ```ts
67
- * logger.error(new Error(""));
68
- * ```
69
- * @param messageOrError The error object or message to log
70
- * @param messages Any other message objects or error objects
213
+ * Log a specific level and content
214
+ * @param level The specific level to log
215
+ * @param message The content of the message
216
+ * @param messages Any addtional messages
71
217
  */
72
- error(messageOrError: unknown, ...messages: unknown[]): void;
218
+ log(level: LogLevelType, message: any, ...messages: any[]): void;
73
219
  /**
74
- * Used to write any remaning logs to the stdin and close the sidecar logger - NEEDS to be called on app exit or on app cleanup
75
- * as if not it will keep the app alive and wait for flush command
220
+ * Convenience method for INFO level
221
+ */
222
+ info(message: any, ...messages: any[]): void;
223
+ /**
224
+ * Convenience method for WARN level
225
+ */
226
+ warn(message: any, ...messages: any[]): void;
227
+ /**
228
+ * Convenience method for ERROR level
229
+ */
230
+ error(message: any, ...messages: any[]): void;
231
+ /**
232
+ * Convenience method for DEBUG level
233
+ */
234
+ debug(message: any, ...messages: any[]): void;
235
+ /**
236
+ * Convenience method for FATAL level
237
+ */
238
+ fatal(message: any, ...messages: any[]): void;
239
+ /**
240
+ * Flush remaning buffer to log files
76
241
  */
77
242
  flush(): Promise<void>;
78
243
  /**
79
- * Captures the stack trace of the caller of the logger (not the logger itself)
244
+ * Used to reload / refresh the process
80
245
  */
81
- private getLogCallStack;
246
+ reload(): Promise<void>;
82
247
  /**
83
- * Write to the stdin of the process in the protocol defined
84
- * @param request The request payload
248
+ * Used to shut down the child process and clean up
85
249
  */
86
- private writeToStdin;
250
+ shutdown(): Promise<void>;
87
251
  /**
88
- * Extracts as much information as possible from an error or object or any other type and returns it as a string, if it has multiple fields to it
89
- * then they will be serpated by new lines
90
- * @param object The error object or any object to extract information from
91
- * @returns A detailed string representation of the error/object
252
+ * Get current logger options (read-only copy)
92
253
  */
93
- private extractInfo;
254
+ get options(): Readonly<LoggerOptions>;
94
255
  }
95
- declare const _default: {
96
- NodeLogger: typeof NodeLogger;
97
- };
98
- export = _default;
99
256
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,YAAY,KAAK,GAAG,QAAQ,SAAS,CAAC,CAAC;AAwBvC;;;GAGG;AACH,cAAM,UAAU;IACd;;OAEG;IACH,OAAO,CAAC,QAAQ,CAA0B;IAE1C;;OAEG;IACH,OAAO,CAAC,SAAS,CAA6D;IAE9E;;OAEG;IACH,OAAO,CAAC,WAAW,CAAS;IAE5B;;OAEG;IACH,OAAO,CAAC,eAAe,CAAS;IAEhC;;;OAGG;gBACS,OAAO,GAAE,OAAO,CAAC,KAAK,CAAC,iBAAiB,CAAkB;IA6CtE;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAOxB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAczB;;;OAGG;IACH,OAAO,CAAC,mBAAmB;IAY3B;;;;;OAKG;IACH,OAAO,CAAC,GAAG;IAoDX;;;;OAIG;IACI,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,QAAQ,EAAE,OAAO,EAAE;IAIpD;;;;OAIG;IACI,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,QAAQ,EAAE,OAAO,EAAE;IAIpD;;;;;;;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