Learn R Programming

MazamaCoreUtils (version 0.6.2)

logger.setup: Set up Python-style logging

Description

Configure level-specific log files using the package logging API.

Usage

logger.setup(
  traceLog = NULL,
  debugLog = NULL,
  infoLog = NULL,
  warnLog = NULL,
  errorLog = NULL,
  fatalLog = NULL
)

Value

No return value. Called for side effects.

Arguments

traceLog

File path receiving TRACE messages.

debugLog

File path receiving DEBUG messages.

infoLog

File path receiving INFO messages.

warnLog

File path receiving WARN messages.

errorLog

File path receiving ERROR messages.

fatalLog

File path receiving FATAL messages.

Details

Logging is built on top of the logger package while retaining the historical MazamaCoreUtils logging interface.

Separate log files can be created for different log levels so that, for example, an errorLog contains only ERROR and FATAL messages while a debugLog contains DEBUG messages as well as all higher-severity messages.

Any log file argument left as NULL is disabled and no file will be created for that level.

After initialization, logging statements can be generated with: logger.trace(), logger.debug(), logger.info(), logger.warn(), logger.error(), and logger.fatal().

Log messages are formatted with:


LEVEL [YYYY-MM-DD HH:MM:SS UTC] message

Console logging is enabled by default only for FATAL messages. Use logger.setLevel() to display additional log messages in the console.

See Also

logger.trace(), logger.debug(), logger.info(), logger.warn(), logger.error(), logger.fatal()

Examples

Run this code
if (FALSE) {
# Create three log files
logger.setup(
  debugLog = "debug.log",
  infoLog = "info.log",
  errorLog = "error.log"
)

# Generate log messages
logger.trace("trace statement #%d", 1)
logger.debug("debug statement")
logger.info("info statement %s %s", "with", "arguments")
logger.warn("warn statement: %s", "about to try something risky")

result <- try(1 / "a", silent = TRUE)
logger.error("error message: %s", geterrmessage())
logger.fatal("fatal statement: %s", "THE END")

cat(readLines("debug.log"), sep = "\n")
cat(readLines("info.log"), sep = "\n")
cat(readLines("error.log"), sep = "\n")
}

Run the code above in your browser using DataLab