@@ -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 ------------------------------------------------------------
0 commit comments