logging library

Provides a flexible logging infrastructure with support for multiple log providers and structured logging.

This library implements a comprehensive logging system inspired by Microsoft.Extensions.Logging, supporting various output formats and log levels with dependency injection integration.

Basic Usage

Configure logging with dependency injection:

final services = ServiceCollection()
  ..addLogging((builder) {
    builder
      ..setMinimumLevel(LogLevel.debug)
      ..addSimpleConsole()
      ..addDebug();
  });

final provider = services.buildServiceProvider();
final logger = provider.getRequiredService<ILogger>();

Logging Messages

Log messages at different severity levels:

logger.logInformation('Application started');
logger.logWarning('Low disk space');
logger.logError('Failed to connect', error: exception);
logger.logDebug('Cache hit for key: {Key}', ['user123']);

Console Formatters

Multiple console output formats are available:

  • Simple: Human-readable format with colors
  • JSON: Structured JSON output for log aggregation
  • Systemd: Systemd journal compatible format
builder.addJsonConsole();  // JSON format
builder.addSystemdConsole();  // Systemd format

Scoped Logging

Create log scopes for contextual information:

using((scope) {
  logger.logInformation('Processing order');
  // All logs in this scope include the order ID
}, logger.beginScope({'OrderId': '12345'}));

Classes

BufferedLogger
Enables logging providers to handle batched log entries.
BufferedLogRecord
Represents a buffered log record to be written in batch to a buffered logger.
BufferedLogRecordImpl
Default implementation of BufferedLogRecord.
ConsoleFormatter
Allows custom log message formatting for console output.
ConsoleFormatterNames
Reserved formatter names for built-in console formatters.
ConsoleFormatterOptions
Base options class for console log formatters.
ConsoleLogger
A logger that writes messages to the console output.
ConsoleLoggerProvider
The provider for the ConsoleLogger.
DebugLogger
A logger that writes messages in the debug output window only when a debugger is attached.
DebugLoggerProvider
The provider for the DebugLogger.
DefaultLoggerLevelConfigureOptions
EventId
Identifies a logging event.
FormattedConsoleLogger
A logger that writes formatted messages to the console output.
FormattedConsoleLoggerProvider
Provider for FormattedConsoleLogger instances.
JsonConsoleFormatter
A console formatter that formats log messages as JSON.
JsonConsoleFormatterOptions
Options for the built-in JSON console log formatter.
LogDefineOptions
Options for configuring logger message delegates.
LogEntry<TState>
Holds information about a log entry.
Logger
Represents a type used to perform logging.
LoggerFactory
Represents a type used to configure the logging system and create instances of Logger from the registered LoggerProviders.
LoggerFactoryOptions
The options for a LoggerFactory.
LoggerFilterOptions
The options for a LoggerFilter.
LoggerFilterRule
Defines a rule used to filter log messages.
LoggerMessage
Creates delegates for logging that can be cached and reused for improved performance.
LoggerProvider
Represents a type that can create instances of Logger.
LoggingBuilder
An interface for configuring logging providers.
NullLogger
Minimalistic logger that does nothing.
NullLoggerFactory
An LoggerFactory used to create instance of NullLogger that logs nothing.
NullScope
An empty scope without any logic.
NullTypedLogger<T>
Generic null logger that does nothing.
Scope
SimpleConsoleFormatter
A simple console formatter that formats log messages with timestamps, log levels, categories, and exception details.
SimpleConsoleFormatterOptions
Options for the built-in simple console log formatter.
StaticFilterOptionsMonitor
SystemdConsoleFormatter
A console formatter that formats log messages for systemd journal.
SystemdConsoleFormatterOptions
Options for the built-in systemd console log formatter.
TypedLogger<T>
A generic logger interface used to enable activation of a named logger from dependency injection.
TypedLoggerImpl<T>
Implementation of TypedLogger that wraps an underlying logger instance.

Enums

LoggerColorBehavior
Describes when to use color when logging messages.
LogLevel

Extensions

ConsoleLoggerFactoryExtensions on LoggingBuilder
Extension methods for the LoggerFactory class.
DebugLoggerFactoryExtensions on LoggingBuilder
Extension methods for the LoggerFactory class.
FilterLoggingBuilderExtensions on LoggingBuilder
Extension methods for setting up logging services in an ServiceCollection.
LoggerExtensions on Logger
LoggerFactoryExtensions on LoggerFactory
LoggerFactory extension methods for common scenarios.
LoggerFilterOptionsExtensions on LoggerFilterOptions
LoggingBuilderExtensions on LoggingBuilder
Extension methods for setting up logging services in a LoggingBuilder.
LoggingServiceCollectionExtensions on ServiceCollection
Extension methods for setting up logging services in a ServiceCollection.
LogLevelExtensions on LogLevel
TypedLoggerFactoryExtensions on LoggerFactory
Extension methods for creating typed loggers.

Typedefs

CategoryLevelFilterAction = bool Function(String? category, LogLevel level)
ConfigureFilter = bool Function(String name, String category, LogLevel level)
ConfigureLoggerFactoryOptions = void Function(LoggerFactoryOptions options)
ConfigureLoggingBuilder = void Function(LoggingBuilder builder)
ConfigureOptionsAction1 = void Function(LoggerFilterOptions options)
FilterAction = bool Function(String provider, String category, LogLevel level)
LevelFilterAction = bool Function(LogLevel level)
LogFormatter<TState> = String Function(TState state, Object? error)
Function to create a String message of the state and exception.