JuliaLogging / LoggingExtras.jl

Composable Loggers for the Julia Logging StdLib

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot write log messages from remote processes to log files.

zekeriyasari opened this issue · comments

I cannot write the log messages from remote processes to a log file. When I execute the following test script testscript.jl whose content is given below,

using Distributed 
using Logging, LoggingExtras

addprocs()

@everywhere function foo()
    @warn("It is bad")
    @info("normal stuff")
    @error("THE WORSE THING")
    @debug("it is chill")
end

function setlogger()
    TeeLogger(
        MinLevelLogger(FileLogger("/tmpinfo.log"), Logging.Info),
        MinLevelLogger(FileLogger("/tmp/warn.log"), Logging.Warn),
    ) |> global_logger
end 

setlogger()
for w in workers() 
    remote_do(foo, w)
end
foo()

the only log messages shown below are written to /tmp/info.log, which I think come from the last call to foo.

┌ Warning: It is bad
└ @ Main /tmp/deleteme.jl:10
┌ Info: normal stuff
└ @ Main /tmp/deleteme.jl:11
┌ Error: THE WORSE THING
└ @ Main /tmp/deleteme.jl:12

However all the log messages from all the remote processes are printed on console.

┌ Warning: It is bad
└ @ Main /tmp/deleteme.jl:10
[ Info: normal stuff
┌ Error: THE WORSE THING
└ @ Main /tmp/deleteme.jl:12
┌ Warning: It is bad
└ @ Main /tmp/deleteme.jl:10
[ Info: normal stuff
┌ Warning: It is bad
└ @ Main /tmp/deleteme.jl:10
[ Info: normal stuff
┌ Warning: It is bad
└ @ Main /tmp/deleteme.jl:10
[ Info: normal stuff
┌ Error: THE WORSE THING
└ @ Main /tmp/deleteme.jl:12
┌ Error: THE WORSE THING
└ @ Main /tmp/deleteme.jl:12
┌ Warning: It is bad
└ @ Main /tmp/deleteme.jl:10
┌ Error: THE WORSE THING
└ @ Main /tmp/deleteme.jl:12
┌ Warning: It is bad
└ @ Main /tmp/deleteme.jl:10
[ Info: normal stuff
┌ Warning: It is bad
└ @ Main /tmp/deleteme.jl:10
[ Info: normal stuff
┌ Error: THE WORSE THING
└ @ Main /tmp/deleteme.jl:12
┌ Error: THE WORSE THING
└ @ Main /tmp/deleteme.jl:12
┌ Warning: It is bad
└ @ Main /tmp/deleteme.jl:10
[ Info: normal stuff
┌ Error: THE WORSE THING
└ @ Main /tmp/deleteme.jl:12

I could not see what I am missing. Is there a way to write all the log messages from all the remote workers to log files?

You are only setting the logger on the main process, not the workers.
So they are still logging to the console logger which prints to stdout.
And julia redirects stdout form workers to print to stdout on the main process.

I think that a simple fix would be to add:

@everywhere using Logging, LoggingExtras
#....
@everywhere setlogger()

Though you might actually want to have a different file for each worker, to make it less confusing.

@everywhere function setlogger()
    TeeLogger(
        MinLevelLogger(FileLogger("/tmpinfo_$(my_id()).log"), Logging.Info),
        MinLevelLogger(FileLogger("/tmp/warn_$(my_id()).log"), Logging.Warn),
    ) |> global_logger
end 

Using a separate file for each worker process looks more appropriate. Thank you for your reply.