Skip to content

Commit

Permalink
Add log to file
Browse files Browse the repository at this point in the history
Enable logging to a file via

```
options(jupyter.log_level = 3) # log debug
options(jupyter.log_to_file = TRUE) # log to file
options(jupyter.logfile = 'irkernel.log') # default, relative to current dir!
```

The default log is 'irkernel.log' in the current directory. This probably should
change, but I haven't yet looked where the other kernels log to...
  • Loading branch information
jankatins committed Apr 10, 2016
1 parent ad8e7db commit 60c0c34
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
46 changes: 40 additions & 6 deletions R/logging.r
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,53 @@
stderror <- stderr()

log_debug <- function(...) {
if (getOption('jupyter.log_level') >= 3L) {
cat('DEBUG: ', sprintf(...), '\n', sep = '', file = stderror)
if (isTRUE(getOption('jupyter.log_level') >= 3L)) {
log_msg_stderror('DEBUG', sprintf(...))
log_msg_logfile('DEBUG', sprintf(...))
}
}

log_info <- function(...) {
if (getOption('jupyter.log_level') >= 2L) {
cat('INFO: ', sprintf(...), '\n', sep = '', file = stderror)
if (isTRUE(getOption('jupyter.log_level') >= 2L)) {
log_msg_stderror('INFO', sprintf(...))
log_msg_logfile('INFO', sprintf(...))
}
}

log_error <- function(...) {
if (getOption('jupyter.log_level') >= 1L) {
cat('INFO: ', sprintf(...), '\n', sep = '', file = stderror)
if (isTRUE(getOption('jupyter.log_level') >= 1L)) {
log_msg_stderror('ERROR', sprintf(...))
log_msg_logfile('ERROR', sprintf(...))
}
}

log_msg_stderror <- function(lvl, msg){
cat(sprintf('%s: %s\n', lvl, msg) , file = stderror)
}

.is_changed_logfile <- local({
old_logfile <- ""
function(logfile) {
if (old_logfile != logfile) {
old_logfile <<- old_logfile
TRUE
} else {
FALSE
}
}
})

log_msg_logfile <- function(lvl, msg){
if (isTRUE(getOption('jupyter.log_to_file'))) {
# Use the default again, so we don't crash here if a user remove this option
# TODO: use a better default somewhere in the users home dir? Wherever jupyter itself logs to..
# TODO: this is depended on the current working dir...
cur_logfile <- getOption('jupyter.logfile', "irkernel.log")
if (.is_changed_logfile(cur_logfile)) {
log_msg_stderror('INFO', sprintf('Logging to %s', cur_logfile))
}
log_con <- file(cur_logfile, open='ab')
writeBin(charToRaw(sprintf('%s %s: %s\n', format(Sys.time()), lvl, msg)), log_con, endian='little')
close(log_con)
}
}
2 changes: 2 additions & 0 deletions R/options.r
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ getenv_default <- function(varname, default) {
#' @export
jupyter_option_defaults <- list(
jupyter.log_level = as.integer(getenv_default('JUPYTER_LOG_LEVEL', 1L)),
jupyter.log_to_file = FALSE,
jupyter.logfile = getenv_default('JUPYTER_LOGFILE', 'irkernel.log'),
jupyter.pager_classes = c(
'help_files_with_topic'),
jupyter.plot_mimetypes = c(
Expand Down

0 comments on commit 60c0c34

Please sign in to comment.