lite_logger 0.3.2
lite_logger: ^0.3.2 copied to clipboard
A fast, lightweight, and customizable logging utility for Dart & Flutter — with colored output, emoji icons, log filtering, and powerful formatting.
example/lite_logger_example.dart
// For debugging purposes
// ignore_for_file: avoid_print
import 'package:lite_logger/lite_logger.dart';
void main() {
// NOTE: `output` defaults to LogOutput.auto: `developer.log()` is preferred
// in debug builds (keeps ANSI colors, avoids the `flutter:` logcat escaping
// that renders `\x1B[36m` as `^[[36m`), while `print()` is used in release
// builds where `developer.log()` is not surfaced. `developer.log()` needs
// a debugger/DevTools attached to be visible, so plain `dart run` terminals
// show only the explicit `output: LogOutput.print` sections below.
print('=== Basic Usage (auto output) ===');
const LiteLogger(name: 'App', minLevel: LogLevel.debug)
..info('Application started')
..step('Loading configuration...')
..debug(() => 'Debug timestamp: ${DateTime.now().millisecondsSinceEpoch}')
..success('Configuration loaded')
..warning('Low disk space')
..error('Unable to access database');
print('\n=== Multiple Named Loggers ===');
const apiLogger = LiteLogger(name: 'API');
const dbLogger = LiteLogger(name: 'Database');
const authLogger = LiteLogger(name: 'Auth');
apiLogger.info('Fetching user data');
dbLogger.debug('Executing query...');
authLogger.warning('Token expiring soon');
print('\n=== Using Callback ===');
LiteLogger(
name: 'Service',
minLevel: LogLevel.debug,
callback: (raw, colored, level) {
print('Callback - Raw: $raw, Level: ${level.name}');
},
)
..info('Application started')
..step('Loading configuration...')
..debug(() => 'Debug timestamp: ${DateTime.now().millisecondsSinceEpoch}')
..success('Configuration loaded')
..warning('Low disk space')
..error('Unable to access database');
print('\n=== Using print() explicitly (plain terminals) ===');
const LiteLogger(
name: 'DevLogger',
minLevel: LogLevel.debug,
output: LogOutput.print,
)
..info('This uses print() explicitly')
..warning('Use this for plain `dart run` terminals without a debugger')
..error('Explicit print() is still available');
print('\n=== Using developer.log() explicitly ===');
const LiteLogger(
name: 'PrintLogger',
output: LogOutput.developerLog,
minLevel: LogLevel.debug,
)
..info('This uses developer.log()')
..warning('Cleaner output for tooling')
..error('Explicitly opt out of print()');
// NOTE: the default (LogOutput.auto) already prefers developer.log() in
// debug builds, so `output: LogOutput.developerLog` is only needed to force
// it (e.g. in release builds).
print('\n=== Custom Format with Named Logger ===');
const LiteLogger(name: 'Custom', format: '@{icon} @{level}: @{message}')
..info('Custom format example')
..success('With logger name prefix');
print('\n=== Lazy Evaluation Example ===');
const logger = LiteLogger(name: 'Lazy');
var expensiveCallCount = 0;
logger.debug(() {
expensiveCallCount++;
// This function is only called if debug level is enabled
return 'Expensive computation result: '
'${DateTime.now().millisecondsSinceEpoch}';
});
print('Expensive function was called $expensiveCallCount time(s)');
// With minLevel above debug, the function won't be called
const LiteLogger(minLevel: LogLevel.warning).debug(() {
// This will never execute
return 'This is expensive and will not run';
});
}