node-ansi-logger 1.9.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Luligu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # NodeStorage
2
+
3
+ AnsiLogger is a lightweight, customizable color logger for Node.js.
4
+
5
+ ## Features
6
+
7
+ - Simple and intuitive API for data logging.
8
+ - Customizable colors and apperance.
9
+ - It is also possible to pass a top level logger (like Homebridge or Matter logger) and AnsiLogger will use it
10
+ for output instead of console.
11
+ - Includes also a fully customizable stringify funtions with colors.
12
+
13
+ ## Getting Started
14
+
15
+ ### Prerequisites
16
+
17
+ - Node.js installed on your machine.
18
+ - node-ansi-logger has no dependencies!
19
+
20
+ ### Installation
21
+
22
+ To get started with AnsiLogger in your package
23
+
24
+ ```bash
25
+ npm install node-ansi-logger
26
+ ```
27
+
28
+ # Usage
29
+
30
+ ## Initializing AnsiLogger:
31
+
32
+ Create an instance of AnsiLogger.
33
+
34
+ ```
35
+ import { AnsiLogger, AnsiLoggerParams, LogLevel } from 'node-ansi-logger';
36
+ ```
37
+
38
+ ```
39
+ const log = new AnsiLogger({logName: '<your name>'}); // Eventually other params in AnsiLoggerParams
40
+ ```
41
+
42
+ To import the stringify functions
43
+ ```
44
+ import { stringify, payloadStringify, colorStringify, mqttStringify, debugStringify } from 'node-ansi-logger';
45
+ ```
46
+
47
+ ## Using the logger:
48
+
49
+ ```
50
+ log.debug('Debug message...', ...parameters);
51
+ log.info('Info message...', ...parameters);
52
+ log.warn('Warning message', ...parameters);
53
+ log.error('Error message', ...parameters);
54
+ log(LogLevel.WARN, 'Warning message', ...parameters)
55
+ ```
56
+
57
+ ## Using the logger with colors inside the message:
58
+
59
+ ```
60
+ log.debug(`Debug message ${YELLOW}with yellow part${db}`, ...);
61
+ ```
62
+
63
+ ## Using the logger internal timer:
64
+ ```
65
+ log.startTimer('Time sensitive code started')
66
+ log.stopTimer('Time sensitive code finished')
67
+ ```
68
+
69
+ ## Using the stringify function:
70
+ ```
71
+ stringify({...})
72
+ colorStringify({...})
73
+ ```
74
+
75
+ # Screenshot
76
+
77
+ ![Example Image](https://github.com/Luligu/node-ansi-logger/blob/main/Screenshot.png)
78
+
79
+ # Contributing
80
+
81
+ Contributions to AnsiLogger are welcome.
82
+
83
+ # License
84
+
85
+ This project is licensed under the MIT License - see the LICENSE file for details.
@@ -0,0 +1,3 @@
1
+ export * from './logger.js';
2
+ export * from './stringify.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './logger.js';
2
+ export * from './stringify.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1,186 @@
1
+ /**
2
+ * This file contains the class CustomLogger.
3
+ *
4
+ * @file logger.ts
5
+ * @author Luca Liguori
6
+ * @date 2023-06-01
7
+ * @version 1.8.8
8
+ *
9
+ * All rights reserved.
10
+ *
11
+ */
12
+ export declare const RESET = "\u001B[40;0m";
13
+ export declare const BRIGHT = "\u001B[1m";
14
+ export declare const DIM = "\u001B[2m";
15
+ export declare const UNDERLINE = "\u001B[4m";
16
+ export declare const UNDERLINEOFF = "\u001B[24m";
17
+ export declare const BLINK = "\u001B[5m";
18
+ export declare const BLINKOFF = "\u001B[25m";
19
+ export declare const REVERSE = "\u001B[7m";
20
+ export declare const REVERSEOFF = "\u001B[27m";
21
+ export declare const HIDDEN = "\u001B[8m";
22
+ export declare const HIDDENOFF = "\u001B[28m";
23
+ export declare const CURSORSAVE = "\u001B[s";
24
+ export declare const CURSORRESTORE = "\u001B[u";
25
+ export declare const BLACK = "\u001B[30m";
26
+ export declare const RED = "\u001B[31m";
27
+ export declare const GREEN = "\u001B[32m";
28
+ export declare const YELLOW = "\u001B[33m";
29
+ export declare const BLUE = "\u001B[34m";
30
+ export declare const MAGENTA = "\u001B[35m";
31
+ export declare const CYAN = "\u001B[36m";
32
+ export declare const LIGHT_GREY = "\u001B[37m";
33
+ export declare const GREY = "\u001B[90m";
34
+ export declare const WHITE = "\u001B[97m";
35
+ export declare const db = "\u001B[38;5;247m";
36
+ export declare const nf = "\u001B[38;5;255m";
37
+ export declare const wr = "\u001B[38;5;220m";
38
+ export declare const er = "\u001B[38;5;9m";
39
+ export declare const rs = "\u001B[40;0m";
40
+ export declare const rk = "\u001B[K";
41
+ export declare const dn = "\u001B[38;5;33m";
42
+ export declare const gn = "\u001B[38;5;35m";
43
+ export declare const idn = "\u001B[48;5;21m\u001B[38;5;255m";
44
+ export declare const ign = "\u001B[48;5;22m\u001B[38;5;255m";
45
+ export declare const zb = "\u001B[38;5;207m";
46
+ export declare const hk = "\u001B[38;5;79m";
47
+ export declare const pl = "\u001B[32m";
48
+ export declare const id = "\u001B[37;44m";
49
+ export declare const or = "\u001B[38;5;208m";
50
+ /**
51
+ * LogLevel enumeration to specify the logging level.
52
+ */
53
+ export declare const enum LogLevel {
54
+ INFO = "info",
55
+ WARN = "warn",
56
+ ERROR = "error",
57
+ DEBUG = "debug"
58
+ }
59
+ /**
60
+ * Logger interface for custom loggers that can be passed to AnsiLogger for output instead of console output.
61
+ */
62
+ export interface Logger {
63
+ debug: (...data: any[]) => void;
64
+ info: (...data: any[]) => void;
65
+ warn: (...data: any[]) => void;
66
+ error: (...data: any[]) => void;
67
+ log: (level: LogLevel, message: string, ...parameters: any[]) => void;
68
+ }
69
+ /**
70
+ * TimestampFormat enumeration to specify the format of timestamps in log messages.
71
+ */
72
+ export declare const enum TimestampFormat {
73
+ ISO = 0,
74
+ LOCAL_DATE = 1,
75
+ LOCAL_TIME = 2,
76
+ LOCAL_DATE_TIME = 3,
77
+ TIME_MILLIS = 4,
78
+ HOMEBRIDGE = 5,
79
+ CUSTOM = 6
80
+ }
81
+ /**
82
+ * Parameters for configuring an AnsiLogger instance.
83
+ */
84
+ export interface AnsiLoggerParams {
85
+ hbLog?: Logger;
86
+ logName?: string;
87
+ logDebug?: boolean;
88
+ logWithColors?: boolean;
89
+ logTimestampFormat?: TimestampFormat;
90
+ logCustomTimestampFormat?: string;
91
+ }
92
+ /**
93
+ * AnsiLogger provides a customizable logging utility with ANSI color support.
94
+ * It allows for various configurations such as enabling debug logs, customizing log name, and more.
95
+ */
96
+ export declare class AnsiLogger {
97
+ private logName;
98
+ private logTimestampFormat;
99
+ private logCustomTimestampFormat;
100
+ private hbLog;
101
+ private logStartTime;
102
+ private logWithColors;
103
+ private logDebug;
104
+ private params;
105
+ /**
106
+ * Constructs a new AnsiLogger instance with optional configuration parameters.
107
+ * @param {AnsiLoggerParams} optionalParams - Configuration options for the logger.
108
+ */
109
+ constructor(optionalParams: AnsiLoggerParams);
110
+ /**
111
+ * Sets the name of the logger.
112
+ * @param {string} name - The new name for the logger.
113
+ */
114
+ setLogName(name: string): void;
115
+ /**
116
+ * Enables or disables debug logging.
117
+ * @param {boolean} logDebug - Flag to enable or disable debug logging.
118
+ */
119
+ setLogDebug(logDebug: boolean): void;
120
+ /**
121
+ * Enables or disables logging with ANSI colors.
122
+ * @param {boolean} logWithColors - Flag to enable or disable ANSI color logging.
123
+ */
124
+ setlogWithColors(logWithColors: boolean): void;
125
+ /**
126
+ * Sets the timestamp format for log messages.
127
+ * @param {TimestampFormat} format - The timestamp format to use.
128
+ */
129
+ setLogTimestampFormat(format: TimestampFormat): void;
130
+ /**
131
+ * Sets a custom timestamp format for log messages.
132
+ * @param {string} format - The custom timestamp format string.
133
+ */
134
+ setLogCustomTimestampFormat(format: string): void;
135
+ /**
136
+ * Starts a timer with an optional message.
137
+ * @param {string} message - The message to log when starting the timer.
138
+ */
139
+ startTimer(message: string): void;
140
+ /**
141
+ * Stops the timer started by startTimer and logs the elapsed time.
142
+ * @param {string} message - The message to log along with the elapsed time.
143
+ */
144
+ stopTimer(message: string): void;
145
+ private formatCustomTimestamp;
146
+ private getTimestamp;
147
+ /**
148
+ * Logs a message with a specific level (e.g., info, warn, error, debug) and additional parameters.
149
+ * This method formats the log message with ANSI colors based on the log level and other logger settings.
150
+ * It supports dynamic parameters for more detailed and formatted logging.
151
+ *
152
+ * @param {LogLevel} level - The severity level of the log message.
153
+ * @param {string} message - The primary log message to be displayed.
154
+ * @param {...any[]} parameters - Additional parameters to be logged. Supports any number of parameters.
155
+ */
156
+ log(level: LogLevel, message: string, ...parameters: any[]): void;
157
+ /**
158
+ * Logs an informational message. This is a convenience method that delegates to the `log` method with the `LogLevel.INFO` level.
159
+ *
160
+ * @param {string} message - The informational message to log.
161
+ * @param {...any[]} parameters - Additional parameters to be included in the log message. Supports any number of parameters.
162
+ */
163
+ info(message: string, ...parameters: any[]): void;
164
+ /**
165
+ * Logs a warning message. This is a convenience method that delegates to the `log` method with the `LogLevel.WARN` level.
166
+ *
167
+ * @param {string} message - The warning message to log.
168
+ * @param {...any[]} parameters - Additional parameters to be included in the log message. Supports any number of parameters.
169
+ */
170
+ warn(message: string, ...parameters: any[]): void;
171
+ /**
172
+ * Logs an error message. This is a convenience method that delegates to the `log` method with the `LogLevel.ERROR` level.
173
+ *
174
+ * @param {string} message - The error message to log.
175
+ * @param {...any[]} parameters - Additional parameters to be included in the log message. Supports any number of parameters.
176
+ */
177
+ error(message: string, ...parameters: any[]): void;
178
+ /**
179
+ * Logs a debug message if debug logging is enabled. This is a convenience method that delegates to the `log` method with the `LogLevel.DEBUG` level.
180
+ *
181
+ * @param {string} message - The debug message to log.
182
+ * @param {...any[]} parameters - Additional parameters to be included in the log message. Supports any number of parameters.
183
+ */
184
+ debug(message: string, ...parameters: any[]): void;
185
+ }
186
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,eAAO,MAAM,KAAK,iBAAe,CAAC;AAClC,eAAO,MAAM,MAAM,cAAY,CAAC;AAChC,eAAO,MAAM,GAAG,cAAY,CAAC;AAC7B,eAAO,MAAM,SAAS,cAAY,CAAC;AACnC,eAAO,MAAM,YAAY,eAAa,CAAC;AACvC,eAAO,MAAM,KAAK,cAAY,CAAC;AAC/B,eAAO,MAAM,QAAQ,eAAa,CAAC;AACnC,eAAO,MAAM,OAAO,cAAY,CAAC;AACjC,eAAO,MAAM,UAAU,eAAa,CAAC;AACrC,eAAO,MAAM,MAAM,cAAY,CAAC;AAChC,eAAO,MAAM,SAAS,eAAa,CAAC;AACpC,eAAO,MAAM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BAAgB,CAAC,aAAa,EAAE,OAAO,GAAG,IAAI;IAIrD;;;OAGG;IACI,qBAAqB,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI;IAI3D;;;OAGG;IACI,2BAA2B,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAIxD;;;OAGG;IACI,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAKxC;;;OAGG;IACI,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAWvC,OAAO,CAAC,qBAAqB;IAkB7B,OAAO,CAAC,YAAY;IAgCpB;;;;;;;;OAQG;IAEI,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,UAAU,EAAE,GAAG,EAAE,GAAG,IAAI;IAwDxE;;;;;OAKG;IAEI,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,UAAU,EAAE,GAAG,EAAE,GAAG,IAAI;IAIxD;;;;;OAKG;IAEI,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,UAAU,EAAE,GAAG,EAAE,GAAG,IAAI;IAIxD;;;;;OAKG;IAEI,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,UAAU,EAAE,GAAG,EAAE,GAAG,IAAI;IAIzD;;;;;OAKG;IAEI,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,UAAU,EAAE,GAAG,EAAE,GAAG,IAAI;CAK1D"}
package/dist/logger.js ADDED
@@ -0,0 +1,347 @@
1
+ /**
2
+ * This file contains the class CustomLogger.
3
+ *
4
+ * @file logger.ts
5
+ * @author Luca Liguori
6
+ * @date 2023-06-01
7
+ * @version 1.8.8
8
+ *
9
+ * All rights reserved.
10
+ *
11
+ */
12
+ // ANSI color codes and styles are defined here for use in the logger
13
+ export const RESET = '\x1b[40;0m';
14
+ export const BRIGHT = '\x1b[1m';
15
+ export const DIM = '\x1b[2m';
16
+ export const UNDERLINE = '\x1b[4m';
17
+ export const UNDERLINEOFF = '\x1b[24m';
18
+ export const BLINK = '\x1b[5m';
19
+ export const BLINKOFF = '\x1b[25m';
20
+ export const REVERSE = '\x1b[7m';
21
+ export const REVERSEOFF = '\x1b[27m';
22
+ export const HIDDEN = '\x1b[8m';
23
+ export const HIDDENOFF = '\x1b[28m';
24
+ export const CURSORSAVE = '\x1b[s';
25
+ export const CURSORRESTORE = '\x1b[u';
26
+ export const BLACK = '\x1b[30m';
27
+ export const RED = '\x1b[31m';
28
+ export const GREEN = '\x1b[32m';
29
+ export const YELLOW = '\x1b[33m';
30
+ export const BLUE = '\x1b[34m';
31
+ export const MAGENTA = '\x1b[35m';
32
+ export const CYAN = '\x1b[36m';
33
+ export const LIGHT_GREY = '\x1b[37m';
34
+ export const GREY = '\x1b[90m';
35
+ export const WHITE = '\x1b[97m';
36
+ // ANSI color codes short form to use in the logger
37
+ export const db = '\x1b[38;5;247m'; // Debug
38
+ export const nf = '\x1b[38;5;255m'; // Info
39
+ export const wr = '\x1b[38;5;220m'; // Warn
40
+ export const er = '\x1b[38;5;9m'; // Error
41
+ export const rs = '\x1b[40;0m'; // Reset colors to default foreground and background
42
+ export const rk = '\x1b[K'; // Erase from cursor
43
+ // Used internally for: homebridge-mqtt-accessories and matterbridge-mqtt-accessories
44
+ export const dn = '\x1b[38;5;33m'; // Display name device
45
+ export const gn = '\x1b[38;5;35m'; // Display name group
46
+ export const idn = '\x1b[48;5;21m\x1b[38;5;255m'; // Inverted display name device
47
+ export const ign = '\x1b[48;5;22m\x1b[38;5;255m'; // Inverted display name group
48
+ export const zb = '\x1b[38;5;207m'; // Zigbee
49
+ export const hk = '\x1b[38;5;79m'; // Homekit
50
+ export const pl = '\x1b[32m'; // payload
51
+ export const id = '\x1b[37;44m'; // id or ieee_address or UUID
52
+ export const or = '\x1b[38;5;208m'; // history
53
+ /**
54
+ * AnsiLogger provides a customizable logging utility with ANSI color support.
55
+ * It allows for various configurations such as enabling debug logs, customizing log name, and more.
56
+ */
57
+ export class AnsiLogger {
58
+ logName;
59
+ logTimestampFormat;
60
+ logCustomTimestampFormat;
61
+ hbLog;
62
+ logStartTime;
63
+ logWithColors;
64
+ logDebug;
65
+ params;
66
+ /**
67
+ * Constructs a new AnsiLogger instance with optional configuration parameters.
68
+ * @param {AnsiLoggerParams} optionalParams - Configuration options for the logger.
69
+ */
70
+ constructor(optionalParams) {
71
+ this.params = Object.assign({
72
+ hbLog: undefined,
73
+ logName: 'NodeAnsiLogger',
74
+ logDebug: true,
75
+ logWithColors: true,
76
+ logTimestampFormat: 3 /* TimestampFormat.LOCAL_DATE_TIME */,
77
+ logCustomTimestampFormat: 'yyyy-MM-dd HH:mm:ss',
78
+ }, optionalParams);
79
+ this.hbLog = this.params.hbLog;
80
+ this.logName = this.params.logName;
81
+ this.logDebug = this.params.logDebug;
82
+ this.logWithColors = this.params.logWithColors;
83
+ this.logTimestampFormat = this.params.logTimestampFormat;
84
+ this.logCustomTimestampFormat = this.params.logCustomTimestampFormat;
85
+ this.logStartTime = 0;
86
+ }
87
+ /**
88
+ * Sets the name of the logger.
89
+ * @param {string} name - The new name for the logger.
90
+ */
91
+ setLogName(name) {
92
+ this.logName = name;
93
+ }
94
+ /**
95
+ * Enables or disables debug logging.
96
+ * @param {boolean} logDebug - Flag to enable or disable debug logging.
97
+ */
98
+ setLogDebug(logDebug) {
99
+ this.logDebug = logDebug;
100
+ }
101
+ /**
102
+ * Enables or disables logging with ANSI colors.
103
+ * @param {boolean} logWithColors - Flag to enable or disable ANSI color logging.
104
+ */
105
+ setlogWithColors(logWithColors) {
106
+ this.logWithColors = logWithColors;
107
+ }
108
+ /**
109
+ * Sets the timestamp format for log messages.
110
+ * @param {TimestampFormat} format - The timestamp format to use.
111
+ */
112
+ setLogTimestampFormat(format) {
113
+ this.logTimestampFormat = format;
114
+ }
115
+ /**
116
+ * Sets a custom timestamp format for log messages.
117
+ * @param {string} format - The custom timestamp format string.
118
+ */
119
+ setLogCustomTimestampFormat(format) {
120
+ this.logCustomTimestampFormat = format;
121
+ }
122
+ /**
123
+ * Starts a timer with an optional message.
124
+ * @param {string} message - The message to log when starting the timer.
125
+ */
126
+ startTimer(message) {
127
+ this.logStartTime = Date.now();
128
+ this.info(`Timer started ${message}`);
129
+ }
130
+ /**
131
+ * Stops the timer started by startTimer and logs the elapsed time.
132
+ * @param {string} message - The message to log along with the elapsed time.
133
+ */
134
+ stopTimer(message) {
135
+ if (this.logStartTime !== 0) {
136
+ const timePassed = Date.now() - this.logStartTime;
137
+ this.info(`Timer stoppped at ${timePassed} ms ${message}`);
138
+ }
139
+ this.logStartTime = 0;
140
+ }
141
+ // This function formats a date object into a custom string format.
142
+ // For simplicity, it only handles years, months, days, hours, minutes, and seconds
143
+ // with this format 'yyyy-MM-dd HH:mm:ss'
144
+ formatCustomTimestamp(date, formatString) {
145
+ const year = date.getFullYear();
146
+ const month = date.getMonth() + 1; // getMonth() returns 0-11
147
+ const day = date.getDate();
148
+ const hours = date.getHours();
149
+ const minutes = date.getMinutes();
150
+ const seconds = date.getSeconds();
151
+ // Replace format tokens with actual values. Add more as needed.
152
+ return formatString
153
+ .replace('yyyy', year.toString())
154
+ .replace('MM', month.toString().padStart(2, '0'))
155
+ .replace('dd', day.toString().padStart(2, '0'))
156
+ .replace('HH', hours.toString().padStart(2, '0'))
157
+ .replace('mm', minutes.toString().padStart(2, '0'))
158
+ .replace('ss', seconds.toString().padStart(2, '0'));
159
+ }
160
+ getTimestamp() {
161
+ if (this.logStartTime !== 0) {
162
+ const timePassed = Date.now() - this.logStartTime;
163
+ return `Timer: ${timePassed.toString().padStart(7, ' ')} ms`;
164
+ }
165
+ else {
166
+ let timestamp;
167
+ switch (this.logTimestampFormat) {
168
+ case 1 /* TimestampFormat.LOCAL_DATE */:
169
+ timestamp = new Date().toLocaleDateString();
170
+ break;
171
+ case 2 /* TimestampFormat.LOCAL_TIME */:
172
+ timestamp = new Date().toLocaleTimeString();
173
+ break;
174
+ case 5 /* TimestampFormat.HOMEBRIDGE */:
175
+ case 3 /* TimestampFormat.LOCAL_DATE_TIME */:
176
+ timestamp = new Date().toLocaleString();
177
+ break;
178
+ case 0 /* TimestampFormat.ISO */:
179
+ timestamp = new Date().toISOString();
180
+ break;
181
+ case 4 /* TimestampFormat.TIME_MILLIS */:
182
+ // eslint-disable-next-line max-len
183
+ timestamp = `${new Date().getHours().toString().padStart(2, '0')}:${new Date().getMinutes().toString().padStart(2, '0')}:${new Date().getSeconds().toString().padStart(2, '0')}.${new Date().getMilliseconds().toString().padStart(3, '0')}`;
184
+ break;
185
+ case 6 /* TimestampFormat.CUSTOM */:
186
+ timestamp = this.formatCustomTimestamp(new Date(), this.logCustomTimestampFormat);
187
+ break;
188
+ }
189
+ return timestamp;
190
+ }
191
+ }
192
+ /**
193
+ * Logs a message with a specific level (e.g., info, warn, error, debug) and additional parameters.
194
+ * This method formats the log message with ANSI colors based on the log level and other logger settings.
195
+ * It supports dynamic parameters for more detailed and formatted logging.
196
+ *
197
+ * @param {LogLevel} level - The severity level of the log message.
198
+ * @param {string} message - The primary log message to be displayed.
199
+ * @param {...any[]} parameters - Additional parameters to be logged. Supports any number of parameters.
200
+ */
201
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
202
+ log(level, message, ...parameters) {
203
+ const ts = '\x1b[38;5;249m'; // TimeStamp White medium
204
+ const ln = '\x1b[38;5;31m'; // LogName Cyan
205
+ const s1ln = '\x1b[38;5;0;48;5;31m'; // Highlight LogName Black on Cyan
206
+ const s2ln = '\x1b[38;5;0;48;5;255m'; // Highlight LogName Black on White
207
+ const s3ln = '\x1b[38;5;0;48;5;220m'; // Highlight LogName Black on Yellow
208
+ const s4ln = '\x1b[38;5;0;48;5;9m'; // Highlight LogName Black on Red
209
+ if (this.hbLog !== undefined) {
210
+ this.hbLog[level](message, ...parameters);
211
+ }
212
+ else {
213
+ if (this.logWithColors) {
214
+ let logNameColor = ln;
215
+ if (typeof message !== 'string' || message.startsWith === undefined) {
216
+ logNameColor = ln;
217
+ }
218
+ else if (message.startsWith('****')) {
219
+ logNameColor = s4ln;
220
+ message = message.slice(4);
221
+ }
222
+ else if (message.startsWith('***')) {
223
+ logNameColor = s3ln;
224
+ message = message.slice(3);
225
+ }
226
+ else if (message.startsWith('**')) {
227
+ logNameColor = s2ln;
228
+ message = message.slice(2);
229
+ }
230
+ else if (message.startsWith('*')) {
231
+ logNameColor = s1ln;
232
+ message = message.slice(1);
233
+ }
234
+ switch (level) {
235
+ case "debug" /* LogLevel.DEBUG */:
236
+ if (this.logDebug) {
237
+ // eslint-disable-next-line no-console
238
+ console.log(`${rs}${ts}[${this.getTimestamp()}] ${logNameColor}[${this.logName}]${rs}${db}`, message, ...parameters, rs + rk);
239
+ }
240
+ break;
241
+ case "info" /* LogLevel.INFO */:
242
+ // eslint-disable-next-line no-console
243
+ console.log(`${rs}${ts}[${this.getTimestamp()}] ${logNameColor}[${this.logName}]${rs}${nf}`, message, ...parameters, rs + rk);
244
+ break;
245
+ case "warn" /* LogLevel.WARN */:
246
+ // eslint-disable-next-line no-console
247
+ console.log(`${rs}${ts}[${this.getTimestamp()}] ${logNameColor}[${this.logName}]${rs}${wr}`, message, ...parameters, rs + rk);
248
+ break;
249
+ case "error" /* LogLevel.ERROR */:
250
+ // eslint-disable-next-line no-console
251
+ console.log(`${rs}${ts}[${this.getTimestamp()}] ${logNameColor}[${this.logName}]${rs}${er}`, message, ...parameters, rs + rk);
252
+ break;
253
+ }
254
+ }
255
+ else {
256
+ // eslint-disable-next-line no-console
257
+ console.log(`${rs}[${this.getTimestamp()}] [${this.logName}] [${level}] ${message}`, ...parameters);
258
+ }
259
+ }
260
+ }
261
+ /**
262
+ * Logs an informational message. This is a convenience method that delegates to the `log` method with the `LogLevel.INFO` level.
263
+ *
264
+ * @param {string} message - The informational message to log.
265
+ * @param {...any[]} parameters - Additional parameters to be included in the log message. Supports any number of parameters.
266
+ */
267
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
268
+ info(message, ...parameters) {
269
+ this.log("info" /* LogLevel.INFO */, message, ...parameters);
270
+ }
271
+ /**
272
+ * Logs a warning message. This is a convenience method that delegates to the `log` method with the `LogLevel.WARN` level.
273
+ *
274
+ * @param {string} message - The warning message to log.
275
+ * @param {...any[]} parameters - Additional parameters to be included in the log message. Supports any number of parameters.
276
+ */
277
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
278
+ warn(message, ...parameters) {
279
+ this.log("warn" /* LogLevel.WARN */, message, ...parameters);
280
+ }
281
+ /**
282
+ * Logs an error message. This is a convenience method that delegates to the `log` method with the `LogLevel.ERROR` level.
283
+ *
284
+ * @param {string} message - The error message to log.
285
+ * @param {...any[]} parameters - Additional parameters to be included in the log message. Supports any number of parameters.
286
+ */
287
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
288
+ error(message, ...parameters) {
289
+ this.log("error" /* LogLevel.ERROR */, message, ...parameters);
290
+ }
291
+ /**
292
+ * Logs a debug message if debug logging is enabled. This is a convenience method that delegates to the `log` method with the `LogLevel.DEBUG` level.
293
+ *
294
+ * @param {string} message - The debug message to log.
295
+ * @param {...any[]} parameters - Additional parameters to be included in the log message. Supports any number of parameters.
296
+ */
297
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
298
+ debug(message, ...parameters) {
299
+ if (this.logDebug) {
300
+ this.log("debug" /* LogLevel.DEBUG */, message, ...parameters);
301
+ }
302
+ }
303
+ }
304
+ /*
305
+ \x1b[0m - Reset (clear color)
306
+ \x1b[1m - Bold
307
+ \x1b[3m - Italic
308
+ \x1b[4m - Underline
309
+ \x1b[K - Erase the line from cursor
310
+
311
+ \x1b[30m - Black
312
+ \x1b[31m - Red
313
+ \x1b[32m - Green
314
+ \x1b[33m - Yellow
315
+ \x1b[34m - Blue
316
+ \x1b[35m - Magenta
317
+ \x1b[36m - Cyan
318
+ \x1b[37m - White
319
+
320
+ \x1b[90-97m - Bright
321
+
322
+ \x1b[40m - Black background
323
+ \x1b[41m - Red background
324
+ \x1b[42m - Green background
325
+ \x1b[43m - Yellow background
326
+ \x1b[44m - Blue background
327
+ \x1b[45m - Magenta background
328
+ \x1b[46m - Cyan background
329
+ \x1b[47m - White background
330
+
331
+ \x1b[100-107m - Bright background
332
+
333
+ \x1b[38;2;255;105;50m // Orange
334
+
335
+ RGB foreground
336
+ \x1b[38;2;<R>;<G>;<B>m
337
+
338
+ RGB background
339
+ \x1b[48;2;<R>;<G>;<B>m
340
+
341
+ 256 colors foreground
342
+ \x1b[38;5;<FG COLOR>m
343
+
344
+ 256 colors background
345
+ \x1b[48;5;<BG COLOR>m
346
+ */
347
+ //# sourceMappingURL=logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,qEAAqE;AACrE,MAAM,CAAC,MAAM,KAAK,GAAG,YAAY,CAAC;AAClC,MAAM,CAAC,MAAM,MAAM,GAAG,SAAS,CAAC;AAChC,MAAM,CAAC,MAAM,GAAG,GAAG,SAAS,CAAC;AAC7B,MAAM,CAAC,MAAM,SAAS,GAAG,SAAS,CAAC;AACnC,MAAM,CAAC,MAAM,YAAY,GAAG,UAAU,CAAC;AACvC,MAAM,CAAC,MAAM,KAAK,GAAG,SAAS,CAAC;AAC/B,MAAM,CAAC,MAAM,QAAQ,GAAG,UAAU,CAAC;AACnC,MAAM,CAAC,MAAM,OAAO,GAAG,SAAS,CAAC;AACjC,MAAM,CAAC,MAAM,UAAU,GAAG,UAAU,CAAC;AACrC,MAAM,CAAC,MAAM,MAAM,GAAG,SAAS,CAAC;AAChC,MAAM,CAAC,MAAM,SAAS,GAAG,UAAU,CAAC;AACpC,MAAM,CAAC,MAAM,UAAU,GAAG,QAAQ,CAAC;AACnC,MAAM,CAAC,MAAM,aAAa,GAAG,QAAQ,CAAC;AAEtC,MAAM,CAAC,MAAM,KAAK,GAAG,UAAU,CAAC;AAChC,MAAM,CAAC,MAAM,GAAG,GAAG,UAAU,CAAC;AAC9B,MAAM,CAAC,M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C;IAED;;;;;OAKG;IACH,8DAA8D;IACvD,IAAI,CAAC,OAAe,EAAE,GAAG,UAAiB;QAC/C,IAAI,CAAC,GAAG,6BAAgB,OAAO,EAAE,GAAG,UAAU,CAAC,CAAC;IAClD,CAAC;IAED;;;;;OAKG;IACH,8DAA8D;IACvD,KAAK,CAAC,OAAe,EAAE,GAAG,UAAiB;QAChD,IAAI,CAAC,GAAG,+BAAiB,OAAO,EAAE,GAAG,UAAU,CAAC,CAAC;IACnD,CAAC;IAED;;;;;OAKG;IACH,8DAA8D;IACvD,KAAK,CAAC,OAAe,EAAE,GAAG,UAAiB;QAChD,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClB,IAAI,CAAC,GAAG,+BAAiB,OAAO,EAAE,GAAG,UAAU,CAAC,CAAC;QACnD,CAAC;IACH,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA0CE"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * This file contains the functions stringify.
3
+ *
4
+ * @file stringify.ts
5
+ * @author Luca Liguori
6
+ * @date 2023-07-23
7
+ * @version 1.4.1
8
+ *
9
+ * All rights reserved.
10
+ *
11
+ */
12
+ export declare function payloadStringify(payload: object): string;
13
+ export declare function colorStringify(payload: object): string;
14
+ export declare function historyStringify(payload: object): string;
15
+ export declare function mqttStringify(payload: object): string;
16
+ export declare function debugStringify(payload: object): string;
17
+ export declare function stringify(payload: object, enableColors?: boolean, colorPayload?: number, colorKey?: number, colorString?: number, colorNumber?: number, colorBoolean?: number, colorUndefined?: number, keyQuote?: string, stringQuote?: string): string;
18
+ //# sourceMappingURL=stringify.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stringify.d.ts","sourceRoot":"","sources":["../src/stringify.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AAEF,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAExD;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEtD;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAExD;AAED,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEtD;AAED,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,UAAQ,EAAE,YAAY,SAAM,EAAE,QAAQ,SAAM,EAAE,WAAW,SAAK,EAAE,WAAW,SAAM,EACtI,YAAY,SAAM,EAAE,cAAc,SAAI,EAAE,QAAQ,SAAK,EAAE,WAAW,SAAO,GAAG,MAAM,CA4CnF"}
@@ -0,0 +1,81 @@
1
+ /**
2
+ * This file contains the functions stringify.
3
+ *
4
+ * @file stringify.ts
5
+ * @author Luca Liguori
6
+ * @date 2023-07-23
7
+ * @version 1.4.1
8
+ *
9
+ * All rights reserved.
10
+ *
11
+ */
12
+ export function payloadStringify(payload) {
13
+ return stringify(payload, false, 0, 0, 0, 0, 0, 0, '"', '"');
14
+ }
15
+ export function colorStringify(payload) {
16
+ return stringify(payload, true, 255, 255, 35, 220, 159, 1);
17
+ }
18
+ export function historyStringify(payload) {
19
+ return stringify(payload, true, 0, 208, 247, 247, 247, 247);
20
+ }
21
+ export function mqttStringify(payload) {
22
+ return stringify(payload, true, 69, 247);
23
+ }
24
+ export function debugStringify(payload) {
25
+ return stringify(payload, true, 247, 247);
26
+ }
27
+ export function stringify(payload, enableColors = false, colorPayload = 255, colorKey = 255, colorString = 35, colorNumber = 220, colorBoolean = 159, colorUndefined = 1, keyQuote = '', stringQuote = '\'') {
28
+ const clr = (color) => {
29
+ return enableColors ? `\x1b[38;5;${color}m` : '';
30
+ };
31
+ const reset = () => {
32
+ return enableColors ? '\x1b[0m' : '';
33
+ };
34
+ const isArray = Array.isArray(payload);
35
+ let string = `${reset()}${clr(colorPayload)}` + (isArray ? '[ ' : '{ ');
36
+ Object.entries(payload).forEach(([key, value], index) => {
37
+ if (index > 0) {
38
+ string += ', ';
39
+ }
40
+ let newValue = '';
41
+ newValue = value;
42
+ //console.log(typeof newValue, key, value);
43
+ if (value === null) {
44
+ newValue = `${clr(colorUndefined)}null${reset()}`;
45
+ }
46
+ else if (typeof newValue === 'string') {
47
+ newValue = `${clr(colorString)}${stringQuote}${newValue}${stringQuote}${reset()}`;
48
+ }
49
+ else if (typeof newValue === 'number') {
50
+ newValue = `${clr(colorNumber)}${newValue}${reset()}`;
51
+ }
52
+ else if (typeof newValue === 'boolean') {
53
+ newValue = `${clr(colorBoolean)}${newValue}${reset()}`;
54
+ }
55
+ else if (typeof newValue === 'undefined') {
56
+ newValue = `${clr(colorUndefined)}undefined${reset()}`;
57
+ }
58
+ else if (typeof newValue === 'object') {
59
+ if (Object.keys(newValue).length < 100) {
60
+ newValue = stringify(newValue, enableColors, colorPayload, colorKey, colorString, colorNumber, colorBoolean, colorUndefined, keyQuote, stringQuote);
61
+ }
62
+ else {
63
+ newValue = '{...}';
64
+ }
65
+ }
66
+ else if (typeof newValue === 'bigint') {
67
+ newValue = `${clr(colorNumber + 1)}${newValue}${reset()}`;
68
+ }
69
+ else {
70
+ throw new Error('Stringify unknown type');
71
+ }
72
+ if (isArray) {
73
+ string += `${newValue}`;
74
+ }
75
+ else {
76
+ string += `${clr(colorKey)}${keyQuote}${key}${keyQuote}${reset()}: ${newValue}`;
77
+ }
78
+ });
79
+ return string += ` ${clr(colorPayload)}` + (isArray ? ']' : '}') + `${reset()}`;
80
+ }
81
+ //# sourceMappingURL=stringify.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stringify.js","sourceRoot":"","sources":["../src/stringify.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AAEF,MAAM,UAAU,gBAAgB,CAAC,OAAe;IAC9C,OAAO,SAAS,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CA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"license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/Luligu/node-ansi-logger"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/Luligu/node-ansi-logger/issues"
13
+ },
14
+ "engines": {
15
+ "node": ">=18.0.0"
16
+ },
17
+ "type": "module",
18
+ "main": "dist/index.js",
19
+ "types": "dist/index.d.js",
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "watch": "tsc --watch",
23
+ "test": "jest",
24
+ "test:verbose": "jest --verbose",
25
+ "test:watch": "jest --watch",
26
+ "lint": "eslint src/**.ts",
27
+ "lint:fix": "eslint src/**.ts --fix",
28
+ "clean": "rimraf tsconfig.tsbuildinfo ./dist",
29
+ "cleanBuild": "npm run clean && tsc",
30
+ "deepClean": "rimraf tsconfig.tsbuildinfo package-lock.json ./dist ./node_modules",
31
+ "prepublishOnly": "npm run lint && npm run cleanBuild",
32
+ "checkDependencies": "npx npm-check-updates",
33
+ "updateDependencies": "npx npm-check-updates -u"
34
+ },
35
+ "keywords": [
36
+ "node-ansi-logger",
37
+ "ansi escape",
38
+ "color",
39
+ "logger"
40
+ ],
41
+ "devDependencies": {
42
+ "@types/jest": "^29.5.12",
43
+ "@types/node": "^20.11.19",
44
+ "@typescript-eslint/eslint-plugin": "^7.0.2",
45
+ "@typescript-eslint/parser": "^7.0.2",
46
+ "eslint": "^8.56.0",
47
+ "jest": "^29.7.0",
48
+ "ts-jest": "^29.1.2",
49
+ "typescript": "^5.3.3"
50
+ }
51
+ }