Skip to content

Commit f1d103d

Browse files
authored
Merge pull request #224 from thomasp85/issue110-location-log
Add log location info to meta
2 parents 5f15e68 + cd89a6f commit f1d103d

File tree

7 files changed

+38
-0
lines changed

7 files changed

+38
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# logger (development version)
22

3+
* File and line location of the log call is now available to the layouts (#110, @thomasp85)
34
* Added `log_elapsed()` to show cumulative elapsed running time (@thomasp85)
45
* New `formatter_cli()` allows you to use the syntax from the cli package to create log messages (#210, @thomasp85)
56

R/layouts.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#' * `topenv`: the name of the top environment from which the parent call was called
2323
#' (eg R package name or `GlobalEnv`)
2424
#' * `call`: parent call (if any) calling the logging function
25+
#' * `location`: A list with element `path` and `line` giving the location of the
26+
#' log call
2527
#' * `fn`: function's (if any) name calling the logging function
2628
#'
2729
#' @param log_level log level as per [log_levels()]
@@ -45,6 +47,7 @@ get_logger_meta_variables <- function(log_level = NULL,
4547
topenv = top_env_name(.topenv),
4648
fn = deparse_to_one_line(.topcall[[1]]),
4749
call = deparse_to_one_line(.topcall),
50+
location = log_call_location(.logcall),
4851
time = timestamp,
4952
levelr = log_level,
5053
level = attr(log_level, "level"),

R/logger-meta.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ logger_meta_env <- function(log_level = NULL,
1515
delayedAssign("fn", deparse_to_one_line(.topcall[[1]]), assign.env = env)
1616
delayedAssign("call", deparse_to_one_line(.topcall), assign.env = env)
1717
delayedAssign("topenv", top_env_name(.topenv), assign.env = env)
18+
delayedAssign("location", log_call_location(.logcall), assign.env = env)
1819

1920
env$time <- timestamp
2021
env$levelr <- log_level

R/utils.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@ top_env_name <- function(.topenv = parent.frame()) {
4545
environmentName(topenv(.topenv))
4646
}
4747

48+
#' Finds the location of the logger call (file and line)
49+
#' @return list with path and line element
50+
#' @noRd
51+
#' @param .logcall The call that emitted the log
52+
log_call_location <- function(.logcall) {
53+
call_string <- deparse(.logcall)
54+
loc <- list(
55+
path = "<console>",
56+
line = ""
57+
)
58+
for (trace in .traceback(0)) {
59+
if (identical(call_string, as.vector(trace))) {
60+
ref <- attr(trace, "srcref")
61+
loc$line <- ref[1L]
62+
file <- attr(ref, "srcfile")
63+
if (!is.null(file)) {
64+
loc$path <- normalizePath(file$filename, winslash = "/")
65+
}
66+
break
67+
}
68+
}
69+
loc
70+
}
4871

4972
#' Deparse and join all lines into a single line
5073
#'

man/get_logger_meta_variables.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/helper.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Do not move this to another line as the location of this piece of code is tested for
2+
test_info <- function() {
3+
log_info("TEST")
4+
}
5+
16
local_test_logger <- function(threshold = INFO,
27
formatter = formatter_glue,
38
layout = layout_simple,

tests/testthat/test-logger-meta.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ test_that("captures other environmental metadata", {
3131
expect_equal(env$os_release, sysinfo$release)
3232
expect_equal(env$os_version, sysinfo$version)
3333
expect_equal(env$user, sysinfo$user)
34+
35+
local_test_logger(layout = layout_glue_generator("{location$path}#{location$line}: {msg}"))
36+
expect_output(test_info(), file.path(getwd(), "helper.R#3: TEST"))
3437
})

0 commit comments

Comments
 (0)