Skip to content

Commit

Permalink
Prevent flushing when already flushing
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Jan 24, 2025
1 parent b217e9c commit 4b04f70
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions lib/src/seq_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,29 +115,38 @@ class SeqLogger {
}

/// Checks if the cache should be flushed based on the backlog limit.
bool shouldFlush() => cache.count >= backlogLimit;
bool shouldFlush() => !_flushing && cache.count >= backlogLimit;

/// Whether the logger is currently flushing events.
bool _flushing = false;

/// Flushes at most [backlogLimit] events in the cache to Seq and updates the
/// minimum log level based on the response from Seq.
Future<void> flush() async {
diagnosticLog(SeqLogLevel.verbose, 'Flushing events');
try {
_flushing = true;

diagnosticLog(SeqLogLevel.verbose, 'Flushing events');

final eventsToBeSent = await cache.peek(backlogLimit).toList();
final eventsToBeSent = await cache.peek(backlogLimit).toList();

await client.sendEvents(eventsToBeSent);
await client.sendEvents(eventsToBeSent);

await cache.remove(eventsToBeSent.length);
await cache.remove(eventsToBeSent.length);

final newLogLevel = client.minimumLevelAccepted;
if (minimumLogLevel != newLogLevel) {
diagnosticLog(
SeqLogLevel.verbose,
'Accepted new log level {MinimumLogLevel}',
null,
{'MinimumLogLevel': newLogLevel},
);
final newLogLevel = client.minimumLevelAccepted;
if (minimumLogLevel != newLogLevel) {
diagnosticLog(
SeqLogLevel.verbose,
'Accepted new log level {MinimumLogLevel}',
null,
{'MinimumLogLevel': newLogLevel},
);

minimumLogLevel = newLogLevel;
minimumLogLevel = newLogLevel;
}
} finally {
_flushing = false;
}
}

Expand Down

0 comments on commit 4b04f70

Please sign in to comment.