You can use sink()
to divert messages as well as warnings to a file. The trick is to set the argument type="message"
:
Here is an example adapted from the help for ?sink
:
setwd(tempdir())
## capture messages and errors to a file.
zz <- file("all.Rout", open="wt")
sink(zz, type="message")
try(log("a"))
## reset message sink and close the file connection
sink(type="message")
close(zz)
## Display the log file
readLines("all.Rout")
[1] "Error in log("a") : Non-numeric argument to mathematical function"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…