Created
April 2, 2020 11:36
-
-
Save gupta-himanshu/b45acd89b5b33908fe3d65a4de799762 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Package for Log4R | |
library("log4r") | |
# Default Logger | |
default_logger <- logger() | |
# Debug level enabled Logger (by default DEBUG level is suppressed) | |
debug_logger <- logger("DEBUG") | |
# File Logger | |
log_file <- "sample.log" | |
file_logger <- logger("WARN", appenders = file_appender(log_file)) | |
# Sample program | |
urls <- c( | |
"http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html", | |
"http://en.wikipedia.org/wiki/Xz", | |
"yyyyy" | |
) | |
readUrl <- function(url) { | |
out <- tryCatch( | |
{ | |
debug(debug_logger, "This is the 'try' part") | |
readLines(con=url, warn=FALSE) | |
}, | |
error=function(cond) { | |
error(file_logger, paste("URL does not seem to exist:", url)) | |
error(file_logger, "Here's the original error message:") | |
error(file_logger, cond) | |
return(NA) | |
}, | |
warning=function(cond) { | |
warn(file_logger, paste("URL caused a warning:", url)) | |
warn(file_logger, paste("Here's the original warning message:")) | |
warn(file_logger, cond) | |
return(NULL) | |
}, | |
finally={ | |
info(default_logger, paste("Processed URL:", url)) | |
info(default_logger, "Some other message at the end") | |
} | |
) | |
return(out) | |
} | |
for (url in urls) { | |
readUrl(url) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment