dart_logging
Pino-style structured logging with child loggers. Provides hierarchical logging with automatic context inheritance.
Installation
dependencies:
dart_logging: ^0.11.0-beta
Quick Start
import 'package:dart_logging/dart_logging.dart';
void main() {
final context = createLoggingContext(
transports: [logTransport(logToConsole)],
);
final logger = createLoggerWithContext(context);
logger.info('Hello world');
logger.warn('Something might be wrong');
logger.error('Something went wrong');
// Child logger with inherited context
final childLogger = logger.child({'requestId': 'abc-123'});
childLogger.info('Processing request'); // requestId auto-included
}
Core Concepts
Logging Context
Create a logging context with one or more transports:
final context = createLoggingContext(
transports: [logTransport(logToConsole)],
);
Log Levels
Standard log levels are available (from lowest to highest severity):
logger.trace('Very detailed trace info');
logger.debug('Debugging info');
logger.info('Information');
logger.warn('Warning');
logger.error('Error occurred');
logger.fatal('Fatal error');
Structured Data
Pass structured data with log messages:
logger.info('User logged in', structuredData: {'userId': 123, 'email': 'user@example.com'});
Child Loggers
Create child loggers that inherit and extend context:
final requestLogger = logger.child({'requestId': 'abc-123'});
requestLogger.info('Start'); // Includes requestId
final userLogger = requestLogger.child({'userId': 456});
userLogger.info('Action'); // Includes both requestId and userId
This is useful for adding context that applies to a scope (like a request handler).
Custom Transports
Create custom transports to send logs to different destinations:
void myTransport(LogEntry entry) {
// Send to external service, file, etc.
print('${entry.level}: ${entry.message}');
}
final context = createLoggingContext(
transports: [logTransport(myTransport)],
);
Example: Express Server Logging
import 'dart:js_interop';
import 'package:dart_node_express/dart_node_express.dart';
import 'package:dart_logging/dart_logging.dart';
void main() {
final logger = createLoggerWithContext(
createLoggingContext(transports: [logTransport(logToConsole)]),
);
final app = express();
app.use(middleware((req, res, next) {
final reqLogger = logger.child({'path': req.path, 'method': req.method});
reqLogger.info('Request received');
next();
}));
app.listen(3000, () {
logger.info('Server started', structuredData: {'port': 3000});
}.toJS);
}
Source Code
The source code is available on GitHub.