Skip to content

feat: upgrade logger #1265

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

Open
wants to merge 18 commits into
base: feat/upgrade-logger
Choose a base branch
from

Conversation

tckeong
Copy link

@tckeong tckeong commented Jun 20, 2025

This PR upgrade the original logger, add a new logger backend (logger driver). The new logger backend, CloudWatch Logger, enabling buffered delivery of your application logs to AWS CloudWatch using ExAws.


How It Works

  1. Initialization
    On startup, this backend reads configuration options (:log_group, :log_stream, :level, :format, :metadata) to set up its internal state.

  2. Buffering Logs
    When the application emits a log, the handle_event/2 callback formats it, attaches metadata, timestamps it, and buffers it.

  3. Flush Trigger
    Logs are flushed in two cases:

    • The buffer reaches the configured @max_buffer_size (immediately triggers async flush).
    • A periodic timer (@flush_interval) sends a :flush_buffer message, flushing any pending logs.
  4. AWS Auth Check
    Before sending, the logger verifies that AWS credentials and region are configured via environment variables. If missing, the flush is skipped (avoiding endless retry loops).

  5. Retry Logic
    Logs are sent using ExAws.request/1, in %ExAws.Operation.JSON{} format. On failure, it retries up to @max_retries, with a delay of @retry_delay between attempts. An error is logged on each failure.


Configuration Options

Add to your config/*.exs:

config :logger,
  backends: [:console, {Cadet.Logger.CloudWatchLogger, :cloudwatch_logger}]

config :logger, :cloudwatch_logger,
  level: :info,
  format: "$time $metadata[$level] $message\n",
  metadata: [:request_id],
  log_group: "cadet-logs",
  log_stream: "#{node()}-#{:os.system_time(:second)}",
  • :log_group / :log_stream - CloudWatch Logs group and stream names.
  • :level, :format, :metadata – Same options as Logger.Formatter.

This PR integrates with Elixir’s Logger (:gen_event), offering buffered, retry-enabled delivery to AWS CloudWatch.

@tckeong tckeong self-assigned this Jun 20, 2025
@tckeong tckeong requested review from Copilot and RichDom2185 and removed request for Copilot June 20, 2025 10:05
Copilot

This comment was marked as resolved.

dependabot bot and others added 8 commits June 21, 2025 14:40
Bumps [sentry](https://github.com/getsentry/sentry-elixir) from 11.0.0 to 11.0.1.
- [Release notes](https://github.com/getsentry/sentry-elixir/releases)
- [Changelog](https://github.com/getsentry/sentry-elixir/blob/master/CHANGELOG.md)
- [Commits](getsentry/sentry-elixir@11.0.0...11.0.1)

---
updated-dependencies:
- dependency-name: sentry
  dependency-version: 11.0.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [phoenix_ecto](https://github.com/phoenixframework/phoenix_ecto) from 4.6.4 to 4.6.5.
- [Changelog](https://github.com/phoenixframework/phoenix_ecto/blob/main/CHANGELOG.md)
- [Commits](https://github.com/phoenixframework/phoenix_ecto/commits/v4.6.5)

---
updated-dependencies:
- dependency-name: phoenix_ecto
  dependency-version: 4.6.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
* Add new method

* Use new method

* Fix credo error

* Fix and update tests
* replace warn with warning

"warning: the log level :warn is deprecated, use :warning instead"

* empty doc

"warning: module attribute @doc was set but no definition follows it"

* replace '' with ""

"warning: using single-quoted strings to represent charlists is deprecated."

* move function

"clauses with the same name and arity (number of arguments) should be grouped together"

* make range explicit

"10..0 has a default step of -1, please write 10..0//-1 instead"

* remove alias

"unused alias"

* rename unused variables

"warning: variable "XXX" is unused"

* remove choices from validate_required

"""
warning: attempting to determine the presence of embed_many field :choices with validate_required/3 ... which has no effect.
You can pass the :required option to Ecto.Changeset.cast_embed/3 to achieve this.
"""

* delete unused variables instead

* Revert "remove choices from validate_required"

This reverts commit 088fa96.
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.

3 participants