@@ -649,10 +649,15 @@ AppenderMemory <- R6::R6Class(
649649
650650 # ' @field buffer_events A `list` of [LogEvents]. Contents of the buffer.
651651 buffer_events = function () {
652- ord <- get(" event_order" , envir = private )
653- ord <- ord - min(ord ) + 1L
654- ord <- order(ord )
655- res <- get(" .buffer_events" , envir = private )[ord ]
652+ res <- get(" .buffer_events" , envir = private )
653+
654+ if (length(res )){
655+ ord <- get(" event_order" , envir = private )
656+ ord <- ord - min(ord ) + 1L
657+ ord <- order(ord )
658+ res <- res [ord ]
659+ }
660+
656661 as_event_list(
657662 res [! vapply(res , is.null , FALSE )]
658663 )
@@ -711,6 +716,7 @@ AppenderMemory <- R6::R6Class(
711716
712717# ' Log to a memory buffer
713718# '
719+ # ' @description
714720# ' An Appender that Buffers LogEvents in-memory and and redirects them to other
715721# ' Appenders once certain conditions are met.
716722# '
@@ -727,21 +733,6 @@ AppenderMemory <- R6::R6Class(
727733# ' \R). Setting this to off can have slightly negative performance impacts.}
728734# ' }
729735# '
730- # ' @section Methods:
731- # '
732- # ' \describe{
733- # ' \item{`flush()`}{Manually trigger flushing}
734- # '
735- # ' \item{`add_appender(appender, name = NULL)`, `remove_appender(pos)`}{
736- # ' Add or remove an [Appender]. Supplying a `name` is optional but
737- # ' recommended. After adding an Appender with
738- # ' `appender$add_appender(AppenderConsole$new(), name = "console")` you can
739- # ' refer to it via `appender$appenders$console`. `remove_appender()` can
740- # ' remove an Appender by position or name.
741- # ' }
742- # '
743- # ' }
744- # '
745736# ' @export
746737# ' @seealso [LayoutFormat]
747738# ' @family Appenders
@@ -806,6 +797,15 @@ AppenderBuffer <- R6::R6Class(
806797 invisible (self )
807798 },
808799
800+
801+ # ' @description Exactly like A [Logger], an [AppenderBuffer] can have an
802+ # ' arbitrary amount of Appenders attached. When the buffer is flushed, the
803+ # ' buffered events are dispatched to these Appenders.
804+ # '
805+ # ' @param x single [Appender] or a `list` thereof. Appenders control the
806+ # ' output of a Logger. Be aware that a Logger also inherits the Appenders
807+ # ' of its ancestors (see `vignette("lgr", package = "lgr")` for more info
808+ # ' about Logger inheritance).
809809 set_appenders = function (x ){
810810 if (is.null(x )){
811811 private $ .appenders <- list ()
@@ -828,6 +828,15 @@ AppenderBuffer <- R6::R6Class(
828828 },
829829
830830
831+ # ' @description Add an Appender to the AppenderBuffer
832+ # ' @param appender a single [Appender]
833+ # ' @param name a `character` scalar. Optional but recommended.
834+ # '
835+ # ' @description Add or remove an [Appender]. Supplying a `name` is optional but
836+ # ' recommended. After adding an Appender with
837+ # ' `appender$add_appender(AppenderConsole$new(), name = "console")` you can
838+ # ' refer to it via `appender$appenders$console`. `remove_appender()` can
839+ # ' remove an Appender by position or name.
831840 add_appender = function (
832841 appender ,
833842 name = NULL
@@ -842,6 +851,10 @@ AppenderBuffer <- R6::R6Class(
842851 },
843852
844853
854+ # ' @description remove an appender
855+ # ' @param pos `integer` index or `character` name of the Appender(s) to
856+ # ' remove
857+ # '
845858 remove_appender = function (
846859 pos
847860 ){
@@ -971,10 +984,7 @@ AppenderFileRotating <- R6::R6Class(
971984
972985 if (! file.exists(file )) file.create(file )
973986
974- private $ bq <- rotor :: BackupQueueIndex $ new(
975- file ,
976- backup_dir = backup_dir
977- )
987+ private $ bq <- rotor :: BackupQueueIndex $ new(file )
978988
979989 self $ set_file(file )
980990 self $ set_threshold(threshold )
@@ -1005,7 +1015,7 @@ AppenderFileRotating <- R6::R6Class(
10051015 bq <- get(" bq" , private )
10061016
10071017 if (force || bq $ should_rotate(size = self $ size )){
1008- bq $ push_backup ()
1018+ bq $ push ()
10091019 bq $ prune()
10101020 file.remove(self $ file )
10111021 file.create(self $ file )
@@ -1025,7 +1035,7 @@ AppenderFileRotating <- R6::R6Class(
10251035 file
10261036 ){
10271037 super $ set_file(file )
1028- private $ bq $ set_file (self $ file )
1038+ private $ bq $ set_origin (self $ file )
10291039 self
10301040 },
10311041
@@ -1066,7 +1076,7 @@ AppenderFileRotating <- R6::R6Class(
10661076 set_backup_dir = function (
10671077 x
10681078 ){
1069- private $ bq $ set_backup_dir( x )
1079+ private $ bq $ set_dir( x , create = FALSE )
10701080 self
10711081 },
10721082
@@ -1112,10 +1122,10 @@ AppenderFileRotating <- R6::R6Class(
11121122
11131123 # ' @field backups A `data.frame` containing information on path, file size,
11141124 # ' etc... on the available backups of `file`.
1115- backups = function () get(" bq" , private )$ backups ,
1125+ backups = function () get(" bq" , private )$ files ,
11161126
11171127
1118- backup_dir = function () get(" bq" , private )$ backup_dir
1128+ backup_dir = function () get(" bq" , private )$ dir
11191129 ),
11201130
11211131
@@ -1157,10 +1167,7 @@ AppenderFileRotatingTime <- R6::R6Class(
11571167
11581168 if (! file.exists(file )) file.create(file )
11591169
1160- private $ bq <- rotor :: BackupQueueDateTime $ new(
1161- file ,
1162- backup_dir = backup_dir
1163- )
1170+ private $ bq <- rotor :: BackupQueueDateTime $ new(file )
11641171
11651172 self $ set_file(file )
11661173 self $ set_threshold(threshold )
@@ -1196,7 +1203,7 @@ AppenderFileRotatingTime <- R6::R6Class(
11961203 now = now
11971204 )
11981205 ){
1199- bq $ push_backup (
1206+ bq $ push (
12001207 overwrite = self $ overwrite ,
12011208 now = now
12021209 )
@@ -1324,10 +1331,7 @@ AppenderFileRotatingDate <- R6::R6Class(
13241331
13251332 if (! file.exists(file )) file.create(file )
13261333
1327- private $ bq <- rotor :: BackupQueueDate $ new(
1328- file ,
1329- backup_dir = backup_dir
1330- )
1334+ private $ bq <- rotor :: BackupQueueDate $ new(file )
13311335
13321336 self $ set_file(file )
13331337 self $ set_threshold(threshold )
@@ -1350,143 +1354,6 @@ AppenderFileRotatingDate <- R6::R6Class(
13501354)
13511355
13521356
1353- # AppenderSyslog ----------------------------------------------------------
1354-
1355- # ' Log to the POSIX System Log
1356- # '
1357- # ' @description
1358- # ' An Appender that writes to the syslog on supported POSIX platforms. Requires
1359- # ' the \pkg{rsyslog} package.
1360- # '
1361- # ' @seealso [LayoutFormat], [LayoutJson]
1362- # ' @family Appenders
1363- # ' @export
1364- # ' @examples
1365- # ' if (requireNamespace("rsyslog", quietly = TRUE)) {
1366- # ' lg <- get_logger("rsyslog/test")
1367- # ' lg$add_appender(AppenderSyslog$new(), "syslog")
1368- # ' lg$info("A test message")
1369- # '
1370- # ' if (Sys.info()[["sysname"]] == "Linux"){
1371- # ' system("journalctl -t 'rsyslog/test'")
1372- # ' }
1373- # '
1374- # ' invisible(lg$config(NULL)) # cleanup
1375- # ' }
1376- AppenderSyslog <- R6 :: R6Class(
1377- " AppenderSyslog" ,
1378- inherit = Appender ,
1379- cloneable = FALSE ,
1380- public = list (
1381- initialize = function (
1382- identifier = NULL ,
1383- threshold = NA_integer_ ,
1384- layout = LayoutFormat $ new(" %m" ),
1385- filters = NULL ,
1386- syslog_levels = c(
1387- " CRITICAL" = " fatal" ,
1388- " ERR" = " error" ,
1389- " WARNING" = " warn" ,
1390- " INFO" = " info" ,
1391- " DEBUG" = " debug" ,
1392- " DEBUG" = " trace"
1393- )
1394- ){
1395- if (! requireNamespace(" rsyslog" , quietly = TRUE )) {
1396- stop(" The 'rsyslog' package is required for this appender." )
1397- }
1398- self $ set_threshold(threshold )
1399- self $ set_layout(layout )
1400- self $ set_filters(filters )
1401- self $ set_identifier(identifier )
1402- self $ set_syslog_levels(syslog_levels )
1403- },
1404-
1405-
1406- append = function (event ){
1407- identifier <- get(" .identifier" , private )
1408- if (is.null(identifier )) identifier <- event $ logger
1409-
1410- rsyslog :: open_syslog(identifier = identifier )
1411- rsyslog :: set_syslog_mask(" DEBUG" )
1412- on.exit(rsyslog :: close_syslog())
1413-
1414- rsyslog :: syslog(
1415- private $ .layout $ format_event(event ),
1416- level = private $ to_syslog_levels(event $ level )
1417- )
1418- },
1419-
1420-
1421- # ' @description Define conversion between lgr and syslog log levels
1422- # ' @param x
1423- # ' * a named `character` vector mapping whose names are log
1424- # ' levels as understood by [rsyslog::syslog()] and whose values are [lgr
1425- # ' log levels][log_levels] (either `character` or `numeric`)
1426- # ' * a `function` that takes a vector of lgr log levels as input and
1427- # ' returns a `character` vector of log levels for [rsyslog::syslog()].
1428- set_syslog_levels = function (x ){
1429- if (is.function(x )){
1430- private $ .syslog_levels <- x
1431- } else {
1432- assert(all_are_distinct(unname(x )))
1433- assert(is_equal_length(x , names(x )))
1434- private $ .syslog_levels <- structure(
1435- standardize_log_levels(unname(x )),
1436- names = names(x )
1437- )
1438- }
1439-
1440- self
1441- },
1442-
1443- # ' @description Set a string to identify the process.
1444- set_identifier = function (x ){
1445- assert(is.null(x ) || is_scalar_character(x ))
1446- private $ .identifier <- x
1447- self
1448- }
1449- ),
1450-
1451- # +- active ---------------------------------------------------------------
1452- active = list (
1453- destination = function () sprintf(" syslog [%s]" , private $ .identifier ),
1454-
1455- # ' @field identifier `character` scalar. A string identifying the process;
1456- # ' if `NULL` defaults to the logger name
1457- identifier = function () get(" .identifier" , private ),
1458-
1459- # ' @field syslog_levels. Either a named `character` vector or a `function`
1460- # ' mapping lgr [log_levels] to rsyslog log levels. See
1461- # ' `$set_syslog_levels()`.
1462- syslog_levels = function () get(" .syslog_levels" , private )
1463- ),
1464-
1465- private = list (
1466- finalize = function (){
1467- rsyslog :: close_syslog()
1468- },
1469-
1470- to_syslog_levels = function (
1471- levels
1472- ){
1473- sl <- get(" .syslog_levels" , private )
1474- levels <- standardize_log_levels(levels )
1475-
1476- if (is.function(sl )){
1477- res <- sl(levels )
1478- } else {
1479- res <- names(private $ .syslog_levels )[match(levels , unname(private $ .syslog_levels ))]
1480- }
1481-
1482- toupper(res )
1483- },
1484-
1485- .identifier = NULL ,
1486- .syslog_levels = NULL
1487- )
1488- )
1489-
14901357
14911358
14921359# print.Appender ----------------------------------------------------------
0 commit comments