log_custom_printer 3.2.0
log_custom_printer: ^3.2.0 copied to clipboard
Custom logging for Dart with ANSI colors, JSON serialization, and get_it DI.
log_custom_printer #
A Dart logging library with typed log objects, ANSI color formatting, JSON serialization, and dependency injection (get_it).
It is suitable for CLI apps, servers, and scripts, and can also be used in Flutter apps after registering the printer at startup.
v3.0.0 — The package is now pure Dart (no Flutter dependency). Registration APIs (
registerLogPrinter,registerLogPrinterColor,registerLogPrinterSimple) still returnLoggerPersistenceServicewith integrated cache support. The Flutter visual console was moved to a separate package. See doc/ConsoleView.md.v3.2.0 — File persistence no longer uses a
FileTyperegistration parameter; cache files are JSON.
Features #
- Typed log hierarchy:
DebugLog,InfoLog,WarningLog,ErrorLog - ANSI color formatting for terminal output
- JSON serialization with
json_serializable - Flexible configuration via type filtering and enable/disable flags
- Cache support with in-memory and optional JSON file persistence
- Concurrency-safe I/O using path-based serialization in
FileManager - Dependency injection with
registerLogPrinter* - Mixin support via
LoggerClassMixin - Traceability with origin metadata (
className/runtimeType)
Installation #
Add to pubspec.yaml:
dependencies:
log_custom_printer:
git:
url: https://github.com/saulogatti/log_custom_printer.git
# or local path:
# path: ../log_custom_printer
dart pub get
Requirement: Dart SDK >=3.13.0 <4.0.0
Basic usage #
Startup configuration (required before emitting logs) #
import 'package:log_custom_printer/log_custom_printer.dart';
void main() {
final persistenceService = registerLogPrinterColor(
config: ConfigLog(enableLog: true),
maxLogsInCache: 100,
cacheFilePath: '/path/to/save/logs', // optional
);
// Flutter apps: runApp(const MyApp());
}
Cache system #
registerLogPrinterColor and registerLogPrinterSimple return LoggerPersistenceService for querying and cleanup. With cacheFilePath, FileManager serializes writes per path to avoid race conditions.
final allLogs = await persistenceService.getAllLogs();
final errorLogs = await persistenceService.getLogsByType(EnumLoggerType.error);
await persistenceService.clearLogs();
await persistenceService.clearLogsByType(EnumLoggerType.debug);
For custom storage, implement ILoggerCacheRepository and pass it to registerLogPrinter(printer, cacheRepository: ..., config: ...).
Mixin (recommended) #
class MyClass with LoggerClassMixin {
void myFunction() {
logDebug('Starting function');
logInfo('Processing data...');
try {
logInfo('Success');
} catch (error, stackTrace) {
logError('Error: $error', stackTrace);
}
}
}
Log objects and JSON #
final debugLog = DebugLog('Debug message', typeClass: runtimeType);
debugLog.sendLog();
final errorLog = ErrorLog('Failure', StackTrace.current, typeClass: runtimeType);
errorLog.sendLog();
final json = debugLog.toJson();
final restored = DebugLog.fromJson(json);
Advanced configuration #
void main() {
registerLogPrinterColor(
config: ConfigLog(
enableLog: true,
onlyClasses: {DebugLog, ErrorLog},
),
);
// Flutter apps: runApp(const MyApp());
}
Delivery rules #
ConfigLog.enableLog == false: most logs are dropped;ErrorLogstill runs viaalwaysPrint.ConfigLog.onlyClasses: when not empty, only listed types are accepted.LoggerClassMixin: setsclassNameusing the emitter classruntimeType.- Cache repository: keeps up to
maxLogEntriesper log type (based on repository implementation).
Architecture (summary) #
LoggerObject/LoggerObjectBase— log hierarchy and dispatchLogPrinterService— output and cache coordination (viaget_it)registerLogPrinter/registerLogPrinterColor/registerLogPrinterSimpleConfigLog— filters andenableLogLoggerPersistenceService— cache access after registrationILoggerCacheRepository— customizable persistence contractLogQuery/LogFilterEngine/LogSortEngine— composable filtering and sorting
Log types #
| Type | ANSI color | Usage |
|---|---|---|
DebugLog |
Yellow | Debugging |
InfoLog |
White | General information |
WarningLog |
Green | Warnings |
ErrorLog |
Red | Errors (alwaysPrint) |
Printers #
LogSimplePrint— simple output (no ANSI color formatting in default strategy)LogWithColorPrint— ANSI color output
Shortcuts: registerLogPrinterColor / registerLogPrinterSimple; custom printers can be registered with registerLogPrinter.
Visual console (Flutter) #
The real-time Flutter console UI is not part of this repository in v3. To migrate from v2 or integrate the separate package, see doc/ConsoleView.md.
Development #
dart pub get
dart run build_runner build
# or: ./ci.sh -build
dart analyze
dart test
dart doc
Generated API docs are output to doc/api; project guides are kept alongside them under doc/.
New log types #
- Extend
LoggerObjectBase - Add
@JsonSerializable()and generatedpart - Implement
getColor(),fromJson, andtoJson - Add an
EnumLoggerTypevalue, map it inlogger_enum.dart, and register it inlogger_json_list_type.dart(_typeConstructorsandfromString) - Export the type from
lib/log_custom_printer.dart - Run
dart run build_runner build
Documentation #
| File | Content |
|---|---|
| README.md | Overview (this page) |
| CHANGELOG.md | Version history |
| doc/Core.md | Core and dependency injection |
| doc/LogTypes.md | Log types |
| doc/Printers.md | Printing strategies |
| doc/Configuration.md | Configuration and filters |
| doc/Utilities.md | Utilities and cache |
| doc/ConsoleView.md | Flutter console migration |
| doc/DOCUMENTATION.md | Extended architecture |
Contributing #
- Keep the public API stable
- Record relevant changes in
CHANGELOG.md - Run tests before opening PRs
- Follow Effective Dart
License #
See LICENSE in the repository.