-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathREADME.Rmd
136 lines (100 loc) · 3.89 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file. -->
```{r setup, include = FALSE, warning = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(log4r)
```
# log4r
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/log4r)](https://cran.r-project.org/package=log4r)
[![R-CMD-check](https://github.com/johnmyleswhite/log4r/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/johnmyleswhite/log4r/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
**log4r** is a fast, lightweight, object-oriented approach to logging in R based
on the widely-emulated [Apache Log4j](https://logging.apache.org/log4j/) project.
**log4r** differs from other R logging packages in its focus on performance and
simplicity. As such, it has fewer features -- although it is still quite
extensible, as seen below -- but is much faster. See
`vignette("performance", package = "log4r")` for details.
Unlike other R logging packages, **log4r** also has first-class support for
structured logging. See `vignette("structured-logging", package = "log4r")` for
details.
## Installation
The package is available from CRAN:
```r
install.packages("log4r")
```
If you want to use the development version, you can install the package from
GitHub as follows:
```r
# install.packages("remotes")
remotes::install_github("johnmyleswhite/log4r")
```
## Usage
Logging is configured by passing around `logger` objects created by `logger()`.
By default, this will log to the console and suppress messages below the
`"INFO"` level:
```{r basic-example}
logger <- logger()
log_info(logger, "Located nearest gas station.")
log_warn(logger, "Ez-Gas sensor network is not available.")
log_debug(logger, "Debug messages are suppressed by default.")
```
Logging destinations are controlled by **Appenders**, a few of which are
provided by the package. For instance, if we want to debug-level messages to a
file:
```{r file-example, echo = 1:7}
log_file <- tempfile()
logger <- logger("DEBUG", appenders = file_appender(log_file))
log_info(logger, "Messages are now written to the file instead.")
log_debug(logger, "Debug messages are now visible.")
readLines(log_file)
unlink(log_file)
```
The `appenders` parameter takes a list, so you can log to multiple destinations
transparently.
For local development or simple batch R scripts run manually, writing log
messages to a file for later inspection is convenient. However, for deployed R
applications or automated scripts it is more likely you will need to send logs
to a central location; see
`vignette("logging-beyond-local-files", package = "log4r")`.
To control the format of the messages you can change the **Layout** used by
each appender. Layouts are functions; you can write your own quite easily:
```{r layout-example}
my_layout <- function(level, ...) {
paste0(format(Sys.time()), " [", level, "] ", ..., collapse = "")
}
logger <- logger(appenders = console_appender(my_layout))
log_info(logger, "Messages should now look a little different.")
```
With an appropriate layout, you can also use *structured logging*, enriching log
messages with contextual fields:
```{r sl-example}
logger <- logger(appenders = console_appender(logfmt_log_layout()))
log_info(
logger, message = "processed entries", file = "catpics_01.csv",
entries = 4124, elapsed = 2.311
)
```
## Older APIs
The 0.2 API is still supported, but will issue deprecation warnings when used:
```{r old-api, echo = 1:12}
logger <- create.logger()
logfile(logger) <- log_file
level(logger) <- "INFO"
debug(logger, 'A Debugging Message')
info(logger, 'An Info Message')
warn(logger, 'A Warning Message')
error(logger, 'An Error Message')
fatal(logger, 'A Fatal Error Message')
readLines(log_file)
unlink(log_file)
```
## License
The package is available under the terms of the Artistic License 2.0.