1
1
import 'dart:developer' as developer;
2
2
import 'package:flutter/foundation.dart' ;
3
3
4
- /// Configurable log levels
4
+ /// Configurable log levels for the policy engine logging system.
5
+ ///
6
+ /// Log levels are ordered from least to most severe:
7
+ /// - [debug] : Detailed information for debugging purposes
8
+ /// - [info] : General information about application flow
9
+ /// - [warning] : Indicates a potential issue that doesn't stop execution
10
+ /// - [error] : Indicates an error that may affect functionality
5
11
enum LogLevel {
12
+ /// Detailed information for debugging purposes
6
13
debug (0 ),
14
+
15
+ /// General information about application flow
7
16
info (1 ),
17
+
18
+ /// Indicates a potential issue that doesn't stop execution
8
19
warning (2 ),
20
+
21
+ /// Indicates an error that may affect functionality
9
22
error (3 );
10
23
11
24
const LogLevel (this .value);
25
+
26
+ /// The numeric value representing the log level severity
12
27
final int value;
13
28
}
14
29
15
- /// Structured log data for better debugging
30
+ /// Structured log data container for enhanced debugging and monitoring.
31
+ ///
32
+ /// This class encapsulates all relevant information for a log entry,
33
+ /// including contextual data, timing information, and error details.
34
+ /// It provides a consistent structure for logging across the policy engine.
16
35
class LogData {
17
- final String message;
18
- final LogLevel level;
19
- final String ? tag;
20
- final String ? screen;
21
- final String ? operation;
22
- final Duration ? duration;
23
- final Object ? error;
24
- final StackTrace ? stackTrace;
25
- final Map <String , dynamic >? context;
26
- final DateTime timestamp;
27
-
36
+ /// Creates a new LogData instance with the specified parameters.
37
+ ///
38
+ /// [message] The main log message
39
+ /// [level] The severity level of the log entry
40
+ /// [tag] Optional tag for categorizing logs
41
+ /// [screen] Current screen or context where the log was generated
42
+ /// [operation] Specific operation being performed
43
+ /// [duration] Duration of the operation (for performance logging)
44
+ /// [error] Associated error object, if any
45
+ /// [stackTrace] Stack trace for error logging
46
+ /// [context] Additional contextual data as key-value pairs
47
+ /// [timestamp] Timestamp when the log entry was created (defaults to current time)
28
48
LogData ({
29
49
required this .message,
30
50
required this .level,
@@ -38,7 +58,40 @@ class LogData {
38
58
DateTime ? timestamp,
39
59
}) : timestamp = timestamp ?? DateTime .now ();
40
60
41
- /// Convert to structured log format
61
+ /// The main log message
62
+ final String message;
63
+
64
+ /// The severity level of the log entry
65
+ final LogLevel level;
66
+
67
+ /// Optional tag for categorizing logs
68
+ final String ? tag;
69
+
70
+ /// Current screen or context where the log was generated
71
+ final String ? screen;
72
+
73
+ /// Specific operation being performed
74
+ final String ? operation;
75
+
76
+ /// Duration of the operation (for performance logging)
77
+ final Duration ? duration;
78
+
79
+ /// Associated error object, if any
80
+ final Object ? error;
81
+
82
+ /// Stack trace for error logging
83
+ final StackTrace ? stackTrace;
84
+
85
+ /// Additional contextual data as key-value pairs
86
+ final Map <String , dynamic >? context;
87
+
88
+ /// Timestamp when the log entry was created
89
+ final DateTime timestamp;
90
+
91
+ /// Converts the log data to a structured format suitable for external logging systems.
92
+ ///
93
+ /// Returns a [Map] containing all relevant log information in a standardized format.
94
+ /// This is particularly useful for integration with external monitoring tools.
42
95
Map <String , dynamic > toStructuredLog () {
43
96
return {
44
97
'message' : message,
@@ -54,6 +107,17 @@ class LogData {
54
107
}
55
108
}
56
109
110
+ /// Centralized logging handler for the Flutter Policy Engine.
111
+ ///
112
+ /// This class provides a comprehensive logging solution with the following features:
113
+ /// - Configurable log levels and filtering
114
+ /// - Structured logging for better debugging
115
+ /// - Performance timing capabilities
116
+ /// - Screen/context-aware logging
117
+ /// - Integration with Flutter's developer tools
118
+ ///
119
+ /// The LogHandler is designed to be used statically throughout the application
120
+ /// and can be configured once at startup for consistent logging behavior.
57
121
class LogHandler {
58
122
static const String _defaultTag = '[PolicyEngine]' ;
59
123
static bool _isDebugMode = kDebugMode;
@@ -66,25 +130,39 @@ class LogHandler {
66
130
static bool _useStructuredLogging = true ;
67
131
static bool _useDeveloperLog = true ;
68
132
69
- /// Set the current screen for logging context
133
+ /// Sets the current screen context for all subsequent log entries.
134
+ ///
135
+ /// This method updates the logging context to include the current screen name,
136
+ /// which helps in debugging by providing better context for log entries.
137
+ ///
138
+ /// [screenName] The name of the current screen or context
70
139
static void setScreen (String screenName) {
71
140
_currentScreen = screenName;
72
141
_updateTag ();
73
142
}
74
143
75
- /// Set a custom tag for the current screen
144
+ /// Sets a custom tag for a specific screen while maintaining screen context.
145
+ ///
146
+ /// This method allows for more granular control over log categorization
147
+ /// by providing a custom tag for specific screens or contexts.
148
+ ///
149
+ /// [screenName] The name of the current screen or context
150
+ /// [customTag] The custom tag to use for this screen
76
151
static void setScreenTag (String screenName, String customTag) {
77
152
_currentScreen = screenName;
78
153
_tag = customTag;
79
154
}
80
155
81
- /// Get the current screen name
156
+ /// Gets the current screen name being used for logging context.
82
157
static String get currentScreen => _currentScreen;
83
158
84
- /// Get the current tag
159
+ /// Gets the current tag being used for log categorization.
85
160
static String get currentTag => _tag;
86
161
87
- /// Update tag based on current screen
162
+ /// Updates the tag based on the current screen context.
163
+ ///
164
+ /// This internal method ensures that the tag reflects the current screen
165
+ /// when no custom tag has been explicitly set.
88
166
static void _updateTag () {
89
167
if (_currentScreen.isNotEmpty) {
90
168
_tag = '[$_currentScreen ]' ;
@@ -93,7 +171,20 @@ class LogHandler {
93
171
}
94
172
}
95
173
96
- /// Configure the LogHandler settings
174
+ /// Configures the LogHandler with custom settings.
175
+ ///
176
+ /// This method allows for comprehensive configuration of the logging system.
177
+ /// All parameters are optional and will use sensible defaults if not provided.
178
+ ///
179
+ /// [tag] Custom tag for log categorization
180
+ /// [screen] Initial screen context
181
+ /// [isDebugMode] Whether to enable debug mode logging
182
+ /// [includeTimestamp] Whether to include timestamps in log entries
183
+ /// [includeStackTrace] Whether to include stack traces for errors
184
+ /// [includeSystemInfo] Whether to include system information in logs
185
+ /// [minLogLevel] Minimum log level to output (filters out lower levels)
186
+ /// [useStructuredLogging] Whether to use structured logging format
187
+ /// [useDeveloperLog] Whether to use Flutter's developer.log
97
188
static void configure ({
98
189
String ? tag,
99
190
String ? screen,
@@ -121,7 +212,12 @@ class LogHandler {
121
212
}
122
213
}
123
214
124
- /// Internal method to output logs using dart:developer
215
+ /// Internal method to output logs using Flutter's developer.log.
216
+ ///
217
+ /// This method handles the actual log output using Flutter's built-in
218
+ /// developer tools for better debugging experience.
219
+ ///
220
+ /// [logData] The structured log data to output
125
221
static void _logWithDeveloper (LogData logData) {
126
222
if (! _useDeveloperLog) return ;
127
223
@@ -146,7 +242,20 @@ class LogHandler {
146
242
}
147
243
}
148
244
149
- /// Generic log method with configurable parameters
245
+ /// Generic log method with configurable parameters.
246
+ ///
247
+ /// This is the core logging method that handles all log entry creation
248
+ /// and output. It respects the current configuration settings and
249
+ /// only outputs logs that meet the minimum level requirements.
250
+ ///
251
+ /// [message] The main log message
252
+ /// [level] The severity level of the log entry
253
+ /// [error] Associated error object, if any
254
+ /// [stackTrace] Stack trace for error logging
255
+ /// [context] Additional contextual data
256
+ /// [operation] Specific operation being performed
257
+ /// [duration] Duration of the operation (for performance logging)
258
+ /// [screenOverride] Override the current screen context
150
259
static void output (
151
260
String message, {
152
261
LogLevel level = LogLevel .debug,
@@ -175,7 +284,18 @@ class LogHandler {
175
284
_logWithDeveloper (logData);
176
285
}
177
286
178
- /// Convenience methods for different log levels
287
+ /// Logs a debug-level message.
288
+ ///
289
+ /// Debug logs provide detailed information useful for debugging
290
+ /// and development purposes. These are typically filtered out in production.
291
+ ///
292
+ /// [message] The debug message
293
+ /// [error] Associated error object, if any
294
+ /// [stackTrace] Stack trace for error logging
295
+ /// [context] Additional contextual data
296
+ /// [operation] Specific operation being performed
297
+ /// [duration] Duration of the operation
298
+ /// [screenOverride] Override the current screen context
179
299
static void debug (
180
300
String message, {
181
301
Object ? error,
@@ -197,6 +317,18 @@ class LogHandler {
197
317
);
198
318
}
199
319
320
+ /// Logs an info-level message.
321
+ ///
322
+ /// Info logs provide general information about application flow
323
+ /// and are useful for understanding normal operation.
324
+ ///
325
+ /// [message] The info message
326
+ /// [error] Associated error object, if any
327
+ /// [stackTrace] Stack trace for error logging
328
+ /// [context] Additional contextual data
329
+ /// [operation] Specific operation being performed
330
+ /// [duration] Duration of the operation
331
+ /// [screenOverride] Override the current screen context
200
332
static void info (
201
333
String message, {
202
334
Object ? error,
@@ -218,6 +350,18 @@ class LogHandler {
218
350
);
219
351
}
220
352
353
+ /// Logs a warning-level message.
354
+ ///
355
+ /// Warning logs indicate potential issues that don't stop execution
356
+ /// but should be investigated.
357
+ ///
358
+ /// [message] The warning message
359
+ /// [error] Associated error object, if any
360
+ /// [stackTrace] Stack trace for error logging
361
+ /// [context] Additional contextual data
362
+ /// [operation] Specific operation being performed
363
+ /// [duration] Duration of the operation
364
+ /// [screenOverride] Override the current screen context
221
365
static void warning (
222
366
String message, {
223
367
Object ? error,
@@ -239,6 +383,18 @@ class LogHandler {
239
383
);
240
384
}
241
385
386
+ /// Logs an error-level message.
387
+ ///
388
+ /// Error logs indicate actual errors that may affect functionality
389
+ /// and should be addressed immediately.
390
+ ///
391
+ /// [message] The error message
392
+ /// [error] Associated error object, if any
393
+ /// [stackTrace] Stack trace for error logging
394
+ /// [context] Additional contextual data
395
+ /// [operation] Specific operation being performed
396
+ /// [duration] Duration of the operation
397
+ /// [screenOverride] Override the current screen context
242
398
static void error (
243
399
String message, {
244
400
Object ? error,
@@ -260,7 +416,13 @@ class LogHandler {
260
416
);
261
417
}
262
418
263
- /// Performance logging with timing
419
+ /// Executes a callback function and logs its execution time.
420
+ ///
421
+ /// This method is useful for performance monitoring and debugging.
422
+ /// It automatically logs the duration of the operation upon completion.
423
+ ///
424
+ /// [operation] Name of the operation being timed
425
+ /// [callback] The function to execute and time
264
426
static void time (String operation, void Function () callback) {
265
427
final stopwatch = Stopwatch ()..start ();
266
428
try {
@@ -275,7 +437,14 @@ class LogHandler {
275
437
}
276
438
}
277
439
278
- /// Async performance logging
440
+ /// Executes an async callback function and logs its execution time.
441
+ ///
442
+ /// This method is useful for performance monitoring of asynchronous operations.
443
+ /// It automatically logs the duration of the operation upon completion.
444
+ ///
445
+ /// [operation] Name of the operation being timed
446
+ /// [callback] The async function to execute and time
447
+ /// Returns the result of the async operation
279
448
static Future <T > timeAsync <T >(
280
449
String operation,
281
450
Future <T > Function () callback,
@@ -294,12 +463,23 @@ class LogHandler {
294
463
}
295
464
}
296
465
297
- /// Legacy method for backward compatibility
466
+ /// Legacy method for backward compatibility.
467
+ ///
468
+ /// This method maintains compatibility with existing code that uses
469
+ /// the old logging interface. It delegates to the debug method.
470
+ ///
471
+ /// [message] The log message
472
+ /// [error] Associated error object, if any
473
+ /// [stackTrace] Stack trace for error logging
298
474
static void show (String message, {Object ? error, StackTrace ? stackTrace}) {
299
475
debug (message, error: error, stackTrace: stackTrace);
300
476
}
301
477
302
- /// Reset configuration to defaults
478
+ /// Resets the LogHandler configuration to default values.
479
+ ///
480
+ /// This method restores all configuration settings to their initial
481
+ /// default values, which is useful for testing or resetting the
482
+ /// logging system to a known state.
303
483
static void reset () {
304
484
_tag = _defaultTag;
305
485
_currentScreen = '' ;
0 commit comments