Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export(formatter_sprintf)
export(get_logger_meta_variables)
export(grayscale_by_log_level)
export(layout_blank)
export(layout_gha)
export(layout_glue)
export(layout_glue_colors)
export(layout_glue_generator)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* `log_appender()`, `log_layout()` and `log_formatter()` now check that you are calling them with a function, and return the previously set value (#170, @hadley)
* new function to return number of log indices (#194, @WurmPeter)
* `appender_async` is now using `mirai` instead of a custom background process and queue system (#214, @hadley @shikokuchuo)
* `layout_gha()` for providing native GitHub Action logging. This automatically gets used when running code in github actions (@thomasp85)

## Fixes

Expand Down
41 changes: 41 additions & 0 deletions R/layouts.R
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,47 @@ default_fields <- function() {
)
}

#' Format a log record for github actions
#'
#' GitHub Actions can recognise specially formatted output and make these
#' prominent in the output. The `layout_gha()` layout will ensure the correct
#' formatting when running in GitHub Actions.
#'
#' @note
#' GitHub Actions only recognise the log levels `error`, `warning`, `notice`,
#' and `debug`. Because of this, `FATAL` and `ERROR` are coerced to `error`,
#' `SUCCESS` and `INFO` are coerced to `notice`, and `DEBUG` and `TRACE` are
#' coerced to `debug` (`WARN` maps directly to `warning`).
#'
#' @inheritParams layout_simple
#' @return character vector
#' @export
#' @family log_layouts
#'
layout_gha <- structure(
function(level,
msg,
namespace = NA_character_,
.logcall = sys.call(),
.topcall = sys.call(-1),
.topenv = parent.frame()) {
level <- attr(level, "level")
gha_level <- switch(level,
FATAL = ,
ERROR = "error",
WARN = "warning",
SUCCESS = ,
INFO = "notice",
DEBUG = ,
TRACE = "debug",
"notice" # Defaults to notice if for some reason another level gets used
)
title <- if (gha_level == "debug") "" else paste0(" title=", level)
paste0("::", gha_level, title, "::", msg)
},
generator = quote(layout_gha())
)

# nocov start
#' Format a log record for syslognet
#'
Expand Down
5 changes: 3 additions & 2 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ namespaces_reset <- function() {

namespaces_default <- function() {
has_glue <- requireNamespace("glue", quietly = TRUE)
is_running_gha <- isTRUE(Sys.getenv("GITHUB_ACTIONS") == "true") && !needs_stdout()
Copy link
Owner

Choose a reason for hiding this comment

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

I need to think about this one, as I can imagine (and know) folks using logger in GHA writing outside of the GHA logs, although hopefully explicitly setting the layout as well 🤔

Anyway, this is a super cool idea, thank you! I'm traveling/teaching in the next ~48 hours, but I will definitely get back to this PR for a more detailed look on Friday.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I can imagine. Ultimatively it is your call and I understand being weary of messing up peoples current deployment. On the other hand it would be a nice quality-of-life improvement for new projects


list(
global = list(
default = list(
threshold = as.loglevel(Sys.getenv("LOGGER_LOG_LEVEL", unset = "INFO")),
layout = layout_simple,
layout = if (is_running_gha) layout_gha else layout_simple,
formatter = if (has_glue) formatter_glue else formatter_sprintf,
appender = if (needs_stdout()) appender_stdout else appender_console
)
),
.logger = list(
default = list(
threshold = ERROR,
layout = layout_simple,
layout = if (is_running_gha) layout_gha else layout_simple,
formatter = formatter_sprintf,
appender = if (needs_stdout()) appender_stdout else appender_console
)
Expand Down
1 change: 1 addition & 0 deletions man/get_logger_meta_variables.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_blank.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions man/layout_gha.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_glue.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_glue_colors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_glue_generator.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_json.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_json_parser.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_logging.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_simple.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading