oklog 1.0.0
oklog: ^1.0.0 copied to clipboard
A simple yet capable logging utility for Dart and Flutter. Just log. ok.
oklog #
A simple yet capable logging utility for Dart and Flutter. Just log. ok.
Features #
- Five log levels:
trace,debug,info,warn,error - Colored, emoji-decorated console output via
ConsoleLogger - Filter logs by class name using
allowListanddenyList - Silent no-op logging via
DummyLogger - Global
loginstance ready to use out of the box
Getting started #
Add the dependency to your pubspec.yaml:
dependencies:
oklog: ^1.0.0
Then import the library:
import 'package:oklog/oklog.dart';
Usage #
Basic logging #
import 'package:oklog/oklog.dart';
void main() {
log.level = LogLevel.trace; // show all levels
log.trace('main', 'Trace message');
log.debug('main', 'Debug message');
log.info('main', 'Info message');
log.warn('main', 'Warning message');
try {
throw Exception('Something went wrong!');
} catch (e, st) {
log.error('main', 'An error occurred.', e, st);
}
}
Using with a class instance #
Pass this as the first argument and the class name is resolved automatically:
class MyClass {
void myMethod() {
log.info(this, 'Hello from MyClass');
}
}
Filtering with allowList and denyList #
// Only log messages from classes whose name contains 'main'
log.allowList = ['main'];
// Suppress log messages from classes whose name contains 'MyClass'
log.denyList = ['MyClass'];
// Clear filters
log.allowList.clear();
log.denyList.clear();
Silencing output with DummyLogger #
log = DummyLogger(); // all log calls become no-ops
Changing the log level #
log.level = LogLevel.warn; // only warn and error are printed
Log levels #
| Level | Description |
|---|---|
trace |
Fine-grained diagnostic messages |
debug |
General debugging information |
info |
Informational messages |
warn |
Warnings with optional error/stack |
error |
Errors with error object and stack |