|
| 1 | +module Connectors |
| 2 | + |
| 3 | +using ..LogCompose |
| 4 | +import ..LogCompose: logcompose, log_min_level |
| 5 | + |
| 6 | +using Logging, LogRoller, SyslogLogging, LoggingExtras, Sockets |
| 7 | + |
| 8 | +#-------------------------------------------------------------------- |
| 9 | +# The connectors for individual logger implementations should ideally |
| 10 | +# be in their own packages. |
| 11 | +#-------------------------------------------------------------------- |
| 12 | + |
| 13 | +function logcompose(::Type{Logging.SimpleLogger}, config::Dict{String,Any}, logger_config::Dict{String,Any}) |
| 14 | + level = log_min_level(logger_config, "Info") |
| 15 | + |
| 16 | + streamname = strip(get(logger_config, "stream", "stdout")) |
| 17 | + @assert !isempty(streamname) |
| 18 | + |
| 19 | + stream = streamname == "stdout" ? stdout : |
| 20 | + streamname == "stderr" ? stderr : |
| 21 | + open(streamname, "w+") |
| 22 | + |
| 23 | + Logging.SimpleLogger(stream, level) |
| 24 | +end |
| 25 | + |
| 26 | +const sysloglck = ReentrantLock() |
| 27 | +function logcompose(::Type{SyslogLogging.SyslogLogger}, config::Dict{String,Any}, logger_config::Dict{String,Any}) |
| 28 | + ident = logger_config["identity"] |
| 29 | + level = log_min_level(logger_config, "Info") |
| 30 | + |
| 31 | + kwargs = Dict{Symbol,Any}() |
| 32 | + |
| 33 | + kwargs[:facility] = Symbol(get(logger_config, "facility", "user")) |
| 34 | + |
| 35 | + if get(logger_config, "lock", false) |
| 36 | + kwargs[:lck] = sysloglck |
| 37 | + end |
| 38 | + |
| 39 | + server_type = get(logger_config, "server_type", "local") |
| 40 | + if (server_type == "tcp") || (server_type == "udp") |
| 41 | + kwargs[:tcp] = (server_type == "tcp") |
| 42 | + if haskey(logger_config, "server_host") |
| 43 | + kwargs[:host] = Sockets.IPv4(logger_config["server_host"]) |
| 44 | + end |
| 45 | + if haskey(logger_config, "server_port") |
| 46 | + kwargs[:port] = parse(Int, logger_config["server_port"]) |
| 47 | + end |
| 48 | + end |
| 49 | + SyslogLogging.SyslogLogger(ident, level; kwargs...) |
| 50 | +end |
| 51 | + |
| 52 | +function logcompose(::Type{LogRoller.RollingLogger}, config::Dict{String,Any}, logger_config::Dict{String,Any}) |
| 53 | + filename = String(strip(logger_config["filename"])) |
| 54 | + @assert !isempty(filename) |
| 55 | + |
| 56 | + level = log_min_level(logger_config, "Info") |
| 57 | + sizelimit = parse(Int, get(logger_config, "sizelimit", "10240000")) |
| 58 | + nfiles = parse(Int, get(logger_config, "nfiles", "5")) |
| 59 | + timestamp_identifier = Symbol(get(logger_config, "timestamp_identifier", "time")) |
| 60 | + |
| 61 | + LogRoller.RollingLogger(filename, sizelimit, nfiles, level; timestamp_identifier=timestamp_identifier) |
| 62 | +end |
| 63 | + |
| 64 | +function logcompose(::Type{LoggingExtras.TeeLogger}, config::Dict{String,Any}, logger_config::Dict{String,Any}) |
| 65 | + destinations = [LogCompose.logger(config, dest) for dest in logger_config["destinations"]] |
| 66 | + LoggingExtras.TeeLogger(destinations...) |
| 67 | +end |
| 68 | + |
| 69 | +end # module Connectors |
0 commit comments