onLog method
void
onLog(
- LogLevel level,
- dynamic message, {
- String? tag,
- Object? error,
- StackTrace? stackTrace,
Called when a new log entry is emitted by a logger.
This method provides complete context about the log event, including:
level
: the severity or type of the log (e.g., info, error, debug)timestamp
: the exact time the log entry was recordedmessage
: the textual content of the logtag
: the name or label of the logger that emitted the message
This method is called immediately after the message is stored in the logger’s internal buffer, allowing real-time response.
Example
void onLog(LogLevel level, DateTime timestamp, String message, String tag) {
sendToRemoteServer({
'level': level.name,
'time': timestamp.toIso8601String(),
'source': tag,
'log': message,
});
}
Implementation
void onLog(LogLevel level, dynamic message, {String? tag, Object? error, StackTrace? stackTrace}) {
if (!_level.isEnabledFor(level)) return;
final record = LogRecord(
level,
message.toString(),
loggerName: (tag ?? _name).replaceAll("[", "").replaceAll("]", ""),
error: error,
stackTrace: stackTrace,
);
final lines = _printer.log(record);
final outputFn = _output ?? _defaultOutput;
for (final line in lines) {
outputFn(line);
}
}