Debian Codegraph @

man/LayoutJson.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/LayoutJson.R
3 \name{LayoutJson}
4 \alias{LayoutJson}
5 \title{Format LogEvents as JSON}
6 \description{
7 A format for formatting LogEvents as
8 \href{https://jsonlines.org/}{jsonlines} log files. This provides a
9 nice balance between human- an machine-readability.
10 }
11 \examples{
12 # setup a dummy LogEvent
13 event <- LogEvent$new(
14 logger = Logger$new("dummy logger"),
15 level = 200,
16 timestamp = Sys.time(),
17 caller = NA_character_,
18 msg = "a test message",
19 custom_field = "LayoutJson can handle arbitrary fields"
20 )
21
22 lo <- LayoutJson$new()
23 lo$format_event(event)
24
25 lo <- LayoutJson$new(
26 transform_event_names = toupper,
27 excluded_fields = c("RAWMSG", "CALLER"))
28
29 lo$format_event(event)
30
31 lo <- LayoutJson$new(
32 transform_event = function(e) {
33 values <- e$values
34 values$msg <- toupper(values$msg)
35 values
36 },
37 timestamp_fmt = "\%a \%b \%d \%H:\%M:\%S \%Y",
38 excluded_fields = c("RAWMSG", "CALLER"))
39
40 lo$format_event(event)
41 }
42 \seealso{
43 \code{\link[=read_json_lines]{read_json_lines()}}, \url{https://jsonlines.org/}
44
45 Other Layouts:
46 \code{\link{Layout}},
47 \code{\link{LayoutFormat}},
48 \code{\link{LayoutGlue}}
49 }
50 \concept{Layouts}
51 \section{Super class}{
52 \code{\link[lgr:Layout]{lgr::Layout}} -> \code{LayoutJson}
53 }
54 \section{Active bindings}{
55 \if{html}{\out{<div class="r6-active-bindings">}}
56 \describe{
57 \item{\code{toJSON_args}}{a \code{list}}
58
59 \item{\code{timestamp_fmt}}{a \code{character} scalar or a \code{function} that accepts a \code{POSIXct}
60 as its single argument}
61
62 \item{\code{transform_event}}{a \code{function} that accepts a \code{LogEvent} as its single argument}
63
64 \item{\code{transform_event_names}}{a named \code{character} vector or a function that accepts a
65 \code{character} vector of field names as its single argument.}
66 }
67 \if{html}{\out{</div>}}
68 }
69 \section{Methods}{
70 \subsection{Public methods}{
71 \itemize{
72 \item \href{#method-LayoutJson-new}{\code{LayoutJson$new()}}
73 \item \href{#method-LayoutJson-format_event}{\code{LayoutJson$format_event()}}
74 \item \href{#method-LayoutJson-set_toJSON_args}{\code{LayoutJson$set_toJSON_args()}}
75 \item \href{#method-LayoutJson-set_timestamp_fmt}{\code{LayoutJson$set_timestamp_fmt()}}
76 \item \href{#method-LayoutJson-set_transform_event}{\code{LayoutJson$set_transform_event()}}
77 \item \href{#method-LayoutJson-set_transform_event_names}{\code{LayoutJson$set_transform_event_names()}}
78 \item \href{#method-LayoutJson-toString}{\code{LayoutJson$toString()}}
79 \item \href{#method-LayoutJson-parse}{\code{LayoutJson$parse()}}
80 \item \href{#method-LayoutJson-read}{\code{LayoutJson$read()}}
81 \item \href{#method-LayoutJson-clone}{\code{LayoutJson$clone()}}
82 }
83 }
84 \if{html}{\out{
85 <details open><summary>Inherited methods</summary>
86 <ul>
87 <li><span class="pkg-link" data-pkg="lgr" data-topic="Layout" data-id="set_excluded_fields"><a href='../../lgr/html/Layout.html#method-Layout-set_excluded_fields'><code>lgr::Layout$set_excluded_fields()</code></a></span></li>
88 </ul>
89 </details>
90 }}
91 \if{html}{\out{<hr>}}
92 \if{html}{\out{<a id="method-LayoutJson-new"></a>}}
93 \if{latex}{\out{\hypertarget{method-LayoutJson-new}{}}}
94 \subsection{Method \code{new()}}{
95 Creates a new instance of this \link[R6:R6Class]{R6} class.
96 \subsection{Usage}{
97 \if{html}{\out{<div class="r">}}\preformatted{LayoutJson$new(
98 toJSON_args = list(auto_unbox = TRUE),
99 timestamp_fmt = NULL,
100 transform_event = function(event) event[["values"]],
101 transform_event_names = NULL,
102 excluded_fields = "rawMsg"
103 )}\if{html}{\out{</div>}}
104 }
105
106 \subsection{Arguments}{
107 \if{html}{\out{<div class="arguments">}}
108 \describe{
109 \item{\code{toJSON_args}}{a list of arguments passed to \code{\link[jsonlite:fromJSON]{jsonlite::toJSON()}},}
110
111 \item{\code{timestamp_fmt}}{Format to be applied to the timestamp. This is
112 applied after \code{transform_event} but \verb{before transform_event_names}
113 \itemize{
114 \item \code{NULL}: formatting of the timestamp is left to \code{\link[jsonlite:fromJSON]{jsonlite::toJSON()}},
115 \item a \code{character} scalar as for \code{\link[=format.POSIXct]{format.POSIXct()}}, or
116 \item a \code{function} that returns a vector of the same length as its
117 (\link{POSIXct}) input. The returned vector can be of any type
118 supported by \code{\link[jsonlite:fromJSON]{jsonlite::toJSON()}}.
119 }}
120
121 \item{\code{transform_event}}{a \code{function} with a single argument that
122 takes a \link{LogEvent} object and returns a \code{list} of values.}
123
124 \item{\code{transform_event_names}}{\itemize{
125 \item \code{NULL}: don't process names
126 \item a named \code{character} vector of the format \code{new_name = old_name}
127 \item or a \code{function} with a single mandatory argument that accepts a
128 \code{character} vector of field names. Applied after \code{transform_event}.
129 }}
130
131 \item{\code{excluded_fields}}{A \code{character} vector of field names to exclude
132 from the final output. Applied after \code{transform_event_names}.}
133 }
134 \if{html}{\out{</div>}}
135 }
136 }
137 \if{html}{\out{<hr>}}
138 \if{html}{\out{<a id="method-LayoutJson-format_event"></a>}}
139 \if{latex}{\out{\hypertarget{method-LayoutJson-format_event}{}}}
140 \subsection{Method \code{format_event()}}{
141 \subsection{Usage}{
142 \if{html}{\out{<div class="r">}}\preformatted{LayoutJson$format_event(event)}\if{html}{\out{</div>}}
143 }
144
145 }
146 \if{html}{\out{<hr>}}
147 \if{html}{\out{<a id="method-LayoutJson-set_toJSON_args"></a>}}
148 \if{latex}{\out{\hypertarget{method-LayoutJson-set_toJSON_args}{}}}
149 \subsection{Method \code{set_toJSON_args()}}{
150 \subsection{Usage}{
151 \if{html}{\out{<div class="r">}}\preformatted{LayoutJson$set_toJSON_args(x)}\if{html}{\out{</div>}}
152 }
153
154 \subsection{Arguments}{
155 \if{html}{\out{<div class="arguments">}}
156 \describe{
157 \item{\code{x}}{a \code{list}}
158 }
159 \if{html}{\out{</div>}}
160 }
161 }
162 \if{html}{\out{<hr>}}
163 \if{html}{\out{<a id="method-LayoutJson-set_timestamp_fmt"></a>}}
164 \if{latex}{\out{\hypertarget{method-LayoutJson-set_timestamp_fmt}{}}}
165 \subsection{Method \code{set_timestamp_fmt()}}{
166 \subsection{Usage}{
167 \if{html}{\out{<div class="r">}}\preformatted{LayoutJson$set_timestamp_fmt(x)}\if{html}{\out{</div>}}
168 }
169
170 \subsection{Arguments}{
171 \if{html}{\out{<div class="arguments">}}
172 \describe{
173 \item{\code{x}}{a \code{character} scalar or a \code{function} that accepts a \code{POSIXct}
174 as its single argument}
175 }
176 \if{html}{\out{</div>}}
177 }
178 }
179 \if{html}{\out{<hr>}}
180 \if{html}{\out{<a id="method-LayoutJson-set_transform_event"></a>}}
181 \if{latex}{\out{\hypertarget{method-LayoutJson-set_transform_event}{}}}
182 \subsection{Method \code{set_transform_event()}}{
183 \subsection{Usage}{
184 \if{html}{\out{<div class="r">}}\preformatted{LayoutJson$set_transform_event(x)}\if{html}{\out{</div>}}
185 }
186
187 \subsection{Arguments}{
188 \if{html}{\out{<div class="arguments">}}
189 \describe{
190 \item{\code{x}}{a \code{function} that accepts a \code{LogEvent} as its single argument}
191 }
192 \if{html}{\out{</div>}}
193 }
194 }
195 \if{html}{\out{<hr>}}
196 \if{html}{\out{<a id="method-LayoutJson-set_transform_event_names"></a>}}
197 \if{latex}{\out{\hypertarget{method-LayoutJson-set_transform_event_names}{}}}
198 \subsection{Method \code{set_transform_event_names()}}{
199 \subsection{Usage}{
200 \if{html}{\out{<div class="r">}}\preformatted{LayoutJson$set_transform_event_names(x)}\if{html}{\out{</div>}}
201 }
202
203 \subsection{Arguments}{
204 \if{html}{\out{<div class="arguments">}}
205 \describe{
206 \item{\code{x}}{a named \code{character} vector or a function that accepts a
207 \code{character} vector of field names as its single argument.}
208 }
209 \if{html}{\out{</div>}}
210 }
211 }
212 \if{html}{\out{<hr>}}
213 \if{html}{\out{<a id="method-LayoutJson-toString"></a>}}
214 \if{latex}{\out{\hypertarget{method-LayoutJson-toString}{}}}
215 \subsection{Method \code{toString()}}{
216 Represent the \code{LayoutJson} class as a string
217 \subsection{Usage}{
218 \if{html}{\out{<div class="r">}}\preformatted{LayoutJson$toString()}\if{html}{\out{</div>}}
219 }
220
221 }
222 \if{html}{\out{<hr>}}
223 \if{html}{\out{<a id="method-LayoutJson-parse"></a>}}
224 \if{latex}{\out{\hypertarget{method-LayoutJson-parse}{}}}
225 \subsection{Method \code{parse()}}{
226 Read and parse file written using this Layout
227
228 This can be used by the \verb{$data} active binding of an \link{Appender}
229 \subsection{Usage}{
230 \if{html}{\out{<div class="r">}}\preformatted{LayoutJson$parse(file)}\if{html}{\out{</div>}}
231 }
232
233 \subsection{Arguments}{
234 \if{html}{\out{<div class="arguments">}}
235 \describe{
236 \item{\code{file}}{\code{character} scalar: path to a file}
237 }
238 \if{html}{\out{</div>}}
239 }
240 }
241 \if{html}{\out{<hr>}}
242 \if{html}{\out{<a id="method-LayoutJson-read"></a>}}
243 \if{latex}{\out{\hypertarget{method-LayoutJson-read}{}}}
244 \subsection{Method \code{read()}}{
245 Read a file written using this Layout (without parsing)
246
247 This can be used by the \verb{$show()} method of an \link{Appender}
248 \subsection{Usage}{
249 \if{html}{\out{<div class="r">}}\preformatted{LayoutJson$read(file, threshold = NA_integer_, n = 20L)}\if{html}{\out{</div>}}
250 }
251
252 \subsection{Arguments}{
253 \if{html}{\out{<div class="arguments">}}
254 \describe{
255 \item{\code{file}}{\code{character} scalar: path to a file}
256
257 \item{\code{threshold}}{\code{character} Minimum log level to show. Requires parsing
258 of the log file (but will still display unparsed output)}
259
260 \item{\code{n}}{\code{integer} number of lines to show}
261 }
262 \if{html}{\out{</div>}}
263 }
264 }
265 \if{html}{\out{<hr>}}
266 \if{html}{\out{<a id="method-LayoutJson-clone"></a>}}
267 \if{latex}{\out{\hypertarget{method-LayoutJson-clone}{}}}
268 \subsection{Method \code{clone()}}{
269 The objects of this class are cloneable with this method.
270 \subsection{Usage}{
271 \if{html}{\out{<div class="r">}}\preformatted{LayoutJson$clone(deep = FALSE)}\if{html}{\out{</div>}}
272 }
273
274 \subsection{Arguments}{
275 \if{html}{\out{<div class="arguments">}}
276 \describe{
277 \item{\code{deep}}{Whether to make a deep clone.}
278 }
279 \if{html}{\out{</div>}}
280 }
281 }
282 }