write<TState> method
void
write<TState>({
- required LogEntry<
TState> logEntry, - ExternalScopeProvider? scopeProvider,
- required StringBuffer textWriter,
override
Writes the log message to the specified StringBuffer.
Implementations can use ANSI color codes in the output for console coloring.
Implementation
@override
void write<TState>({
required LogEntry<TState> logEntry,
ExternalScopeProvider? scopeProvider,
required StringBuffer textWriter,
}) {
final message = logEntry.formatter(logEntry.state, logEntry.exception);
final json = <String, dynamic>{
'Timestamp': _formatTimestamp(
_options.useUtcTimestamp ? DateTime.now().toUtc() : DateTime.now(),
),
'LogLevel': _getLogLevelString(logEntry.logLevel),
'Category': logEntry.category,
'EventId': {
'Id': logEntry.eventId.id,
if (logEntry.eventId.name != null) 'Name': logEntry.eventId.name,
},
if (message.isNotEmpty) 'Message': message,
};
// Add scopes if available
if (_options.includeScopes && scopeProvider != null) {
final scopes = <Object?>[];
scopeProvider.forEachScope<Object?>((scope, _) {
scopes.add(scope);
}, null);
if (scopes.isNotEmpty) {
json['Scopes'] = scopes;
}
}
// Add state if it's a Map
if (logEntry.state is Map<String, dynamic>) {
json['State'] = logEntry.state;
}
// Add exception if present
if (logEntry.exception != null) {
json['Exception'] = {
'Type': logEntry.exception.runtimeType.toString(),
'Message': logEntry.exception.toString(),
};
}
// Encode to JSON
final encoder = _options.useJsonIndentation
? const JsonEncoder.withIndent(' ')
: const JsonEncoder();
textWriter.write(encoder.convert(json));
}