Skip to content

Commit bb5a24b

Browse files
committed
Update version to 2.0.2, enhance documentation, and improve logging functionality
1 parent 57139e8 commit bb5a24b

5 files changed

Lines changed: 184 additions & 8 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Files and directories created by pub.
22
.dart_tool/
33
.packages
4-
4+
doc/
55
# Conventional directory for build outputs.
66
build/
77

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.2
2+
- Fix: The `Console` class was not properly exporting the `enableColors` and `showTimestamp` properties. Now, these properties are correctly defined as static variables, allowing users to control the output formatting as intended.
3+
4+
15
## 2.0.1
26
- Fix: Global toggles were not working as expected. Now, `Console.enableColors` and `Console.showTimestamp` properly control the output formatting.
37

lib/console_tools.dart

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
1-
/// Support for doing something awesome.
1+
/// A lightweight, colorized console logger for Dart and Flutter applications.
22
///
3-
/// More dartdocs go here.
3+
/// This library provides the [Console] class for structured, leveled logging
4+
/// with ANSI color support, timestamps, optional tags, and pretty-printed JSON.
5+
///
6+
/// ## Quick start
7+
///
8+
/// ```dart
9+
/// import 'package:console_tools/console_tools.dart';
10+
///
11+
/// void main() {
12+
/// Console.d('Debug message'); // grey
13+
/// Console.i('Server started', tag: 'HTTP'); // cyan
14+
/// Console.s('Login successful'); // green
15+
/// Console.w('Slow API response'); // yellow
16+
/// Console.e('Unhandled exception'); // red
17+
/// Console.json({'user': 'Younes', 'role': 'dev'}); // pretty JSON
18+
/// }
19+
/// ```
20+
///
21+
/// Color output can be disabled globally:
22+
///
23+
/// ```dart
24+
/// Console.enableColors = false;
25+
/// ```
26+
///
27+
/// Timestamps can be hidden:
28+
///
29+
/// ```dart
30+
/// Console.showTimestamp = false;
31+
/// ```
432
library console_tools;
533

634
export 'src/console_tools_base.dart';
7-
8-
// TODO: Export any libraries intended for clients of this package.

lib/src/console_tools_base.dart

Lines changed: 148 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,74 @@
11
import 'dart:async';
22
import '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+
/// ```
659
class 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 = '\x1B[0m';
27102

103+
// Maps each log level to its ANSI foreground color code
28104
static const _colors = {
29105
LogLevel.debug: '\x1B[90m', // grey
30106
LogLevel.info: '\x1B[36m', // cyan
@@ -33,6 +109,7 @@ class Console {
33109
LogLevel.error: '\x1B[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')}:'

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: console_tools
22
description: A console logger class, with custom colors and text styles already defined.
3-
version: 2.0.1
3+
version: 2.0.2
44
homepage: https://github.com/ymrabti/console_tools.git
55

66
environment:

0 commit comments

Comments
 (0)