daroczig / logger

A lightweight, modern and flexible, log4j and futile.logger inspired logging utility for R
https://daroczig.github.io/logger
280 stars 41 forks source link

Feature request: Log Customization for appender_file and appender_console #85

Closed camult closed 3 years ago

camult commented 3 years ago

I wonder if it is possible to print different layouts one for the console and another one with more details to a file.

For example:

logLayoutFile <- "{node} \t {user} \t {pid} \t {time} \t {fn} \t {level} \t {msg}"
logLayoutConsole <- "{fn} {level} {msg}"

logger <- layout_glue_generator(format_console = logLayoutConsole , format_file = logLayoutFile )
log_layout(logger)

Thank you.

daroczig commented 3 years ago

Sure, I think you are interested in the index parameter of the related function for stacking logger configs: https://daroczig.github.io/logger/articles/customize_logger.html#stacking-loggers-1

camult commented 3 years ago

It seems that the index is for the level, can it be used for the layout?

library("logger")
logLayoutFile <- layout_glue_generator(format = "{node} \t {user} \t {pid} \t {time} \t {fn} \t {level} \t {msg}")
logLayoutConsole <- layout_glue_generator(format = "{fn} {level} {msg}")
log_layout(logLayoutFile)
logfile <- "log.txt"
log_appender(appender_tee(file = logfile))
log_info('This is a log info message!')
log_warn('This is a log warning message!')
log_error('This is a log error message!')

The expected result sent to the console should be just:

function.name    INFO    This is a log info message!
function.name    WARN    This is a log warning message!
function.name    ERROR   This is a log error message!

While the file log.txt should have:

Linuxsys     username    10000   2021-05-17 11:07:56     function.name   INFO    This is a log info message!
Linuxsys     username    10000   2021-05-17 11:07:58     function.name   WARN    This is a log warning message!
Linuxsys     username    10000   2021-05-17 11:07:59     function.name   ERROR   This is a log error message!

Can you provide an example where the log in the console has a different layout from the log saved to a file?