MicroLogging.jl icon indicating copy to clipboard operation
MicroLogging.jl copied to clipboard

Basic question re logging to file

Open JockLawrie opened this issue 7 years ago • 3 comments

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"

JockLawrie avatar Apr 08 '18 02:04 JockLawrie

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)

JockLawrie avatar Apr 17 '18 04:04 JockLawrie

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.

jballanc avatar Apr 24 '18 17:04 jballanc

Ah, thanks. I''m working around it by logging to STDOUT and then redirecting STDOUT to a file.

Thanks again, Jock

JockLawrie avatar May 06 '18 23:05 JockLawrie