Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create periodically send text to a "log file" while printing normal output to console?

Tags:

r

I'm creating R code for a Monte Carlo simulation of a professional sport. Because the game dynamics are very complicated and to make the debugging process simpler, I'd like to have R send a line of text for every action that happens in the game to a "log file." The log file would be a comprehensive, play by play description of what's happening in the simulation, and would look something like this…

  • "GAME BEGINS"
  • POSSESSION ASSIGNED TO X TEAM
  • PLAYER Y GETS BALL
  • PLAYER Y SCORES
  • FOUL BY PLAYER Z OCCURS
  • SUBSTITUTION OCCURS (PLAYER W <-> PLAYER Q)
  • "GAME ENDS"

I can't just use the sink() function because while the simulation is running, I setup a progress bar (with the setTxtProgressBar function) and real time scores to be printed to the console. If I used sink(), I couldn't see any of the progress indicators or scores on the R console. Does this make sense? In other words I need to periodically send text to a log file in a cumulative fashion. Here is some example code to give you something to work with…

Thanks

for (i in 1:100)
{**SOMEHOW NEED TO PRINT LINE "START LOOP" TO LOG FILE**;
a <- rnorm(n = 100, mean = i, sd = 5);
print(mean(a)); #PRINT THIS MEAN TO THE CONSOLE
**SOMEHOW PRINT "LOOP 'i' COMPLETE" TO LOG FILE**}
like image 972
Slyron Avatar asked Apr 03 '14 16:04

Slyron


People also ask

How do I print a log message in Python?

Use logging Module to Print Log Message to a File in Python getLogger(name) method. There is a convention to use __name__ variable as the name of the logger. Once we have created a new logger, we should remember to log all our messages using the new logger.info() instead of the root's logging.info() method.


4 Answers

See ?cat. You can open a file connection to your log file and specify that in your cat call. When you don't specify a file name or connection it will print to the console.

As you say, don't use sink() as it will make the log file the default connection. Rather, open a named connection with file().

> log_con <- file("test.log")
> cat("write to log", file = log_con) # creates file and writes to it
> cat("write to console") # prints to console
write to console

The above results in a log file with the line "write to log" and "write to console" printed on the console.

If you need to append to your log file, set append = TRUE and use the file name instead of the file() connection.

> cat("add to log", file = "test.log", append = TRUE)
like image 68
Gregor Thomas Avatar answered Oct 03 '22 23:10

Gregor Thomas


To open the log file in "append" mode:

log_con <- file("test.log",open="a")
like image 24
BigFinger Avatar answered Oct 03 '22 22:10

BigFinger


Figured it out, thanks to shujaa and BigFinger. To summarize, here is how you would do it with my example code:

log_con <- file("/filepath/log.txt", open="a")

for (i in 1:100)
{
cat("loop begins", file = log_con, sep="\n")
a <- rnorm(n = 100, mean = i, sd = 5)
print(mean(a))
cat("single loop completed", file = log_con, sep="\n")
}

close(log_con)
like image 32
Slyron Avatar answered Oct 03 '22 23:10

Slyron


The library log4r seems to be more complete than an homemade one: https://github.com/johnmyleswhite/log4r

like image 22
Jérôme B Avatar answered Oct 03 '22 21:10

Jérôme B