-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
teal-logger.Rmd
198 lines (168 loc) · 7.83 KB
/
teal-logger.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
---
title: "Getting started"
author: "NEST CoreDev Team"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Getting started}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
## Introduction
The `teal.logger` package utilizes the `logger` framework to record events within a package. `logger` is an R package that supports logging namespaces, hierarchical logging, various log destinations, vectorization, and more. For further details about `logger`, you can refer to:
* [GitHub](https://github.com/daroczig/logger)
* [CRAN](https://CRAN.R-project.org/package=logger)
## Default Logging Setup of `teal.logger`
`teal.logger` employs the `register_logger()` function to create a new logger in a user-defined namespace. `teal.logger` comes with a predefined pair of layout and logging level. Other packages in the `teal` framework utilize `teal.logger` to log crucial events, such as setting up `teal` data, alterations in the filter panel, records of merging data slices inside modules, and so on.
Defaults of `teal.logger`:
* Logging level: `logger.INFO`
* Logging layout: `[{level}] {format(time, \"%Y-%m-%d %H:%M:%OS4\")} pid:{pid} token:[{token}] {ans} {msg}`
* Logs emitted to `stdout`
For a more comprehensive understanding of various logging levels and layout interpretation, you can explore the [`logger` documentation](https://daroczig.github.io/logger/articles/customize_logger.html).
## Customizing the Log Level
While utilizing `register_logger()`, the default logging level for the new logger is `logger::INFO`. You can modify this behavior in four distinct ways:
1. Using the `logger` interface.
```r
library(teal.logger)
register_logger("my_namespace")
logger::log_threshold(logger::TRACE, namespace = "my_namespace")
```
Customizing `teal`'s logging level:
```r
library(teal.logger)
logger::log_threshold(logger::TRACE, namespace = "teal")
```
2. Utilizing the function argument of `register_logger()`.
```r
library(teal.logger)
register_logger("my_namespace", level = logger::TRACE)
```
Customizing `teal`'s logging level:
```r
library(teal.logger)
register_logger(namespace = "teal", level = logger::TRACE)
```
3. Setting an environment variable.
```r
library(teal.logger)
Sys.setenv(TEAL.LOG_LEVEL = "TRACE")
register_logger("my_namespace")
```
Customizing `teal`'s logging level:
```r
Sys.setenv(TEAL.LOG_LEVEL = "TRACE")
library(teal.logger)
```
4. Setting an `option`.
```r
library(teal.logger)
options(teal.log_level = logger::TRACE)
register_logger("my_namespace")
```
Customizing `teal`'s logging level:
```r
options(teal.log_level = logger::TRACE)
library(teal.logger)
```
Do keep in mind that packages in the `teal` framework register their loggers upon library loading. Therefore, if you intend to alter the default logging level via an option or a system environment variable, ensure you modify the variable before loading the library.
Example below:
```r
# library(teal)
# Sys.setenv(TEAL.LOG_LEVEL = "TRACE") # won't change the default
```
```r
# Sys.setenv(TEAL.LOG_LEVEL = "TRACE")
# library(teal)
# will change the default because teal is attached after changing the variable
```
Alternatively, you can change the options after loading a package and then call `teal.logger::register_logger()` with the appropriate namespace name to change the default of a single package. E.g. this will change the defaults of
`teal.logger`. Replace `teal.logger` with the name of the package you want to change.
```r
library(teal.logger)
Sys.setenv(TEAL.LOG_LEVEL = "TRACE")
register_logger('teal.logger')
```
For a more comprehensive understanding, consult the [`logger` documentation](https://daroczig.github.io/logger/articles/customize_logger.html) and the documentation for `teal.logger::register_logger()`.
## Customizing the Log Layout
When using `register_logger()`, the default log layout for the new logger is `[{level}] {format(time, \"%Y-%m-%d %H:%M:%OS4\")} pid:{pid} token:[{token}] {ans} {msg}`.You can modify this behavior in four distinct ways:
1. Using the `logger` interface.
```r
library(teal.logger)
register_logger("my_namespace")
logger::log_layout("{level} {msg}", namespace = "my_namespace")
```
Customizing `teal`'s logging layout:
```r
library(teal.logger)
logger::log_layout("{level} {msg}", namespace = "teal")
```
2. Utilizing the function argument of `register_logger()`.
```r
library(teal.logger)
register_logger("my_namespace", layout = "{level} {msg}")
```
Customizing `teal`'s logging layout:
```r
library(teal.logger)
register_logger(namespace = "teal", layout = "{level} {msg}")
```
3. Setting an environment variable.
```r
library(teal.logger)
Sys.setenv(TEAL.LOG_LAYOUT = "{level} {msg}")
register_logger("my_namespace")
```
Customizing `teal`'s logging layout:
```r
Sys.setenv(TEAL.LOG_LAYOUT = "{level} {msg}")
library(teal.logger)
```
4. Setting an `option`.
```r
library(teal.logger)
options(teal.log_layout = "{level} {msg}")
register_logger("my_namespace")
```
Customizing `teal`'s logging layout:
```r
options(teal.log_layout = "{level} {msg}")
library(teal.logger)
```
Like changing the log level, remember that `teal` registers its logger during library loading. Thus, if you aim to modify the default logging layout via an option or a system environment variable, ensure you adjust the variable prior to loading the library.
Example below:
```r
library(teal.logger)
Sys.setenv(TEAL.LOG_LAYOUT = "{level} {msg}") # won't change the default
```
```r
Sys.setenv(TEAL.LOG_LAYOUT = "{level} {msg}")
library(teal.logger)
# will change the default because teal is attached after changing the variable
```
Alternatively, you can change the options after loading a package and then call `teal.logger::register_logger()` with the appropriate namespace name to change the default of a single package. E.g. this will change the defaults of `teal.logger`. Replace `teal.logger` with the name of the package you want to change.
```r
library(teal.logger)
Sys.setenv(TEAL.LOG_LAYOUT = "{level} {msg}")
register_logger('teal.logger')
```
For additional insights, consult the [`logger` documentation](https://daroczig.github.io/logger/articles/customize_logger.html) and the documentation for `teal.logger::register_logger()`.
## Customizing the Log Destination
By default, `teal.logger` sends logs to `stdout`. If you wish to alter this behavior, you need to employ [`logger`'s API](https://daroczig.github.io/logger/articles/customize_logger.html#delivering-log-records-to-their-destination-1).
For instance, to redirect `teal`'s logs to `stderr` using `logger`'s API:
```r
library(teal.logger)
logger::log_appender(logger::appender_stderr, namespace = "teal")
```
## Logging in Other `teal.X` Packages
Additional `teal.X` packages, such as `teal.data`, employ `teal.logger::register_logger()` to enroll loggers in their namespace. Each package establishes a logger in the namespace equivalent to the package name, e.g., `teal.data` creates a logger in the `teal.data` namespace. A package initializes its logger during package loading and utilizes default logging settings, subject to modifications explained in the preceding sections.
## Example of Usage
Below is a minimal working example that demonstrates logging using `teal`'s logging setup. The default `TEAL.LOG.LEVEL` is `"INFO"`, implying that logs with a level above `level = 400` won't appear in `stdout`.
```r
library(teal.logger)
register_logger(namespace = "my_module")
logger::log_error("This is an ERROR level log from my module", namespace = "my_module") # 200
logger::log_warn("This is a WARN level log from my module", namespace = "my_module") # 300
logger::log_success("This is a SUCCESS level log from my module", namespace = "my_module") # 350
logger::log_info("This is an INFO level log from my module", namespace = "my_module") # 400
logger::log_info("This is a DEBUG level log from my module", namespace = "my_module") # 500
logger::log_trace("This is a TRACE level log from my module", namespace = "my_module") # 600
```