Skip to content

[WIP] Set up logging and alerting with CloudWatch - #20

Draft
wagnerlmichael wants to merge 5 commits into
masterfrom
mwagner/13-set-up-logging-and-alerting-with-cloudwatch
Draft

wagnerlmichael wants to merge 5 commits into
masterfrom
mwagner/13-set-up-logging-and-alerting-with-cloudwatch

Conversation

@wagnerlmichael

Copy link
Copy Markdown
Member

[WIP]

@wagnerlmichael wagnerlmichael linked an issue Sep 17, 2026 that may be closed by this pull request
Comment thread logging.R
# Log records are built from the named arguments passed to the logging call,
# so every call should pass a `msg` plus any other fields worth recording
logger::log_formatter(logger::formatter_json)
logger::log_layout(logger::layout_json_parser(fields = c("time", "level")))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Are time and level the layout fields that we want?

Comment thread logging.R
# The layout above always writes `time` and then `level` as the first two
# fields of the record, so we can route on the level without parsing the JSON
appender_split_by_level <- function(lines) {
is_error <- grepl('^\\{"time":"[^"]*","level":"(ERROR|FATAL)"', lines)

@wagnerlmichael wagnerlmichael Sep 17, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This grepl grabs the error and fatal levels. I'm curious if this is what you meant by

Emit errors to stderr with full traceback and an ERROR log level

In my crash course on logging, I see that these are the different levels: docs. Are error and fatal levels that are typically routed to stderr instead of stdout?

From what I can tell this split doesn't actually impact anything, and that if we removed the split it would land in cloudwatch the same, what do you think?

Comment thread logging.R
@@ -0,0 +1,86 @@
# Logging setup and helpers for the API. Every log record is a single line of
# JSON so that CloudWatch (or any other log consumer) can parse its fields.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There is a formatter, layout, appender anatomy https://daroczig.github.io/logger/articles/anatomy.html

Comment thread logging.R
}

# Plumber error handler. Mirrors the response produced by plumber's default
# handler (plumber:::defaultErrorHandler) but replaces its print(err) with a

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Set up logging and alerting with CloudWatch

1 participant