11import 'dart:async' ;
22import 'dart:convert' ;
33
4- enum LogLevel { debug, info, success, warning, error }
4+ /// Severity levels used by [Console] to categorize log messages.
5+ ///
6+ /// Each level maps to a distinct ANSI color and icon in the console output:
7+ ///
8+ /// | Level | Color | Icon |
9+ /// |-----------|--------|------|
10+ /// | [debug] | grey | 🐛 |
11+ /// | [info] | cyan | ℹ️ |
12+ /// | [success] | green | ✅ |
13+ /// | [warning] | yellow | ⚠️ |
14+ /// | [error] | red | ❌ |
15+ enum LogLevel {
16+ /// Verbose / debug-only information. Useful during development.
17+ debug,
18+
19+ /// General informational messages about normal program flow.
20+ info,
21+
22+ /// Confirmation that an operation completed successfully.
23+ success,
24+
25+ /// Something unexpected happened but execution can continue.
26+ warning,
27+
28+ /// A serious problem that requires immediate attention.
29+ error,
30+ }
531
32+ /// A static utility class for structured, colorized console logging.
33+ ///
34+ /// [Console] writes formatted log lines to standard output using ANSI escape
35+ /// codes. Each message is prefixed with an optional timestamp, a level icon,
36+ /// and an optional tag, making log output easy to scan at a glance.
37+ ///
38+ /// All methods are static — the class cannot be instantiated.
39+ ///
40+ /// ## Output format
41+ ///
42+ /// ```
43+ /// [HH:mm:ss] <icon> [tag] <message>
44+ /// ```
45+ ///
46+ /// ## Example
47+ ///
48+ /// ```dart
49+ /// Console.enableColors = true; // default
50+ /// Console.showTimestamp = true; // default
51+ ///
52+ /// Console.d('Connecting…', tag: 'DB');
53+ /// Console.i('Server ready', tag: 'HTTP');
54+ /// Console.s('User created');
55+ /// Console.w('Rate limit close', tag: 'API');
56+ /// Console.e('Null pointer', tag: 'CRASH');
57+ /// Console.json({'status': 'ok'});
58+ /// ```
659class Console {
60+ /// Forwards a message to [i] using [dart:developer] -compatible parameters.
61+ ///
62+ /// This method mirrors the signature of `dart:developer` 's `log` function so
63+ /// that [Console] can be used as a drop-in replacement. Only [text] and
64+ /// [name] (mapped to *tag*) are used; the remaining parameters are accepted
65+ /// for API compatibility but are currently ignored.
66+ ///
67+ /// Parameters:
68+ /// - [text] — the message to log.
69+ /// - [name] — tag label shown in brackets (defaults to `'logger'` ).
70+ /// - [time] , [sequenceNumber] , [level] , [zone] , [error] , [stackTrace] —
71+ /// unused; kept for `dart:developer` compatibility.
772 static log (
873 Object ? text, {
974 String name = 'logger' ,
@@ -19,12 +84,23 @@ class Console {
1984
2085 Console ._();
2186
87+ /// Whether ANSI color codes are applied to log output.
88+ ///
89+ /// Set to `false` when logging to a file or a terminal that does not support
90+ /// ANSI escape sequences.
91+ ///
92+ /// Defaults to `true` .
2293 static bool enableColors = true ;
94+
95+ /// Whether a `[HH:mm:ss]` timestamp prefix is prepended to every message.
96+ ///
97+ /// Defaults to `true` .
2398 static bool showTimestamp = true ;
2499
25- // ANSI colors
100+ // ANSI reset sequence
26101 static const _reset = '\x 1B[0m' ;
27102
103+ // Maps each log level to its ANSI foreground color code
28104 static const _colors = {
29105 LogLevel .debug: '\x 1B[90m' , // grey
30106 LogLevel .info: '\x 1B[36m' , // cyan
@@ -33,6 +109,7 @@ class Console {
33109 LogLevel .error: '\x 1B[31m' , // red
34110 };
35111
112+ // Maps each log level to its display icon
36113 static const _icons = {
37114 LogLevel .debug: '🐛' ,
38115 LogLevel .info: 'ℹ️' ,
@@ -41,27 +118,95 @@ class Console {
41118 LogLevel .error: '❌' ,
42119 };
43120
121+ /// Logs a **debug** message (grey 🐛).
122+ ///
123+ /// Use for verbose, developer-only information that is not relevant in
124+ /// production.
125+ ///
126+ /// ```dart
127+ /// Console.d('Socket opened', tag: 'WS');
128+ /// ```
129+ ///
130+ /// - [message] — the value to print; `toString()` is called automatically.
131+ /// - [tag] — optional label shown in brackets, e.g. `[WS]` .
44132 static void d (Object ? message, {String ? tag}) =>
45133 _log (LogLevel .debug, message, tag);
46134
135+ /// Logs an **info** message (cyan ℹ️).
136+ ///
137+ /// Use for general informational messages about normal application flow.
138+ ///
139+ /// ```dart
140+ /// Console.i('Server started on port 8080', tag: 'HTTP');
141+ /// ```
142+ ///
143+ /// - [message] — the value to print.
144+ /// - [tag] — optional label shown in brackets.
47145 static void i (Object ? message, {String ? tag}) =>
48146 _log (LogLevel .info, message, tag);
49147
148+ /// Logs a **success** message (green ✅).
149+ ///
150+ /// Use to confirm that an operation completed without errors.
151+ ///
152+ /// ```dart
153+ /// Console.s('Payment processed', tag: 'BILLING');
154+ /// ```
155+ ///
156+ /// - [message] — the value to print.
157+ /// - [tag] — optional label shown in brackets.
50158 static void s (Object ? message, {String ? tag}) =>
51159 _log (LogLevel .success, message, tag);
52160
161+ /// Logs a **warning** message (yellow ⚠️).
162+ ///
163+ /// Use when something unexpected occurred but execution can safely continue.
164+ ///
165+ /// ```dart
166+ /// Console.w('Cache miss — falling back to network', tag: 'CACHE');
167+ /// ```
168+ ///
169+ /// - [message] — the value to print.
170+ /// - [tag] — optional label shown in brackets.
53171 static void w (Object ? message, {String ? tag}) =>
54172 _log (LogLevel .warning, message, tag);
55173
174+ /// Logs an **error** message (red ❌).
175+ ///
176+ /// Use for serious problems that require immediate attention or indicate a
177+ /// failure in program logic.
178+ ///
179+ /// ```dart
180+ /// Console.e('Unhandled exception: $e', tag: 'CRASH');
181+ /// ```
182+ ///
183+ /// - [message] — the value to print.
184+ /// - [tag] — optional label shown in brackets.
56185 static void e (Object ? message, {String ? tag}) =>
57186 _log (LogLevel .error, message, tag);
58187
188+ /// Pretty-prints a JSON-encodable [jsonObj] at the [LogLevel.debug] level.
189+ ///
190+ /// The object is serialized with a two-space indent so nested structures are
191+ /// easy to read in the console.
192+ ///
193+ /// ```dart
194+ /// Console.json({'user': 'Younes', 'role': 'developer'}, tag: 'AUTH');
195+ /// ```
196+ ///
197+ /// - [jsonObj] — any value accepted by [JsonEncoder] (Map, List, num, etc.).
198+ /// - [tag] — optional label shown in brackets.
59199 static void json (Object jsonObj, {String ? tag}) {
60200 final encoder = const JsonEncoder .withIndent (' ' );
61201 final pretty = encoder.convert (jsonObj);
62202 _log (LogLevel .debug, pretty, tag);
63203 }
64204
205+ /// Internal method that formats and prints a single log line.
206+ ///
207+ /// Builds the output string from the optional timestamp, the level icon,
208+ /// an optional tag, and the message, then wraps it with ANSI color codes
209+ /// if [enableColors] is `true` .
65210 static void _log (LogLevel level, Object ? message, String ? tag) {
66211 final time = showTimestamp ? _timeNow () : '' ;
67212 final icon = _icons[level];
@@ -78,6 +223,7 @@ class Console {
78223 print ('$color $base $_reset ' );
79224 }
80225
226+ /// Returns the current wall-clock time formatted as `[HH:mm:ss]` .
81227 static String _timeNow () {
82228 final now = DateTime .now ();
83229 return '[${now .hour .toString ().padLeft (2 , '0' )}:'
0 commit comments