Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic question re logging to file #14

Open
JockLawrie opened this issue Apr 8, 2018 · 3 comments
Open

Basic question re logging to file #14

JockLawrie opened this issue Apr 8, 2018 · 3 comments

Comments

@JockLawrie
Copy link

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
Copy link
Author

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)

@jballanc
Copy link

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.

@JockLawrie
Copy link
Author

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

Thanks again,
Jock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants