Logger doesn't respect bash output redirection

I have some code where most of the output is in @info statements using the Julia stdlib logger. I now have some big jobs which I want to run in the background and dump their logs somewhere. Normally I would do this with

julia bin.jl arg1 arg2 > outlog &

My problem is that when I do this the output from println and the like get properly redirected, but everything output by the logger still gets spit to stdout and, even worse, does not get spit to the log file.

I understand that it’s possible to use the logger to redirect to a log file as well, but the above procedure is super simple and works very nicely for 99% of use cases (were I only using println).

So, my question is, do I have no choice but to tell the logger to redirect it’s output? That would be a bit unfortunate as it would require me to have my program accept an additional command line argument rather than just having it use the existing functionality of bash.

I think you need to redirect stderr as well.

You can also use my package IOLogging.jl with the FileLogger, built precisely for this purpose. You can pretty much drop it in place and just use @info etc. normally.

julia bin.jl arg1 arg2 > outlog 2>&1 &


Now my question is: why does the logger output go to stderr (even for @info statements)? It wouldn’t have occurred to me that it might be doing that.

1 Like

That’s just the way it’s defined by default:

But you can give it a new IO such as stdout - you do have to set the logger using global_logger in that case though. This will also log everything that’s logged to that single IO.

I mean, why is that the default? I assume there’s some reason why this was done, but I’m curious as to what that is as everyone will probably expect it to output to stdout.

That I do not know - all I know is that the logging interface is only semi-stable:

so this can (and probably will) change a little bit in the future. Your best bet right now is to either redirect it yourself if you want to redirect everything to one file, ore use either IOLogging or LoggingExtras. Both allow you to redirect each level separately.

stdout is for output, stderr is for messaging.

1 Like