@decaf-ts/logging 0.2.3 → 0.3.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.
@@ -2,121 +2,207 @@ import { LoggerFactory, LoggingConfig, LoggingContext, StringLike, Theme, Logger
2
2
  import { LogLevel } from "./constants";
3
3
  /**
4
4
  * @description A minimal logger implementation.
5
- * @summary MiniLogger is a lightweight logging class that implements the VerbosityLogger interface.
6
- * It provides basic logging functionality with support for different log levels and verbosity.
5
+ * @summary MiniLogger is a lightweight logging class that implements the Logger interface.
6
+ * It provides basic logging functionality with support for different log levels, verbosity,
7
+ * context-aware logging, and customizable formatting.
8
+ * @param {string} context - The context (typically class name) this logger is associated with
9
+ * @param {Partial<LoggingConfig>} conf - Optional configuration to override global settings
10
+ * @class MiniLogger
11
+ * @example
12
+ * // Create a new logger for a class
13
+ * const logger = new MiniLogger('MyClass');
7
14
  *
8
- * @class
15
+ * // Log messages at different levels
16
+ * logger.info('This is an info message');
17
+ * logger.debug('This is a debug message');
18
+ * logger.error('Something went wrong');
19
+ *
20
+ * // Create a child logger for a specific method
21
+ * const methodLogger = logger.for('myMethod');
22
+ * methodLogger.verbose('Detailed information', 2);
23
+ *
24
+ * // Log with custom configuration
25
+ * logger.for('specialMethod', { style: true }).info('Styled message');
9
26
  */
10
27
  export declare class MiniLogger implements Logger {
11
28
  protected context: string;
12
29
  protected conf?: Partial<LoggingConfig> | undefined;
13
- /**
14
- * @description Creates a new MiniLogger instance.
15
- * @summary Initializes a MiniLogger with the given class name, optional configuration, and method name.
16
- *
17
- * @param context - The name of the class using this logger.
18
- * @param [conf] - Optional logging configuration. Defaults to Info level and verbosity 0.
19
- * @param [id] - Optional unique identifier for the logger instance.
20
- */
21
30
  constructor(context: string, conf?: Partial<LoggingConfig> | undefined);
22
31
  protected config(key: keyof LoggingConfig): LoggingConfig[keyof LoggingConfig];
23
- for(method?: string | ((...args: any[]) => any), config?: Partial<LoggingConfig>, ...args: any[]): Logger;
24
32
  /**
25
- * @description Creates a formatted log string.
26
- * @summary Generates a log string with timestamp, colored log level, and message.
27
- *
28
- * @param level - The log level as a string.
29
- * @param message
30
- * @param stack
31
- * @return A formatted log string.
33
+ * @description Creates a child logger for a specific method or context
34
+ * @summary Returns a new logger instance with the current context extended by the specified method name
35
+ * @param {string | Function} method - The method name or function to create a logger for
36
+ * @param {Partial<LoggingConfig>} config - Optional configuration to override settings
37
+ * @param {...any} args - Additional arguments to pass to the logger factory
38
+ * @return {Logger} A new logger instance for the specified method
39
+ */
40
+ for(method?: string | ((...args: any[]) => any), config?: Partial<LoggingConfig>): Logger;
41
+ /**
42
+ * @description Creates a formatted log string
43
+ * @summary Generates a log string with timestamp, colored log level, context, and message
44
+ * @param {LogLevel} level - The log level for this message
45
+ * @param {StringLike | Error} message - The message to log or an Error object
46
+ * @param {string} [stack] - Optional stack trace to include in the log
47
+ * @return {string} A formatted log string with all components
32
48
  */
33
49
  protected createLog(level: LogLevel, message: StringLike | Error, stack?: string): string;
34
50
  /**
35
- * @description Logs a message with the specified log level.
51
+ * @description Logs a message with the specified log level
36
52
  * @summary Checks if the message should be logged based on the current log level,
37
- * then uses the appropriate console method to output the log.
38
- *
39
- * @param level - The log level of the message.
40
- * @param msg - The message to be logged.
41
- * @param stack
53
+ * then uses the appropriate console method to output the formatted log
54
+ * @param {LogLevel} level - The log level of the message
55
+ * @param {StringLike | Error} msg - The message to be logged or an Error object
56
+ * @param {string} [stack] - Optional stack trace to include in the log
57
+ * @return {void}
42
58
  */
43
59
  protected log(level: LogLevel, msg: StringLike | Error, stack?: string): void;
44
60
  /**
45
- * @description LLogs a `way too verbose` or a silly message.
46
- * @summary Logs a message at the Silly level if the current verbosity allows it.
47
- *
48
- * @param msg - The message to be logged.
49
- * @param verbosity - The verbosity level of the message (default: 0).
61
+ * @description Logs a message at the silly level
62
+ * @summary Logs a message at the silly level if the current verbosity setting allows it
63
+ * @param {StringLike} msg - The message to be logged
64
+ * @param {number} [verbosity=0] - The verbosity level of the message
65
+ * @return {void}
50
66
  */
51
67
  silly(msg: StringLike, verbosity?: number): void;
52
68
  /**
53
- * @description Logs a verbose message.
54
- * @summary Logs a message at the Verbose level if the current verbosity allows it.
55
- *
56
- * @param msg - The message to be logged.
57
- * @param verbosity - The verbosity level of the message (default: 0).
69
+ * @description Logs a message at the verbose level
70
+ * @summary Logs a message at the verbose level if the current verbosity setting allows it
71
+ * @param {StringLike} msg - The message to be logged
72
+ * @param {number} [verbosity=0] - The verbosity level of the message
73
+ * @return {void}
58
74
  */
59
75
  verbose(msg: StringLike, verbosity?: number): void;
60
76
  /**
61
- * @description Logs an info message.
62
- * @summary Logs a message at the Info level.
63
- *
64
- * @param msg - The message to be logged.
77
+ * @description Logs a message at the info level
78
+ * @summary Logs a message at the info level for general application information
79
+ * @param {StringLike} msg - The message to be logged
80
+ * @return {void}
65
81
  */
66
82
  info(msg: StringLike): void;
67
83
  /**
68
- * @description Logs a debug message.
69
- * @summary Logs a message at the Debug level.
70
- *
71
- * @param msg - The message to be logged.
84
+ * @description Logs a message at the debug level
85
+ * @summary Logs a message at the debug level for detailed troubleshooting information
86
+ * @param {StringLike} msg - The message to be logged
87
+ * @return {void}
72
88
  */
73
89
  debug(msg: StringLike): void;
74
90
  /**
75
- * @description Logs an error message.
76
- * @summary Logs a message at the Error level.
77
- *
78
- * @param msg - The message to be logged.
91
+ * @description Logs a message at the error level
92
+ * @summary Logs a message at the error level for errors and exceptions
93
+ * @param {StringLike | Error} msg - The message to be logged or an Error object
94
+ * @return {void}
79
95
  */
80
96
  error(msg: StringLike | Error): void;
97
+ /**
98
+ * @description Updates the logger configuration
99
+ * @summary Merges the provided configuration with the existing configuration
100
+ * @param {Partial<LoggingConfig>} config - The configuration options to apply
101
+ * @return {void}
102
+ */
81
103
  setConfig(config: Partial<LoggingConfig>): void;
82
104
  }
83
105
  /**
84
- * @description A static class for managing logging operations.
106
+ * @description A static class for managing logging operations
85
107
  * @summary The Logging class provides a centralized logging mechanism with support for
86
- * different log levels and verbosity. It uses a singleton pattern to maintain a global
108
+ * different log levels, verbosity, and styling. It uses a singleton pattern to maintain a global
87
109
  * logger instance and allows creating specific loggers for different classes and methods.
110
+ * @class Logging
111
+ * @example
112
+ * // Set global configuration
113
+ * Logging.setConfig({ level: LogLevel.debug, style: true });
114
+ *
115
+ * // Get a logger for a specific class
116
+ * const logger = Logging.for('MyClass');
117
+ *
118
+ * // Log messages at different levels
119
+ * logger.info('Application started');
120
+ * logger.debug('Processing data...');
121
+ *
122
+ * // Log with context
123
+ * const methodLogger = Logging.for('MyClass.myMethod');
124
+ * methodLogger.verbose('Detailed operation information', 1);
125
+ *
126
+ * // Log errors
127
+ * try {
128
+ * // some operation
129
+ * } catch (error) {
130
+ * logger.error(error);
131
+ * }
132
+ * @mermaid
133
+ * classDiagram
134
+ * class Logger {
135
+ * <<interface>>
136
+ * +for(method, config, ...args)
137
+ * +silly(msg, verbosity)
138
+ * +verbose(msg, verbosity)
139
+ * +info(msg)
140
+ * +debug(msg)
141
+ * +error(msg)
142
+ * +setConfig(config)
143
+ * }
144
+ *
145
+ * class Logging {
146
+ * -global: Logger
147
+ * -_factory: LoggerFactory
148
+ * -_config: LoggingConfig
149
+ * +setFactory(factory)
150
+ * +setConfig(config)
151
+ * +getConfig()
152
+ * +get()
153
+ * +verbose(msg, verbosity)
154
+ * +info(msg)
155
+ * +debug(msg)
156
+ * +silly(msg)
157
+ * +error(msg)
158
+ * +for(object, config, ...args)
159
+ * +because(reason, id)
160
+ * +theme(text, type, loggerLevel, template)
161
+ * }
88
162
  *
89
- * @class
163
+ * class MiniLogger {
164
+ * +constructor(context, conf?)
165
+ * }
166
+ *
167
+ * Logging ..> Logger : creates
168
+ * Logging ..> MiniLogger : creates by default
90
169
  */
91
170
  export declare class Logging {
92
171
  /**
93
- * @description The global logger instance.
94
- * @summary A singleton instance of VerbosityLogger used for global logging.
172
+ * @description The global logger instance
173
+ * @summary A singleton instance of Logger used for global logging
95
174
  */
96
175
  private static global?;
97
176
  /**
98
- * @description Factory function for creating logger instances.
99
- * @summary A function that creates new VerbosityLogger instances. By default, it creates a MiniLogger.
177
+ * @description Factory function for creating logger instances
178
+ * @summary A function that creates new Logger instances. By default, it creates a MiniLogger.
100
179
  */
101
180
  private static _factory;
102
181
  /**
103
- * @description Configuration for the logging system.
104
- * @summary Stores the global verbosity level and log level settings.
182
+ * @description Configuration for the logging system
183
+ * @summary Stores the global logging configuration including verbosity, log level, styling, and formatting settings
105
184
  */
106
185
  private static _config;
186
+ private constructor();
107
187
  /**
108
- * @description Private constructor to prevent instantiation.
109
- * @summary Ensures that the Logging class cannot be instantiated as it's designed to be used statically.
188
+ * @description Sets the factory function for creating logger instances
189
+ * @summary Allows customizing how logger instances are created
190
+ * @param {LoggerFactory} factory - The factory function to use for creating loggers
191
+ * @return {void}
110
192
  */
111
- private constructor();
112
193
  static setFactory(factory: LoggerFactory): void;
113
194
  /**
114
- * @description Setter for the logging configuration.
115
- * @summary Allows updating the global logging configuration.
116
- *
117
- * @param config - An object containing verbosity and log level settings.
195
+ * @description Updates the global logging configuration
196
+ * @summary Allows updating the global logging configuration with new settings
197
+ * @param {Partial<LoggingConfig>} config - The configuration options to apply
198
+ * @return {void}
118
199
  */
119
200
  static setConfig(config: Partial<LoggingConfig>): void;
201
+ /**
202
+ * @description Gets a copy of the current global logging configuration
203
+ * @summary Returns a copy of the current global logging configuration
204
+ * @return {LoggingConfig} A copy of the current configuration
205
+ */
120
206
  static getConfig(): LoggingConfig;
121
207
  /**
122
208
  * @description Retrieves or creates the global logger instance.
@@ -161,6 +247,14 @@ export declare class Logging {
161
247
  * @param msg - The message to be logged.
162
248
  */
163
249
  static error(msg: StringLike): void;
250
+ /**
251
+ * @description Creates a logger for a specific object or context
252
+ * @summary Creates a new logger instance for the given object or context using the factory function
253
+ * @param {LoggingContext} object - The object, class, or context to create a logger for
254
+ * @param {Partial<LoggingConfig>} [config] - Optional configuration to override global settings
255
+ * @param {...any} args - Additional arguments to pass to the logger factory
256
+ * @return {Logger} A new logger instance for the specified object or context
257
+ */
164
258
  static for(object: LoggingContext, config?: Partial<LoggingConfig>, ...args: any[]): Logger;
165
259
  /**
166
260
  * @description Creates a logger for a specific reason or context.
@@ -173,5 +267,38 @@ export declare class Logging {
173
267
  * @returns A new VerbosityLogger or ClassLogger instance.
174
268
  */
175
269
  static because(reason: string, id?: string): Logger;
270
+ /**
271
+ * @description Applies theme styling to text
272
+ * @summary Applies styling (colors, formatting) to text based on the theme configuration
273
+ * @param {string} text - The text to style
274
+ * @param {string} type - The type of element to style (e.g., "class", "message", "logLevel")
275
+ * @param {LogLevel} loggerLevel - The log level to use for styling
276
+ * @param {Theme} [template=DefaultTheme] - The theme to use for styling
277
+ * @return {string} The styled text
278
+ * @mermaid
279
+ * sequenceDiagram
280
+ * participant Caller
281
+ * participant Theme as Logging.theme
282
+ * participant Apply as apply function
283
+ * participant Style as styled-string-builder
284
+ *
285
+ * Caller->>Theme: theme(text, type, loggerLevel)
286
+ * Theme->>Theme: Check if styling is enabled
287
+ * alt styling disabled
288
+ * Theme-->>Caller: return original text
289
+ * else styling enabled
290
+ * Theme->>Theme: Get theme for type
291
+ * alt theme not found
292
+ * Theme-->>Caller: return original text
293
+ * else theme found
294
+ * Theme->>Theme: Determine actual theme based on log level
295
+ * Theme->>Apply: Apply each style property
296
+ * Apply->>Style: Apply colors and formatting
297
+ * Style-->>Apply: Return styled text
298
+ * Apply-->>Theme: Return styled text
299
+ * Theme-->>Caller: Return final styled text
300
+ * end
301
+ * end
302
+ */
176
303
  static theme(text: string, type: keyof Theme | keyof LogLevel, loggerLevel: LogLevel, template?: Theme): string;
177
304
  }