Marco Fumagalli
Marco Fumagalli

Reputation: 2477

Logging name of the script - futile.logger R

I'm using package futile.logger to have log message written in a file log.txt. Since my projects is made up of 6 scripts and 20-22 functions I'd like to have a message logged with name of the scripts and name of the function. I created a Logging function that should be called to log the message:

Logging<-function(level,message)
{
  #' Logs event to a Log/log.txt files 
  #' 
  #' @param level: String, Message level: info,error,debug (NO CAPS LOCK)
  #' @param message: String, Message you want to Log
  #'
  #' @return  write the file with the message specified by the user.

  flog.appender(appender.file(file.path(getwd(),'Log/logs.txt')))
  eval(parse(text=sprintf("flog.%s('%s')",level,message)))
}

Let's suppose a file named foo.R with a function fun_1:

    fun_1 <- function(input){
       #doing some stuff
       Logging("info","some log message")
                            }

Which logs something like:

INFO [date hours] some log message

I 'd like to have a message that that contain also name of the scripts from which Logging function was called and name of the function where the Logging function was called i.e

 INFO foo fun_1 [date hours] some log message

Thanks

Upvotes: 4

Views: 625

Answers (1)

Kots
Kots

Reputation: 510

This is a bit late, but now futile.logger provides such a functionality.

Namely, you can try

fun_1 <- function() {

   # doing some stuff
   flog.info("some log message")}

then in your main file

require(futile.logger)
# create output layout based on documentation and your needs
layout <- layout.format('[~l] [~f] [~t] ~m')
# set the created output
flog.layout(layout)

flog.threshold(DEBUG)
fun_1()
flog.appender(appender.file("log_example.log")))

in log_example.log which is stored in the project's wd you can check your output

Upvotes: 1

Related Questions