loki_logger 1.1.0
loki_logger: ^1.1.0 copied to clipboard
Dart utility library for publishing logs to a Loki Server
Loki Logger #
A powerful, flexible logging solution for Dart and Flutter applications with Grafana Loki integration. Inspired by logger for Dart.
Features #
- π Complete Logging Solution: Local logging with formatting and filtering
- π Grafana Loki Integration: Send logs directly to your Loki server
- π§© Modular Architecture: Use components together or separately
- π¨ Pretty Printing: Beautiful, formatted logs in the console
- π Customizable Filtering: Control which logs are processed
- π¦ Batched Log Sending: Efficient log transmission to Loki
Installation #
Add this to your package's pubspec.yaml
file:
dependencies:
loki_logger: ^latest_version
Then run:
$ flutter pub get
Quick Start #
// Create a simple logger
final logger = LokiLogger();
// Start logging!
logger.d("Debug message");
logger.i("Info message");
logger.w("Warning message");
logger.e("Error message", Exception("Something went wrong"));
Usage Scenarios #
Local Logging Only #
final logger = LokiLogger(
name: 'AppLogger',
printer: PrettyPrinter(
methodCount: 2,
errorMethodCount: 8,
lineLength: 120,
colors: true,
printEmojis: true,
printTime: true,
),
);
logger.i('Application started');
Loki Integration #
final logger = LokiLogger(
name: 'AppLogger',
config: LokiConfig(
host: 'https://loki.example.com',
labels: {'app': 'my_app', 'environment': 'production'},
batching: true,
interval: 10,
),
);
logger.i('User logged in');
Using LokiClient Directly #
final lokiClient = LokiClient(
config: LokiConfig(
host: 'https://loki.example.com',
labels: {'app': 'my_app'},
),
);
lokiClient.log(
level: 'info',
message: 'User action',
customLabels: {'user_id': '12345'},
);
// Don't forget to dispose when done
lokiClient.dispose();
Configuration #
Log Levels #
Loki Logger supports the following log levels (in order of verbosity):
Level.trace
: Detailed tracing informationLevel.debug
: Debug informationLevel.info
: General informationLevel.warning
: WarningsLevel.error
: ErrorsLevel.fatal
: Critical errors
Set the global log level:
// In development
LokiLogger.level = Level.debug;
// In production
LokiLogger.level = Level.info;
LokiConfig Options #
LokiConfig(
host: 'https://loki.example.com', // Required
interval: 10, // Seconds between batch sends
batching: true, // Enable/disable batching
clearOnError: false, // Whether to discard logs on error
replaceTimestamp: true, // Replace log timestamps with current time
labels: {'app': 'my_app'}, // Global labels
timeout: 5000, // Request timeout in milliseconds
basicAuth: 'username:password', // Basic auth credentials
);
Best Practices #
- Configure Appropriate Log Levels: Use different levels for development and production
- Use Batching in Production: More efficient for high-volume logging
- Add Contextual Information with Labels: Enhance logs with metadata
- Include Error Objects and Stack Traces: For better debugging
- Dispose Resources: Clean up when using LokiClient directly
Credits #
This package was inspired by the logger package for Dart, originally created by Simon Choi and further developed by Harm Aarts. Loki Logger extends the functionality with Grafana Loki integration while maintaining a similar API.
License #
This project is licensed under the MIT License - see the LICENSE file for details.