Write to syslog from R

Hello FrieseWoudLoper,

This is an example of a log4r appender in combination with the syslognet::syslog function.
Normally I do not use the syslog information, but I installed Kiwi Syslog Server – Free Edition to test the following code.
This is not endorsement for the product (even though it worked out of the box :grinning:)
Check out the parameters of the syslognet::syslog function to see if this might work for you.

Update: Kiwi is listening to port 1468 (unless told otherwise)

library(log4r)
#> 
#> Attaching package: 'log4r'
#> The following object is masked from 'package:base':
#> 
#>     debug

fwl_layout <- function (time_format = "%Y-%m-%d %H:%M:%S") {
  function(level, ...) {
    msg <- paste0(..., collapse = "")
    # sprintf("%-5s [%s] %s\n", level, log4r:::fmt_current_time(time_format), 
    #         msg)
    sprintf("%s %s\n",log4r:::fmt_current_time(time_format), 
            msg)
  }
}

syslog_appender = function(layout=fwl_layout()) {
  stopifnot(is.function(layout))
  layout <- compiler::cmpfun(layout)
  
  function(level, ...) {
    msg <- layout(level, ...)
    #syslognet::syslog(msg,port=1468L)
    syslognet::syslog(msg,host='myhostname',app_name='',proc_id= NULL,port=1468L)
  }
}

fwl_logger <- log4r::logger(threshold = "INFO",appenders= syslog_appender())

log4r_info <- function() {
  log4r::info(fwl_logger, "my_info_message")
}

log4r_error <- function() {
  log4r::error(fwl_logger, "my_error_message")
}

log4r_debug <- function() {
  log4r::debug(fwl_logger, "my_debug_message")
}

log4r_debug()

log4r_info() 

log4r_error() 

Created on 2020-06-15 by the reprex package (v0.3.0)

With entries in syslog:
image

2 Likes