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:
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
:
logger.INFO
[{level}] {format(time, \"%Y-%m-%d %H:%M:%OS4\")} pid:{pid} token:[{token}] {ans} {msg}
stdout
For a more comprehensive understanding of various logging levels and
layout interpretation, you can explore the logger
documentation.
While utilizing register_logger()
, the default logging
level for the new logger is logger::INFO
. You can modify
this behavior in four distinct ways:
logger
interface.library(teal.logger)
register_logger("my_namespace")
logger::log_threshold(logger::TRACE, namespace = "my_namespace")
Customizing teal
’s logging level:
register_logger()
.Customizing teal
’s logging level:
Customizing teal
’s logging level:
option
.Customizing teal
’s logging level:
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:
# 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 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.
For a more comprehensive understanding, consult the logger
documentation and the documentation for
register_logger()
.
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:
logger
interface.library(teal.logger)
register_logger("my_namespace")
logger::log_layout("{level} {msg}", namespace = "my_namespace")
Customizing teal
’s logging layout:
register_logger()
.Customizing teal
’s logging layout:
Customizing teal
’s logging layout:
option
.Customizing teal
’s logging layout:
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:
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 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.
For additional insights, consult the logger
documentation and the documentation for
register_logger()
.
By default, teal.logger
sends logs to
stdout
. If you wish to alter this behavior, you need to
employ logger
’s
API.
For instance, to redirect teal
’s logs to
stderr
using logger
’s API:
teal.X
PackagesAdditional teal.X
packages, such as
teal.data
, employ 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.
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
.
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