Basic question re logging to file
Hi there,
I'm using Julia 0.6 and I'm starting a new project. Since MicroLogging forms the basis of Base logging for Julia 0.7 and beyond, I figure it's best to get familiar with this now.
As a first step I am trying to log to a file. I can't seem to get the following example working. I don't get any errors, but I also don't get any logs in the logfile. Any ideas?
using MicroLogging
logfile = "logtest.log"
logger = SimpleLogger(open(logfile, "a"))
configure_logging(logger)
@info "msg 1"
@warn "msg 2"
@error "msg 3"
Bump.
I've made some progress, namely I can log to a file but I can only see the log entries when the file handle is closed. How can I view the log entries as they arrive (via tail -f logtest.log)?
using MicroLogging
s = open("logtest.log", "a+")
global_logger(SimpleLogger(s))
@info "msg 1"
close(s)
Julia is using buffered IO for the logfile. When the IO buffer fills or (as you've discovered) the stream is closed, you'll see a write. If you want to see a write before then, you can use flush:
using MicroLogging
s = open("logtest.log", "a+")
global_logger(SimpleLogger(s))
@info "msg 1"
flush(s)
There is also a way in C to set a file to line buffering mode (setlinebuf) so that it will flush after every newline, but I haven't found any equivalent in Julia.
Ah, thanks. I''m working around it by logging to STDOUT and then redirecting STDOUT to a file.
Thanks again, Jock