Skip to content

Commit f97aa07

Browse files
refactor(logging): enhance LogHandler with detailed documentation and structured logging
1 parent 7acd0aa commit f97aa07

File tree

1 file changed

+207
-27
lines changed

1 file changed

+207
-27
lines changed

lib/src/utils/log_handler.dart

Lines changed: 207 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,50 @@
11
import 'dart:developer' as developer;
22
import 'package:flutter/foundation.dart';
33

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
511
enum LogLevel {
12+
/// Detailed information for debugging purposes
613
debug(0),
14+
15+
/// General information about application flow
716
info(1),
17+
18+
/// Indicates a potential issue that doesn't stop execution
819
warning(2),
20+
21+
/// Indicates an error that may affect functionality
922
error(3);
1023

1124
const LogLevel(this.value);
25+
26+
/// The numeric value representing the log level severity
1227
final int value;
1328
}
1429

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.
1635
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)
2848
LogData({
2949
required this.message,
3050
required this.level,
@@ -38,7 +58,40 @@ class LogData {
3858
DateTime? timestamp,
3959
}) : timestamp = timestamp ?? DateTime.now();
4060

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.
4295
Map<String, dynamic> toStructuredLog() {
4396
return {
4497
'message': message,
@@ -54,6 +107,17 @@ class LogData {
54107
}
55108
}
56109

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.
57121
class LogHandler {
58122
static const String _defaultTag = '[PolicyEngine]';
59123
static bool _isDebugMode = kDebugMode;
@@ -66,25 +130,39 @@ class LogHandler {
66130
static bool _useStructuredLogging = true;
67131
static bool _useDeveloperLog = true;
68132

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
70139
static void setScreen(String screenName) {
71140
_currentScreen = screenName;
72141
_updateTag();
73142
}
74143

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
76151
static void setScreenTag(String screenName, String customTag) {
77152
_currentScreen = screenName;
78153
_tag = customTag;
79154
}
80155

81-
/// Get the current screen name
156+
/// Gets the current screen name being used for logging context.
82157
static String get currentScreen => _currentScreen;
83158

84-
/// Get the current tag
159+
/// Gets the current tag being used for log categorization.
85160
static String get currentTag => _tag;
86161

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.
88166
static void _updateTag() {
89167
if (_currentScreen.isNotEmpty) {
90168
_tag = '[$_currentScreen]';
@@ -93,7 +171,20 @@ class LogHandler {
93171
}
94172
}
95173

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
97188
static void configure({
98189
String? tag,
99190
String? screen,
@@ -121,7 +212,12 @@ class LogHandler {
121212
}
122213
}
123214

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
125221
static void _logWithDeveloper(LogData logData) {
126222
if (!_useDeveloperLog) return;
127223

@@ -146,7 +242,20 @@ class LogHandler {
146242
}
147243
}
148244

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
150259
static void output(
151260
String message, {
152261
LogLevel level = LogLevel.debug,
@@ -175,7 +284,18 @@ class LogHandler {
175284
_logWithDeveloper(logData);
176285
}
177286

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
179299
static void debug(
180300
String message, {
181301
Object? error,
@@ -197,6 +317,18 @@ class LogHandler {
197317
);
198318
}
199319

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
200332
static void info(
201333
String message, {
202334
Object? error,
@@ -218,6 +350,18 @@ class LogHandler {
218350
);
219351
}
220352

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
221365
static void warning(
222366
String message, {
223367
Object? error,
@@ -239,6 +383,18 @@ class LogHandler {
239383
);
240384
}
241385

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
242398
static void error(
243399
String message, {
244400
Object? error,
@@ -260,7 +416,13 @@ class LogHandler {
260416
);
261417
}
262418

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
264426
static void time(String operation, void Function() callback) {
265427
final stopwatch = Stopwatch()..start();
266428
try {
@@ -275,7 +437,14 @@ class LogHandler {
275437
}
276438
}
277439

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
279448
static Future<T> timeAsync<T>(
280449
String operation,
281450
Future<T> Function() callback,
@@ -294,12 +463,23 @@ class LogHandler {
294463
}
295464
}
296465

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
298474
static void show(String message, {Object? error, StackTrace? stackTrace}) {
299475
debug(message, error: error, stackTrace: stackTrace);
300476
}
301477

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.
303483
static void reset() {
304484
_tag = _defaultTag;
305485
_currentScreen = '';

0 commit comments

Comments
 (0)