Skip to content

Commit d3f8a4b

Browse files
authored
Merge pull request #62 from gadenbuie/appender-console-message
Add option to output to stdout or stderr in AppenderConsole
2 parents c725c92 + 60aa371 commit d3f8a4b

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

R/Appender.R

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ Appender <- R6::R6Class(
139139
#' **crayon** installed log levels will be coloured by default
140140
#' (but you can modify this behaviour by passing a custom [Layout]).
141141
#'
142+
#' @param output Choose whether the message is printed to the \R console as
143+
#' regular output (`"stdout"`, default in most cases) or as a message
144+
#' (`"stderr"`, default inside a \pkg{knitr} rendering process).
145+
#'
142146
#' @family Appenders
143147
#' @seealso [LayoutFormat]
144148
#' @export
@@ -168,24 +172,43 @@ AppenderConsole <- R6::R6Class(
168172
timestamp_fmt = "%H:%M:%OS3",
169173
colors = getOption("lgr.colors", list())
170174
),
171-
filters = NULL
175+
filters = NULL,
176+
output = NULL
172177
){
173178
self$set_threshold(threshold)
174179
self$set_layout(layout)
175180
self$set_filters(filters)
181+
if (is.null(output)) {
182+
output <- default_console_output(output)
183+
}
184+
private$output <- match.arg(output, c("stdout", "stderr"))
176185
},
177186

178187
append = function(event){
179-
cat(private$.layout$format_event(event), sep = "\n")
188+
output <- switch(private$output, stdout = stdout(), stderr = stderr())
189+
cat(private$.layout$format_event(event), sep = "\n", file = output)
180190
}
181191
),
182192

183193
active = list(
184194
destination = function() "console"
195+
),
196+
197+
private = list(
198+
output = "stdout"
185199
)
186200
)
187201

202+
default_console_output <- function(output) {
203+
if (!is.null(output)) output
188204

205+
# In knitr, default to using stderr where log messages can be better handled
206+
if (isTRUE(getOption("knitr.in.progress", FALSE))) {
207+
return("stderr")
208+
}
209+
210+
"stdout"
211+
}
189212

190213

191214
# AppenderFile ------------------------------------------------------------

man/AppenderConsole.Rd

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_Appender.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,41 @@ test_that("AppenderConsole: $filter() works", {
202202
})
203203

204204

205+
test_that("AppenderConsole: $new(output = \"stderr\") works", {
206+
app <- AppenderConsole$new(output = "stderr")
207+
208+
std_out <- textConnection("msg_out", open = "w", local = TRUE)
209+
sink(std_out, append = TRUE, type = "message")
210+
on.exit(sink(NULL, type = "message"), add = TRUE)
211+
212+
expect_silent(
213+
app$append(event)
214+
)
215+
216+
expect_match(
217+
msg_out,
218+
"ERROR.*:19:33.*foo.*bar"
219+
)
220+
})
221+
222+
test_that("AppenderConsole: chooses stderr by default when in knitr", {
223+
opts <- options(knitr.in.progress = TRUE)
224+
on.exit(options(opts), add = TRUE)
225+
226+
app <- AppenderConsole$new()
227+
std_out <- textConnection("msg_out", open = "w", local = TRUE)
228+
sink(std_out, append = TRUE, type = "message")
229+
on.exit(sink(NULL, type = "message"), add = TRUE)
230+
231+
expect_silent(
232+
app$append(event)
233+
)
234+
235+
expect_match(
236+
msg_out,
237+
"ERROR.*:19:33.*foo.*bar"
238+
)
239+
})
205240

206241

207242
# AppenderBuffer ----------------------------------------------------

0 commit comments

Comments
 (0)