Skip to content

Commit 491ebc2

Browse files
committed
make AppenderSyslog use the logger name as default identifier, more flexible to_syslog_levels function
1 parent e64ee82 commit 491ebc2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+2013
-573
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: lgr
33
Title: A Fully Featured Logging Framework
4-
Version: 0.3.1.9000
4+
Version: 0.3.1.9001
55
Authors@R:
66
person(given = "Stefan",
77
family = "Fleck",

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# lgr 0.3.1.9000
22

3+
* Added AppenderSyslog for logging to syslog via
4+
[rsyslog](https://github.com/atheriel/rsyslog) (thanks to atheriel)
5+
36

47
# lgr 0.3.1
58

R/Appender.R

Lines changed: 83 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2892,7 +2892,7 @@ AppenderFileRotatingDate <- R6::R6Class(
28922892
#' Log to the POSIX System Log
28932893
#'
28942894
#' An Appender that writes to Syslog on supported POSIX platforms. Requires the
2895-
#' \code{rsyslog} package.
2895+
#' \pkg{rsyslog} package.
28962896
#'
28972897
#' @eval r6_usage(AppenderSyslog)
28982898
#'
@@ -2903,8 +2903,17 @@ AppenderFileRotatingDate <- R6::R6Class(
29032903
#' @section Fields:
29042904
#'
29052905
#' \describe{
2906-
#' \item{`identifier`}{`character` scalar. A string identifying the process.}
2907-
#' \item{`...`}{Further arguments passed on to \code{\link[rsyslog]{open_syslog}}.}
2906+
#' \item{`identifier`}{`character` scalar. A string identifying the process;
2907+
#' if `NULL` defaults to the logger name}
2908+
#' \item{`syslog_levels`}{a named `character` vector or a `function` mapping
2909+
#' lgr log levels to syslog log levels. If a `character` vector is supplied,
2910+
#' its names must be valid levels as understood by [rsyslog::syslog()]
2911+
#' and its values must be [log levels](log_levels) as understood by lgr
2912+
#' (either `character` or `numeric`). You can also supply a `function` that
2913+
#' transforms numeric lgr log levels into syslog levels.
2914+
#' Please be aware that this function should be able to handle vectors of
2915+
#' arbitrary length.}
2916+
#' \item{`...`}{Further arguments passed on to [rsyslog::open_syslog()]}
29082917
#' }
29092918
#'
29102919
#' @export
@@ -2914,12 +2923,15 @@ AppenderFileRotatingDate <- R6::R6Class(
29142923
#'
29152924
#' @examples
29162925
#' if (requireNamespace("rsyslog", quietly = TRUE)) {
2917-
#' lg <- get_logger("test")
2918-
#' lg$add_appender(AppenderSyslog$new("myapp"), "syslog")
2919-
#'
2926+
#' lg <- get_logger("rsyslog/test")
2927+
#' lg$add_appender(AppenderSyslog$new(), "syslog")
29202928
#' lg$info("A test message")
29212929
#'
2922-
#' lg$config(NULL)
2930+
#' if (Sys.info()[["sysname"]] == "Linux"){
2931+
#' system("journalctl -t 'rsyslog/test'")
2932+
#' }
2933+
#'
2934+
#' invisible(lg$config(NULL)) # cleanup
29232935
#' }
29242936
NULL
29252937

@@ -2931,10 +2943,18 @@ AppenderSyslog <- R6::R6Class(
29312943
cloneable = FALSE,
29322944
public = list(
29332945
initialize = function(
2934-
identifier,
2946+
identifier = NULL,
29352947
threshold = NA_integer_,
2936-
layout = LayoutFormat$new(),
2948+
layout = LayoutFormat$new("%m"),
29372949
filters = NULL,
2950+
syslog_levels = c(
2951+
"CRITICAL" = "fatal",
2952+
"ERR" = "error",
2953+
"WARNING" = "warn",
2954+
"INFO" = "info",
2955+
"DEBUG" = "debug",
2956+
"DEBUG" = "trace"
2957+
),
29382958
...
29392959
){
29402960
if (!requireNamespace("rsyslog", quietly = TRUE)) {
@@ -2943,42 +2963,81 @@ AppenderSyslog <- R6::R6Class(
29432963
self$set_threshold(threshold)
29442964
self$set_layout(layout)
29452965
self$set_filters(filters)
2946-
private$.identifier <- identifier
29472966

2948-
rsyslog::open_syslog(identifier = identifier, ...)
2949-
# Handle the mask manually via the threshold.
2950-
rsyslog::set_syslog_mask("DEBUG")
2967+
private$.identifier <- identifier
2968+
self$set_syslog_levels(syslog_levels)
29512969
},
29522970

2971+
29532972
append = function(event){
2973+
identifier <- get(".identifier", private)
2974+
if (is.null(identifier)) identifier <- event$logger
2975+
2976+
rsyslog::open_syslog(identifier = identifier)
2977+
rsyslog::set_syslog_mask("DEBUG")
2978+
on.exit(rsyslog::close_syslog())
2979+
29542980
rsyslog::syslog(
29552981
private$.layout$format_event(event),
2956-
level = private$to_syslog_level(event$level)
2982+
level = private$to_syslog_levels(event$level)
29572983
)
2984+
},
2985+
2986+
2987+
set_syslog_levels = function(x){
2988+
if (is.function(x)){
2989+
private$.syslog_levels <- x
2990+
} else {
2991+
assert(all_are_distinct(unname(x)))
2992+
assert(is_equal_length(x, names(x)))
2993+
private$.syslog_levels <- structure(
2994+
standardize_log_levels(unname(x)),
2995+
names = names(x)
2996+
)
2997+
}
2998+
2999+
self
3000+
},
3001+
3002+
set_identifier = function(x){
3003+
if (!is.null(x)){
3004+
assert(is_scalar_character(x))
3005+
private$.identifier <- x
3006+
}
3007+
3008+
self
29583009
}
29593010
),
29603011

29613012
# +- active ---------------------------------------------------------------
29623013
active = list(
2963-
destination = function() sprintf("syslog [%s]", private$.identifier)
3014+
destination = function() sprintf("syslog [%s]", private$.identifier),
3015+
identifier = function() get("identifier", private),
3016+
syslog_levels = function() get("syslog_levels", private)
29643017
),
29653018

29663019
private = list(
29673020
finalize = function(){
29683021
rsyslog::close_syslog()
29693022
},
29703023

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-
)
3024+
to_syslog_levels = function(
3025+
levels
3026+
){
3027+
sl <- get(".syslog_levels", private)
3028+
levels <- standardize_log_levels(levels)
3029+
3030+
if (is.function(sl)){
3031+
res <- sl(levels)
3032+
} else {
3033+
res <- names(private$.syslog_levels)[match(levels, unname(private$.syslog_levels))]
3034+
}
3035+
3036+
toupper(res)
29793037
},
29803038

2981-
.identifier = ""
3039+
.identifier = NULL,
3040+
.syslog_levels = NULL
29823041
)
29833042
)
29843043

R/log_levels.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ error_msg_log_levels <- function(varname, log_levels){
294294
paste(sprintf("%s (%s)", names(log_levels), log_levels), collapse = ", ")
295295

296296
paste0(
297-
"'", varname, "' must either the numeric or character representation",
297+
"'", varname, "' must either the numeric or character representation ",
298298
"of one of the following log levels: ", ll_text
299299
)
300300
}

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ reference:
2727
- AppenderGmail
2828
- AppenderSendmail
2929
- AppenderPushbullet
30+
- AppenderSyslog
3031
- title: Layouts
3132
desc: Format LogEvents before output
3233
contents:

docs/404.html

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/LICENSE-text.html

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/LICENSE.html

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/index.html

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)