Skip to content

Commit

Permalink
Use a logger with a namespace in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
spuun committed Sep 25, 2024
1 parent c843192 commit ff2d2fa
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/amqproxy.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ require "ini"
require "log"

class AMQProxy::CLI
Log = ::Log.for(self)

@listen_address = ENV["LISTEN_ADDRESS"]? || "localhost"
@listen_port = ENV["LISTEN_PORT"]? || 5673
@log_level : Log::Severity = Log::Severity::Info
@log_level : ::Log::Severity = ::Log::Severity::Info
@idle_connection_timeout : Int32 = ENV.fetch("IDLE_CONNECTION_TIMEOUT", "5").to_i
@term_timeout = -1
@term_client_close_timeout = 0
Expand All @@ -21,7 +23,7 @@ class AMQProxy::CLI
section.each do |key, value|
case key
when "upstream" then @upstream = value
when "log_level" then @log_level = Log::Severity.parse(value)
when "log_level" then @log_level = ::Log::Severity.parse(value)
when "idle_connection_timeout" then @idle_connection_timeout = value.to_i
when "term_timeout" then @term_timeout = value.to_i
when "term_client_close_timeout" then @term_client_close_timeout = value.to_i
Expand All @@ -33,7 +35,7 @@ class AMQProxy::CLI
case key
when "port" then @listen_port = value
when "bind", "address" then @listen_address = value
when "log_level" then @log_level = Log::Severity.parse(value)
when "log_level" then @log_level = ::Log::Severity.parse(value)
else raise "Unsupported config #{name}/#{key}"
end
end
Expand All @@ -60,7 +62,7 @@ class AMQProxy::CLI
parser.on("--term-client-close-timeout=SECONDS", "At TERM the server SECONDS seconds for clients to send Close beforing sending Close to clients (default: 0s)") do |v|
@term_client_close_timeout = v.to_i
end
parser.on("-d", "--debug", "Verbose logging") { @log_level = Log::Severity::Debug }
parser.on("-d", "--debug", "Verbose logging") { @log_level = ::Log::Severity::Debug }
parser.on("-c FILE", "--config=FILE", "Load config file") { |v| parse_config(v) }
parser.on("-h", "--help", "Show this help") { puts parser.to_s; exit 0 }
parser.on("-v", "--version", "Display version") { puts AMQProxy::VERSION.to_s; exit 0 }
Expand All @@ -82,11 +84,11 @@ class AMQProxy::CLI
tls = u.scheme == "amqps"

log_backend = if ENV.has_key?("JOURNAL_STREAM")
Log::IOBackend.new(formatter: JournalLogFormat, dispatcher: ::Log::DirectDispatcher)
::Log::IOBackend.new(formatter: Journal::LogFormat, dispatcher: ::Log::DirectDispatcher)
else
Log::IOBackend.new(formatter: StdoutLogFormat, dispatcher: ::Log::DirectDispatcher)
::Log::IOBackend.new(formatter: Stdout::LogFormat, dispatcher: ::Log::DirectDispatcher)
end
Log.setup_from_env(default_level: @log_level, backend: log_backend)
::Log.setup_from_env(default_level: @log_level, backend: log_backend)

server = AMQProxy::Server.new(u.hostname || "", port, tls, @idle_connection_timeout)

Expand Down Expand Up @@ -151,7 +153,7 @@ class AMQProxy::CLI
end
end

struct JournalLogFormat < Log::StaticFormatter
struct Journal::LogFormat < ::Log::StaticFormatter
def run
source
context(before: '[', after: ']')
Expand All @@ -161,7 +163,7 @@ class AMQProxy::CLI
end
end

struct StdoutLogFormat < Log::StaticFormatter
struct Stdout::LogFormat < ::Log::StaticFormatter
def run
timestamp
severity
Expand Down

0 comments on commit ff2d2fa

Please sign in to comment.