Getting started
Install comon_logger, attach a console handler, and emit the first structured records.
Getting started
The smallest useful setup is:
- add
comon_logger - attach a handler to
Logger.root - create a named logger
- emit records with typed tags
Install the core package
dart pub add comon_loggerFor Flutter projects the command is the same:
flutter pub add comon_loggerMinimal console setup
import 'package:comon_logger/comon_logger.dart';
void main() {
Logger.root.addHandler(
ConsoleLogHandler(
filter: const LevelLogFilter(LogLevel.INFO),
formatter: PrettyLogFormatter(),
),
);
final log = Logger('my_app.catalog');
log.info(
'Products loaded',
layer: LogLayer.data,
type: LogType.network,
feature: 'catalog',
);
}What is happening here
Logger.rootis the top-level logger that can receive records from all children.ConsoleLogHandlerprints accepted records.LevelLogFilter(LogLevel.INFO)blocks records belowINFOfor this handler.Logger('my_app.catalog')creates or reuses a named child logger.layer,type, andfeaturemake the record filterable later.
Add an error record
log.severe(
'Failed to load products',
error: Exception('Network timeout'),
stackTrace: StackTrace.current,
layer: LogLayer.data,
type: LogType.network,
feature: 'catalog',
);Flutter session history
When you need in-app diagnostics, add comon_logger_flutter and store records in memory:
flutter pub add comon_logger_flutterimport 'package:comon_logger_flutter/comon_logger_flutter.dart';
final historyHandler = HistoryLogHandler(maxHistory: 2000);
void initLogging() {
Logger.root.addHandler(ConsoleLogHandler());
Logger.root.addHandler(historyHandler);
}