simplest_logger
A lightweight, opinionated, colorful logging utility for Dart applications. Minimal configuration necessary.
Features
- Colorful output with ANSI colors (can be disabled)
- Simple API with info, warning, and error levels
- Context-based logging with automatic class name detection
- Zero configuration required - works out of the box
- No dependencies
- Convenient mixin for class-based logging
- Stack trace support for error logging
- Global configuration for consistent logging across your application
Installation
Add simplest_logger to your pubspec.yaml:
dependencies:
simplest_logger: ^2.0.0
Then run:
dart pub get
Usage
Basic Usage
import 'package:simplest_logger/simplest_logger.dart';
void main() {
// Create a logger instance with a context
final logger = SimplestLogger('MyApp');
// Log messages
logger.info('Application started');
logger.warning('Resource usage is high');
logger.error('Failed to connect to database',
Exception('Connection timeout'),
StackTrace.current
);
}
Using the Mixin
For class-based logging, use the SimplestLoggerMixin:
import 'package:simplest_logger/simplest_logger.dart';
class MyService with SimplestLoggerMixin {
void doSomething() {
logger.info('Starting operation');
// Your code here
logger.info('Operation completed');
}
}
The context comes from runtimeType, so in obfuscated or minified builds it will be a generated name. Use an explicit SimplestLogger('MyService') if that matters.
Configuration
All configuration is done through static methods on SimplestLogger, affecting all logger instances globally.
Log Levels
The level is a threshold: messages of that severity and above are logged.
// Enable all logging globally (the default)
SimplestLogger.setLevel(SimplestLoggerLevel.all);
// Only warnings and errors
SimplestLogger.setLevel(SimplestLoggerLevel.warning);
// Only errors
SimplestLogger.setLevel(SimplestLoggerLevel.error);
// Turn off all logging globally
SimplestLogger.setLevel(SimplestLoggerLevel.none);
Color Output
Enable or disable ANSI color output:
// Disable colors globally
SimplestLogger.useColors(false);
// Enable colors globally
SimplestLogger.useColors(true);
Output Format
The logger produces output in the following format:
LEVEL TIMESTAMP [CONTEXT] MESSAGE
For errors, additional information is included:
LEVEL TIMESTAMP [CONTEXT] MESSAGE
Error: ERROR_DETAILS
Stack trace:
STACK_TRACE
Colors are applied as follows:
- INFO: Green
- WARNING: Yellow
- ERROR: Red
License
MIT License - feel free to use this package in your projects.
Libraries
- simplest_logger
- A lightweight, opinionated, colorful logging utility for Dart applications.