Debian Codegraph @

inst/doc/r_packages.html

raw
1 <!DOCTYPE html>
2
3 <html>
4
5 <head>
6
7 <meta charset="utf-8" />
8 <meta name="generator" content="pandoc" />
9 <meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
10
11 <meta name="viewport" content="width=device-width, initial-scale=1" />
12
13
14
15 <title>Logging from R Packages</title>
16
17 <script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
18 // be compatible with the behavior of Pandoc < 2.8).
19 document.addEventListener('DOMContentLoaded', function(e) {
20 var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
21 var i, h, a;
22 for (i = 0; i < hs.length; i++) {
23 h = hs[i];
24 if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
25 a = h.attributes;
26 while (a.length > 0) h.removeAttribute(a[0].name);
27 }
28 });
29 </script>
30
31 <style type="text/css">
32 code{white-space: pre-wrap;}
33 span.smallcaps{font-variant: small-caps;}
34 span.underline{text-decoration: underline;}
35 div.column{display: inline-block; vertical-align: top; width: 50%;}
36 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
37 ul.task-list{list-style: none;}
38 </style>
39
40
41
42 <style type="text/css">
43 code {
44 white-space: pre;
45 }
46 .sourceCode {
47 overflow: visible;
48 }
49 </style>
50 <style type="text/css" data-origin="pandoc">
51 pre > code.sourceCode { white-space: pre; position: relative; }
52 pre > code.sourceCode > span { line-height: 1.25; }
53 pre > code.sourceCode > span:empty { height: 1.2em; }
54 .sourceCode { overflow: visible; }
55 code.sourceCode > span { color: inherit; text-decoration: inherit; }
56 div.sourceCode { margin: 1em 0; }
57 pre.sourceCode { margin: 0; }
58 @media screen {
59 div.sourceCode { overflow: auto; }
60 }
61 @media print {
62 pre > code.sourceCode { white-space: pre-wrap; }
63 pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
64 }
65 pre.numberSource code
66 { counter-reset: source-line 0; }
67 pre.numberSource code > span
68 { position: relative; left: -4em; counter-increment: source-line; }
69 pre.numberSource code > span > a:first-child::before
70 { content: counter(source-line);
71 position: relative; left: -1em; text-align: right; vertical-align: baseline;
72 border: none; display: inline-block;
73 -webkit-touch-callout: none; -webkit-user-select: none;
74 -khtml-user-select: none; -moz-user-select: none;
75 -ms-user-select: none; user-select: none;
76 padding: 0 4px; width: 4em;
77 color: #aaaaaa;
78 }
79 pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
80 div.sourceCode
81 { }
82 @media screen {
83 pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
84 }
85 code span.al { color: #ff0000; font-weight: bold; }
86 code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
87 code span.at { color: #7d9029; }
88 code span.bn { color: #40a070; }
89 code span.bu { color: #008000; }
90 code span.cf { color: #007020; font-weight: bold; }
91 code span.ch { color: #4070a0; }
92 code span.cn { color: #880000; }
93 code span.co { color: #60a0b0; font-style: italic; }
94 code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
95 code span.do { color: #ba2121; font-style: italic; }
96 code span.dt { color: #902000; }
97 code span.dv { color: #40a070; }
98 code span.er { color: #ff0000; font-weight: bold; }
99 code span.ex { }
100 code span.fl { color: #40a070; }
101 code span.fu { color: #06287e; }
102 code span.im { color: #008000; font-weight: bold; }
103 code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
104 code span.kw { color: #007020; font-weight: bold; }
105 code span.op { color: #666666; }
106 code span.ot { color: #007020; }
107 code span.pp { color: #bc7a00; }
108 code span.sc { color: #4070a0; }
109 code span.ss { color: #bb6688; }
110 code span.st { color: #4070a0; }
111 code span.va { color: #19177c; }
112 code span.vs { color: #4070a0; }
113 code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
114 </style>
115 <script>
116 // apply pandoc div.sourceCode style to pre.sourceCode instead
117 (function() {
118 var sheets = document.styleSheets;
119 for (var i = 0; i < sheets.length; i++) {
120 if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
121 try { var rules = sheets[i].cssRules; } catch (e) { continue; }
122 var j = 0;
123 while (j < rules.length) {
124 var rule = rules[j];
125 // check if there is a div.sourceCode rule
126 if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
127 j++;
128 continue;
129 }
130 var style = rule.style.cssText;
131 // check if color or background-color is set
132 if (rule.style.color === '' && rule.style.backgroundColor === '') {
133 j++;
134 continue;
135 }
136 // replace div.sourceCode by a pre.sourceCode rule
137 sheets[i].deleteRule(j);
138 sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
139 }
140 }
141 })();
142 </script>
143
144
145
146
147 <style type="text/css">body {
148 background-color: #fff;
149 margin: 1em auto;
150 max-width: 700px;
151 overflow: visible;
152 padding-left: 2em;
153 padding-right: 2em;
154 font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
155 font-size: 14px;
156 line-height: 1.35;
157 }
158 #TOC {
159 clear: both;
160 margin: 0 0 10px 10px;
161 padding: 4px;
162 width: 400px;
163 border: 1px solid #CCCCCC;
164 border-radius: 5px;
165 background-color: #f6f6f6;
166 font-size: 13px;
167 line-height: 1.3;
168 }
169 #TOC .toctitle {
170 font-weight: bold;
171 font-size: 15px;
172 margin-left: 5px;
173 }
174 #TOC ul {
175 padding-left: 40px;
176 margin-left: -1.5em;
177 margin-top: 5px;
178 margin-bottom: 5px;
179 }
180 #TOC ul ul {
181 margin-left: -2em;
182 }
183 #TOC li {
184 line-height: 16px;
185 }
186 table {
187 margin: 1em auto;
188 border-width: 1px;
189 border-color: #DDDDDD;
190 border-style: outset;
191 border-collapse: collapse;
192 }
193 table th {
194 border-width: 2px;
195 padding: 5px;
196 border-style: inset;
197 }
198 table td {
199 border-width: 1px;
200 border-style: inset;
201 line-height: 18px;
202 padding: 5px 5px;
203 }
204 table, table th, table td {
205 border-left-style: none;
206 border-right-style: none;
207 }
208 table thead, table tr.even {
209 background-color: #f7f7f7;
210 }
211 p {
212 margin: 0.5em 0;
213 }
214 blockquote {
215 background-color: #f6f6f6;
216 padding: 0.25em 0.75em;
217 }
218 hr {
219 border-style: solid;
220 border: none;
221 border-top: 1px solid #777;
222 margin: 28px 0;
223 }
224 dl {
225 margin-left: 0;
226 }
227 dl dd {
228 margin-bottom: 13px;
229 margin-left: 13px;
230 }
231 dl dt {
232 font-weight: bold;
233 }
234 ul {
235 margin-top: 0;
236 }
237 ul li {
238 list-style: circle outside;
239 }
240 ul ul {
241 margin-bottom: 0;
242 }
243 pre, code {
244 background-color: #f7f7f7;
245 border-radius: 3px;
246 color: #333;
247 white-space: pre-wrap;
248 }
249 pre {
250 border-radius: 3px;
251 margin: 5px 0px 10px 0px;
252 padding: 10px;
253 }
254 pre:not([class]) {
255 background-color: #f7f7f7;
256 }
257 code {
258 font-family: Consolas, Monaco, 'Courier New', monospace;
259 font-size: 85%;
260 }
261 p > code, li > code {
262 padding: 2px 0px;
263 }
264 div.figure {
265 text-align: center;
266 }
267 img {
268 background-color: #FFFFFF;
269 padding: 2px;
270 border: 1px solid #DDDDDD;
271 border-radius: 3px;
272 border: 1px solid #CCCCCC;
273 margin: 0 5px;
274 }
275 h1 {
276 margin-top: 0;
277 font-size: 35px;
278 line-height: 40px;
279 }
280 h2 {
281 border-bottom: 4px solid #f7f7f7;
282 padding-top: 10px;
283 padding-bottom: 2px;
284 font-size: 145%;
285 }
286 h3 {
287 border-bottom: 2px solid #f7f7f7;
288 padding-top: 10px;
289 font-size: 120%;
290 }
291 h4 {
292 border-bottom: 1px solid #f7f7f7;
293 margin-left: 8px;
294 font-size: 105%;
295 }
296 h5, h6 {
297 border-bottom: 1px solid #ccc;
298 font-size: 105%;
299 }
300 a {
301 color: #0033dd;
302 text-decoration: none;
303 }
304 a:hover {
305 color: #6666ff; }
306 a:visited {
307 color: #800080; }
308 a:visited:hover {
309 color: #BB00BB; }
310 a[href^="http:"] {
311 text-decoration: underline; }
312 a[href^="https:"] {
313 text-decoration: underline; }
314
315 code > span.kw { color: #555; font-weight: bold; }
316 code > span.dt { color: #902000; }
317 code > span.dv { color: #40a070; }
318 code > span.bn { color: #d14; }
319 code > span.fl { color: #d14; }
320 code > span.ch { color: #d14; }
321 code > span.st { color: #d14; }
322 code > span.co { color: #888888; font-style: italic; }
323 code > span.ot { color: #007020; }
324 code > span.al { color: #ff0000; font-weight: bold; }
325 code > span.fu { color: #900; font-weight: bold; }
326 code > span.er { color: #a61717; background-color: #e3d2d2; }
327 </style>
328
329
330
331
332 </head>
333
334 <body>
335
336
337
338
339 <h1 class="title toc-ignore">Logging from R Packages</h1>
340
341
342
343 <p>In this vignette I suppose that you are already familiar with <a href="https://daroczig.github.io/logger/articles/customize_logger.html">Customizing
344 the format and destination of log records</a> vignette, especially with
345 the <a href="https://daroczig.github.io/logger/articles/customize_logger.html#log-namespaces">Log
346 namespaces</a> section.</p>
347 <p>So that your R package’s users can suppress (or render with custom
348 layout) the log messages triggered by your R package, it’s wise to
349 record all those log messages in a custom namespace. By default, if you
350 are calling the <code>?log_level</code> function family from an R
351 package after importing from the <code>logger</code> package, then
352 <code>logger</code> will try to auto-guess the calling R package name
353 and use that as the default namespace, see eg:</p>
354 <div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">library</span>(logger)</span>
355 <span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a>devtools<span class="sc">::</span><span class="fu">load_all</span>(<span class="fu">system.file</span>(<span class="st">&quot;demo-packages/logger-tester-package&quot;</span>, <span class="at">package =</span> <span class="st">&quot;logger&quot;</span>))</span>
356 <span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="co">#&gt; ℹ Loading logger.tester</span></span>
357 <span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a><span class="fu">logger_tester_function</span>(INFO, <span class="st">&quot;hi from tester package&quot;</span>)</span></code></pre></div>
358 <p>But if auto-guessing is not your style, then feel free to set your
359 custom namespace (eg the name of your package) in all
360 <code>?log_info</code> etc function calls and let your users know about
361 how to suppress / reformat / redirect your log messages via
362 <code>?log_threshold</code>, <code>?log_layout</code>,
363 <code>?log_appender</code>.</p>
364 <p>Please note that setting the formatter function via
365 <code>?log_formatter</code> should not be left to the R package
366 end-users, as the log message formatter is specific to your logging
367 calls, so that should be decided by the R package author. Feel free to
368 pick any formatter function (eg <code>glue</code>, <code>sprintf</code>,
369 <code>paste</code> or something else), and set that via
370 <code>?log_formatter</code> when your R package is loaded. All other
371 parameters of your <code>logger</code> will inherit from the
372 <code>global</code> namespace – set by your R package’s end user.</p>
373
374
375
376 <!-- code folding -->
377
378
379 <!-- dynamically load mathjax for compatibility with self-contained -->
380 <script>
381 (function () {
382 var script = document.createElement("script");
383 script.type = "text/javascript";
384 script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
385 document.getElementsByTagName("head")[0].appendChild(script);
386 })();
387 </script>
388
389 </body>
390 </html>