@@ -2887,9 +2887,100 @@ AppenderFileRotatingDate <- R6::R6Class(
28872887)
28882888
28892889
2890+ # AppenderSyslog ----------------------------------------------------------
28902891
2892+ # ' Log to the POSIX System Log
2893+ # '
2894+ # ' An Appender that writes to Syslog on supported POSIX platforms. Requires the
2895+ # ' \code{rsyslog} package.
2896+ # '
2897+ # ' @eval r6_usage(AppenderSyslog)
2898+ # '
2899+ # ' @inheritSection Appender Creating a New Appender
2900+ # ' @inheritSection Appender Fields
2901+ # ' @inheritSection Appender Methods
2902+ # '
2903+ # ' @section Fields:
2904+ # '
2905+ # ' \describe{
2906+ # ' \item{`identifier`}{`character` scalar. A string identifying the process.}
2907+ # ' \item{`...`}{Further arguments passed on to \code{\link[rsyslog]{open_syslog}}.}
2908+ # ' }
2909+ # '
2910+ # ' @export
2911+ # ' @seealso [LayoutFormat], [LayoutJson]
2912+ # ' @family Appenders
2913+ # ' @name AppenderSyslog
2914+ # '
2915+ # ' @examples
2916+ # ' if (requireNamespace("rsyslog", quietly = TRUE)) {
2917+ # ' lg <- get_logger("test")
2918+ # ' lg$add_appender(AppenderSyslog$new("myapp"), "syslog")
2919+ # '
2920+ # ' lg$info("A test message")
2921+ # '
2922+ # ' lg$config(NULL)
2923+ # ' }
2924+ NULL
28912925
28922926
2927+ # ' @export
2928+ AppenderSyslog <- R6 :: R6Class(
2929+ " AppenderSyslog" ,
2930+ inherit = Appender ,
2931+ cloneable = FALSE ,
2932+ public = list (
2933+ initialize = function (
2934+ identifier ,
2935+ threshold = NA_integer_ ,
2936+ layout = LayoutFormat $ new(),
2937+ filters = NULL ,
2938+ ...
2939+ ){
2940+ if (! requireNamespace(" rsyslog" , quietly = TRUE )) {
2941+ stop(" The 'rsyslog' package is required for this appender." )
2942+ }
2943+ self $ set_threshold(threshold )
2944+ self $ set_layout(layout )
2945+ self $ set_filters(filters )
2946+ private $ .identifier <- identifier
2947+
2948+ rsyslog :: open_syslog(identifier = identifier , ... )
2949+ # Handle the mask manually via the threshold.
2950+ rsyslog :: set_syslog_mask(" DEBUG" )
2951+ },
2952+
2953+ append = function (event ){
2954+ rsyslog :: syslog(
2955+ private $ .layout $ format_event(event ),
2956+ level = private $ to_syslog_level(event $ level )
2957+ )
2958+ }
2959+ ),
2960+
2961+ # +- active ---------------------------------------------------------------
2962+ active = list (
2963+ destination = function () sprintf(" syslog [%s]" , private $ .identifier )
2964+ ),
2965+
2966+ private = list (
2967+ finalize = function (){
2968+ rsyslog :: close_syslog()
2969+ },
2970+
2971+ to_syslog_level = function (level ){
2972+ # Turn the level into a digit from 1-5, rounding down (and thus "up" in
2973+ # priority).
2974+ digit <- floor(as.integer(pmin(level , 500 , na.rm = TRUE )) / 100.0 )
2975+ switch (
2976+ as.character(digit ), " 1" = " CRITICAL" , " 2" = " ERR" , " 3" = " WARNING" ,
2977+ " 4" = " INFO" , " 5" = " DEBUG"
2978+ )
2979+ },
2980+
2981+ .identifier = " "
2982+ )
2983+ )
28932984
28942985
28952986# utils -------------------------------------------------------------------
0 commit comments