add method
Adds a message
to the log buffer under the specified level
.
If the level already contains entries, the new message is appended.
Example:
logger.add(LogLevel.debug, "Fetching user profile...");
If canPublish is true, the log is published to the console.
If canPublish is false, the log is stored in the internal log buffer.
A base class for building structured logging mechanisms within Jet-based applications.
This class provides an in-memory logging facility with support for multiple LogLevels. Subclasses can extend this base to implement custom logging strategies (e.g., file, console, remote).
Each logger instance is tagged with a custom tag to allow filtering or identification in composite logs.
Usage
Extend this class to build a concrete logger:
class ConsoleLogger extends LoggerFactory {
ConsoleLogger(String tag) : super(tag);
void flush() {
_logs.forEach((level, messages) {
for (final message in messages) {
print('[$tag][$level] $message');
}
});
}
}
Then use:
final logger = ConsoleLogger("MyService");
logger.add(LogLevel.info, "Service started.");
logger.add(LogLevel.error, "Something went wrong.");
logger.flush();
Implementation
void add(LogLevel level, String message, {Object? error, StackTrace? stacktrace}) {
final timestamp = DateTime.now();
if(canPublish) {
_log(level, message, error: error, stacktrace: stacktrace);
} else {
_logs.update(
level,
(details) => details..add(timestamp, _prepareContent(message, error: error, stacktrace: stacktrace)),
ifAbsent: () => {timestamp: _prepareContent(message, error: error, stacktrace: stacktrace)},
);
}
}