Skip to content

Commit cc8083a

Browse files
committed
Merge branch 'master' of https://github.com/s-fleck/lgr
2 parents 49fa553 + 71f4286 commit cc8083a

33 files changed

+286
-451
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ matrix:
1919
- r: 3.2
2020
r_github_packages:
2121
- s-fleck/lgrExtra
22+
- s-fleck/rotor
2223
sudo: false
2324
after_success:
2425
- Rscript -e 'devtools::install();devtools::test()'

DESCRIPTION

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ Maintainer: Stefan Fleck <[email protected]>
1212
Description: A flexible, feature-rich yet light-weight logging
1313
framework based on 'R6' classes. It supports hierarchical loggers,
1414
custom log levels, arbitrary data fields in log events, logging to
15-
plaintext, 'JSON', (rotating) files, memory buffers. For extra appenders
16-
that support logging to databases, email and push notifications
17-
see the the package lgr.app.
15+
plaintext, 'JSON', (rotating) files, memory buffers. For extra
16+
appenders that support logging to databases, email and push
17+
notifications see the the package lgr.app.
1818
License: MIT + file LICENSE
1919
URL: https://s-fleck.github.io/lgr
2020
BugReports: https://github.com/s-fleck/lgr/issues
@@ -34,9 +34,8 @@ Suggests:
3434
jsonlite,
3535
knitr,
3636
rmarkdown,
37-
rotor (>= 0.2.2),
37+
rotor (>= 0.2.4.9002),
3838
rprojroot,
39-
rsyslog,
4039
testthat,
4140
tibble,
4241
tools,
@@ -48,7 +47,7 @@ VignetteBuilder:
4847
Encoding: UTF-8
4948
LazyData: true
5049
Roxygen: list(markdown = TRUE)
51-
RoxygenNote: 7.1.0
50+
RoxygenNote: 7.1.1.9000
5251
Collate:
5352
'Filterable.R'
5453
'utils-sfmisc.R'
@@ -73,4 +72,3 @@ Collate:
7372
'utils-logging.R'
7473
'utils-rd.R'
7574
'utils-rotor.R'
76-
'utils-tests.R'

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export(AppenderFileRotatingDate)
2828
export(AppenderFileRotatingTime)
2929
export(AppenderJson)
3030
export(AppenderMemory)
31-
export(AppenderSyslog)
3231
export(AppenderTable)
3332
export(CannotInitializeAbstractClassError)
3433
export(EventFilter)

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
* AppenderMemory/AppenderBuffer: `flush_threshold` is now independent of
4040
`should_flush` function. `default_should_flush()` is no longer necessary
4141
and has been removed.
42+
43+
* Updated AppenderFileRotating to use new **rotor** 2.5.0
44+
45+
* Most errors now have appropriate subclasses
46+
47+
* Logger$log() dispatches to all appenders, even if some throw an error
4248

4349

4450
# lgr 0.3.4

R/Appender.R

Lines changed: 41 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ----------------------------------------------------------

R/LogEvent.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ format_custom_fields <- function(
451451
){
452452
if (!length(x)) return("")
453453

454-
max_len <- max(256 / length(x) - sum(nchar(names(x))), 16)
454+
max_len <- max(512 / length(x) - sum(nchar(names(x))), 16)
455455

456456
braces <- c("{", "}")
457457
brackets <- c("[", "]")

0 commit comments

Comments
 (0)