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

add RemoteLogger for distributed logging #48121

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/Distributed/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name = "Distributed"
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[deps]
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
Expand Down
1 change: 1 addition & 0 deletions stdlib/Distributed/src/Distributed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ hash(r::RRID, h::UInt) = hash(r.whence, hash(r.id, h))
==(r::RRID, s::RRID) = (r.whence==s.whence && r.id==s.id)

include("clusterserialize.jl")
include("logger.jl")
include("cluster.jl") # cluster setup and management, addprocs
include("messages.jl")
include("process_messages.jl") # process incoming messages
Expand Down
1 change: 1 addition & 0 deletions stdlib/Distributed/src/cluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ function start_worker(out::IO, cookie::AbstractString=readline(stdin); close_std
# To prevent hanging processes on remote machines, newly launched workers exit if the
# master process does not connect in time.
check_master_connect()
Logging.global_logger(RemoteLogger(1, Logging.Info))
while true; wait(); end
catch err
print(stderr, "unhandled exception on $(myid()): $(err)\nexiting.\n")
Expand Down
23 changes: 23 additions & 0 deletions stdlib/Distributed/src/logger.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Logging

struct RemoteLogger <: Logging.AbstractLogger
pid::Int
min_level::Logging.LogLevel
end
function RemoteLogger(pid=1)
RemoteLogger(pid, Logging.Info)
end

Logging.min_enabled_level(logger::RemoteLogger) = logger.min_level
Logging.shouldlog(logger::RemoteLogger, level, _module, group, id) = true

# TODO: probably should live in base/logging.jl?
function logmsg(level::Logging.LogLevel, message, _module, _group, _id, _file, _line; kwargs...)
Logging.@logmsg level message _module=_module _group=_group _id=_id _file=_file _line=_line kwargs...
end

function Logging.handle_message(logger::RemoteLogger, level::Logging.LogLevel, message, _module, _group, _id,
_file, _line; kwargs...)
@nospecialize
remote_do(logmsg, logger.pid, level, message, _module, _group, _id, _file, _line; pid=myid(), kwargs...)
end
14 changes: 13 additions & 1 deletion stdlib/Distributed/test/distributed_exec.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

using Test, Distributed, Random, Serialization, Sockets
using Test, Distributed, Random, Serialization, Sockets, Logging
import Distributed: launch, manage

@test cluster_cookie() isa String
Expand Down Expand Up @@ -1915,6 +1915,18 @@ begin
end
end

# test logging
w = only(addprocs(1))
@test_logs (:info, "from pid $w") begin
prev_logger = global_logger(current_logger())
try
wait(@spawnat w @info("from pid $(myid())"))
finally
global_logger(prev_logger)
end
end
wait(rmprocs([w]))

# Run topology tests last after removing all workers, since a given
# cluster at any time only supports a single topology.
rmprocs(workers())
Expand Down