Skip to content

Commit

Permalink
Add log location info to meta
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Jan 22, 2025
1 parent 4d6ddb4 commit 5343cb3
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 0 deletions.
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)
* File and line location of the log call is now available to the layouts (#110, @thomasp85)

## Fixes

Expand Down
3 changes: 3 additions & 0 deletions R/layouts.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#' * `topenv`: the name of the top environment from which the parent call was called
#' (eg R package name or `GlobalEnv`)
#' * `call`: parent call (if any) calling the logging function
#' * `location`: A list with element `path` and `line` giving the location of the
#' log call
#' * `fn`: function's (if any) name calling the logging function
#'
#' @param log_level log level as per [log_levels()]
Expand All @@ -45,6 +47,7 @@ get_logger_meta_variables <- function(log_level = NULL,
topenv = top_env_name(.topenv),
fn = deparse_to_one_line(.topcall[[1]]),
call = deparse_to_one_line(.topcall),
location = log_call_location(.logcall),
time = timestamp,
levelr = log_level,
level = attr(log_level, "level"),
Expand Down
1 change: 1 addition & 0 deletions R/logger-meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ logger_meta_env <- function(log_level = NULL,
delayedAssign("fn", deparse_to_one_line(.topcall[[1]]), assign.env = env)
delayedAssign("call", deparse_to_one_line(.topcall), assign.env = env)
delayedAssign("topenv", top_env_name(.topenv), assign.env = env)
delayedAssign("location", log_call_location(.logcall), assign.env = env)

env$time <- timestamp
env$levelr <- log_level
Expand Down
23 changes: 23 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@ top_env_name <- function(.topenv = parent.frame()) {
environmentName(topenv(.topenv))
}

#' Finds the location of the logger call (file and line)
#' @return list with path and line element
#' @noRd
#' @param .logcall The call that emitted the log
log_call_location <- function(.logcall) {
call_string <- deparse(.logcall)
loc <- list(
path = "<console>",
line = ""
)
for (trace in .traceback(0)) {
if (identical(call_string, as.vector(trace))) {
ref <- attr(trace, "srcref")
loc$line <- ref[1L]
file <- attr(ref, "srcfile")
if (!is.null(file)) {
loc$path <- file$filename
}
break
}
}
loc
}

#' Deparse and join all lines into a single line
#'
Expand Down
2 changes: 2 additions & 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.

5 changes: 5 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Do not move this to another line as the location of this piece of code is tested for
test_info <- function() {
log_info("TEST")
}

local_test_logger <- function(threshold = INFO,
formatter = formatter_glue,
layout = layout_simple,
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-logger-meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ test_that("captures other environmental metadata", {
expect_equal(env$os_release, sysinfo$release)
expect_equal(env$os_version, sysinfo$version)
expect_equal(env$user, sysinfo$user)

local_test_logger(layout = layout_glue_generator("{location$path}#{location$line}: {msg}"))
expect_output(test_info(), "logger/tests/testthat/helper.R#3: TEST")
})

0 comments on commit 5343cb3

Please sign in to comment.