Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

L-2183 Ensure flushed logs in long running scripts #22

Merged
merged 9 commits into from
Jun 27, 2024
Merged

Conversation

PetrHeinz
Copy link
Member

@PetrHeinz PetrHeinz commented Jun 26, 2024

Resolves #11, resolves #21

Adds new option flushIntervalMs for LogtailHandler which ensures that logs are sent at least every X (every 5 seconds by default). This assumes that logs are created somewhat regularly.

In default configuration, long running script would eventually run out of memory. For this reason, I've changed default bufferLimit to 1000 and changed default behavior from dropping logs to flushing logs on overflow. Also introduced throwExceptions option to control whether we should throw RuntimeException on curl failure, which is by default false now to prevent apps from crashing.

Since we have a bit too many parameters at this point, I've created a builder for comfortable handler creation (all methods will be suggested by IDE, all options named nicely). As an example, see comparison of suggested config from #21:

$logger = new Logger("example-app");
$logger->pushHandler(
      new LogtailHandler(
          "$SOURCE_TOKEN",
          $this->log_level,
          true,
          \Logtail\Monolog\LogtailClient::URL,
          5, //will send logs to Better Stack every fifth log record. You can adjust this to minimise traffic if your script produces lot of messages.
          true //send messages instead of removing old messages
      ));
$logger = new Logger("example-app");
$handler = LogtailHandlerBuilder::withSourceToken("$SOURCE_TOKEN")
  ->withLevel($this->log_level)
  ->withFlushOnOverflow(true)
  ->withBufferLimit(5)
  ->build();
$logger->pushHandler($handler);

@PetrHeinz PetrHeinz requested a review from curusarn June 26, 2024 15:58
@PetrHeinz
Copy link
Member Author

Planning on releasing this as 2.2.0 and 3.2.0 (after adding static types and merging into main branch).

I've added a note into our PHP logging docs about terminating scripts with CTRL+C, but made no effort on implementing it into our library (it doesn't seem to be part of our responsibility).

I will update the code snippets to use LogtailHandlerBuilder instead after release.

Copy link
Contributor

@curusarn curusarn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Left on comment.

@PetrHeinz PetrHeinz merged commit e6be89f into 2.x Jun 27, 2024
14 checks passed
@PetrHeinz PetrHeinz deleted the ph/ensure-flush branch June 27, 2024 09:58
@PetrHeinz PetrHeinz mentioned this pull request Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants