flog.logger: Manage loggers

Description Arguments Usage Additional Usage Details Author(s) See Also Examples

Description

Provides functions for writing log messages and managing loggers. Typically only the flog.[trace|debug|info|warn|error|fatal] functions need to be used in conjunction with flog.threshold to interactively change the log level.

Arguments

msg

The message to log

name

The logger name to use

capture

Capture print output of variables instead of interpolate

logger

The logger to use. If NULL (the default), it is looked up based on name. Provide logger explicitely if the speed of the evaluation of log level is of concern (e.g., a flog.trace call in your function which has to be run many times).

...

Optional arguments to populate the format string

expr

An expression to evaluate

finally

An optional expression to evaluate at the end

Usage

# Conditionally print a log statement at TRACE log level
flog.trace(msg, ..., name=flog.namespace(), logger=NULL, capture=FALSE)

# Conditionally print a log statement at DEBUG log level
flog.debug(msg, ..., name=flog.namespace(), logger=NULL, capture=FALSE)

# Conditionally print a log statement at INFO log level
flog.info(msg, ..., name=flog.namespace(), logger=NULL, capture=FALSE)

# Conditionally print a log statement at WARN log level
flog.warn(msg, ..., name=flog.namespace(), logger=NULL, capture=FALSE)

# Conditionally print a log statement at ERROR log level
flog.error(msg, ..., name=flog.namespace(), logger=NULL, capture=FALSE)

# Print a log statement at FATAL log level
flog.fatal(msg, ..., name=flog.namespace(), logger=NULL, capture=FALSE)

# Execute an expression and capture any warnings or errors ftry(expr, error=stop, silent=FALSE, finally=NULL, details=”)

Additional Usage

These functions generally do not need to be called by an end user.

# Get the ROOT logger
flog.logger()

# Get the logger with the specified name
flog.logger(name)

# Set options for the given logger
flog.logger(name, threshold=NULL, appender=NULL, layout=NULL, carp=NULL)

Details

These functions represent the high level interface to futile.logger.

The primary use case for futile.logger is to write out log messages. There are log writers associated with all the predefined log levels: TRACE, DEBUG, INFO, WARN, ERROR, FATAL. Log messages will only be written if the log level is equal to or more urgent than the current threshold. By default the ROOT logger is set to INFO.

> flog.debug("This won't print")
> flog.info("But this %s", 'will')
> flog.warn("As will %s", 'this')

Typically, the built in log level constants are used in the call, which conform to the log4j levels (from least severe to most severe): TRACE, DEBUG, INFO, WARN, ERROR, FATAL. It is not a strict requirement to use these constants (any numeric value will work), though most users should find this level of granularity sufficient.

Loggers are hierarchical in the sense that any requested logger that is undefined will fall back to its most immediate defined parent logger. The absolute parent is ROOT, which is guaranteed to be defined for the system and cannot be deleted. This means that you can specify a new logger directly.

> flog.info("This will fall back to 'my', then 'ROOT'", name='my.logger')

You can also change the threshold or any other setting associated with a logger. This will create an explicit logger where any unspecified options are copied from the parent logger.

> flog.appender(appender.file("foo.log"), name='my')
> flog.threshold(ERROR, name='my.logger')
> flog.info("This won't print", name='my.logger')
> flog.error("This

If you have a function which gets called many times, it is a good strategy to pass the logger directly instead of its name.

Instead of this: > simulation_fun <- function(i) > flog.trace("We are in loop > i >

... you can do this:: > my_logger <- flog.logger("my.logger") > simulation_fun2 <- function(i) > flog.trace("We are in loop > i >

> system.time(for (i in 1:1000) simulation_fun(i)) > system.time(for (i in 1:1000) simulation_fun2(i))

If you define a logger that you later want to remove, use flog.remove.

The option 'capture' allows you to print out more complicated data structures without a lot of ceremony. This variant doesn't accept format strings and instead appends the value to the next line of output. Consider

> m <- matrix(rnorm(12), nrow=3)
> flog.info("Matrix:",m, capture=TRUE)

which preserves the formatting, whereas using capture=FALSE will have a cluttered output due to recycling.

Author(s)

Brian Lee Yung Rowe

See Also

flog.threshold flog.remove flog.carp flog.appender flog.layout

Examples

 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
flog.threshold(DEBUG)
flog.debug("This debug message will print")

flog.threshold(WARN)
flog.debug("This one won't")

m <- matrix(rnorm(12), nrow=3)
flog.info("Matrix:",m, capture=TRUE)

ftry(log(-1))

## Not run: 
s <- c('FCX','AAPL','JPM','AMZN')
p <- TawnyPortfolio(s)

flog.threshold(TRACE,'tawny')
ws <- optimizePortfolio(p, RandomMatrixDenoiser())
z <- getIndexComposition()

flog.threshold(WARN,'tawny')
ws <- optimizePortfolio(p, RandomMatrixDenoiser())
z <- getIndexComposition()

## End(Not run)

## Not run: 
flog.appender(appender.modulo(1000), name='counter')
lapply(1:10000, function(i) flog.info("value is %s",i, name='counter'))

## End(Not run)

zatonovo/futile.logger documentation built on Feb. 23, 2022, 10:18 p.m.