File: appender_file.Rd

package info (click to toggle)
r-cran-logger 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,324 kB
  • sloc: sh: 13; makefile: 2
file content (88 lines) | stat: -rw-r--r-- 2,561 bytes parent folder | download
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
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/appenders.R
\name{appender_file}
\alias{appender_file}
\title{Append log messages to a file}
\usage{
appender_file(
  file,
  append = TRUE,
  max_lines = Inf,
  max_bytes = Inf,
  max_files = 1L
)
}
\arguments{
\item{file}{path}

\item{append}{boolean passed to \code{cat} defining if the file should
be overwritten with the most recent log message instead of
appending}

\item{max_lines}{numeric specifying the maximum number of lines
allowed in a file before rotating}

\item{max_bytes}{numeric specifying the maximum number of bytes
allowed in a file before rotating}

\item{max_files}{integer specifying the maximum number of files to
be used in rotation}
}
\value{
function taking \code{lines} argument
}
\description{
Log messages are written to a file with basic log rotation: when
max number of lines or bytes is defined to be other than \code{Inf},
then the log file is renamed with a \code{.1} suffix and a new log file
is created. The renaming happens recursively (eg \code{logfile.1}
renamed to \code{logfile.2}) until the specified \code{max_files}, then the
oldest file (\code{logfile.{max_files-1}}) is deleted.
}
\examples{
\dontshow{old <- logger:::namespaces_set()}
## ##########################################################################
## simple example logging to a file
t <- tempfile()
log_appender(appender_file(t))
for (i in 1:25) log_info(i)
readLines(t)

## ##########################################################################
## more complex example of logging to file
## rotated after every 3rd line up to max 5 files

## create a folder storing the log files
t <- tempfile()
dir.create(t)
f <- file.path(t, "log")

## define the file logger with log rotation enabled
log_appender(appender_file(f, max_lines = 3, max_files = 5L))

## enable internal logging to see what's actually happening in the logrotate steps
log_threshold(TRACE, namespace = ".logger")
## log 25 messages
for (i in 1:25) log_info(i)

## see what was logged
lapply(list.files(t, full.names = TRUE), function(t) {
  cat("\n##", t, "\n")
  cat(readLines(t), sep = "\n")
})

\dontshow{logger:::namespaces_set(old)}
}
\seealso{
Other log_appenders: 
\code{\link{appender_async}()},
\code{\link{appender_console}()},
\code{\link{appender_kinesis}()},
\code{\link{appender_pushbullet}()},
\code{\link{appender_slack}()},
\code{\link{appender_stdout}()},
\code{\link{appender_syslog}()},
\code{\link{appender_tee}()},
\code{\link{appender_telegram}()}
}
\concept{log_appenders}