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

Use Outboxer::Logger in publisher #178

Merged
merged 2 commits into from
Nov 16, 2024
Merged
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
12 changes: 6 additions & 6 deletions bin/outboxer_publisher
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ options = {
poll: ENV.fetch('OUTBOXER_POLL', 5.0).to_f,
tick: ENV.fetch('OUTBOXER_TICK', 0.1).to_f,
heartbeat: ENV.fetch('OUTBOXER_HEARTBEAT', 5.0).to_f,
log_level: ENV.fetch('OUTBOXER_LOG_LEVEL', 'info').downcase,
log_level: ENV.fetch('OUTBOXER_LOG_LEVEL', 'INFO'),
sidekiq_redis_url: ENV.fetch('SIDEKIQ_REDIS_URL', 'redis://localhost:6379/0')
}

Sidekiq.configure_client do |config|
config.redis = { url: options[:sidekiq_redis_url], size: options[:concurrency] }
end

logger = Sidekiq.logger
logger.level = Logger.const_get(options[:log_level].upcase)
logger = Outboxer::Logger.new($stdout, level: options[:log_level])

Outboxer::Publisher.publish(
env: options[:env],
Expand All @@ -31,12 +30,13 @@ Outboxer::Publisher.publish(
poll: options[:poll],
tick: options[:tick],
heartbeat: options[:heartbeat],
logger: logger,
logger: logger
) do |message|
case message[:messageable_type]
when 'Event'
EventCreatedJob.perform_async({ 'id' => message[:messageable_id] })

logger.info "Outboxer published message #{message[:id]} for "\
"#{message[:messageable_type]}::#{message[:messageable_id]} "\
end
end

# bin/outboxer_publisher
13 changes: 8 additions & 5 deletions lib/outboxer/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ def initialize(*args, **kwargs)
super(*args, **kwargs)

self.formatter = proc do |severity, datetime, progname, msg|
formatted_time = datetime.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
pid = Process.pid
tid = Thread.current.object_id.to_s(36)
level = severity
"#{formatted_time} pid=#{pid} tid=#{tid} #{level}: #{msg}\n"
current_thread = ::Thread.current

pid = current_thread['outboxer_pid'] ||= ::Process.pid

tid = current_thread["outboxer_tid"] ||=
(current_thread.name || (current_thread.object_id ^ pid).to_s(36))

"#{datetime.utc.iso8601(3)} pid=#{pid} tid=#{tid} #{severity}: #{msg}\n"
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/lib/outboxer/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
module Outboxer
RSpec.describe Logger do
let(:output) { StringIO.new }
let(:logger) { Logger.new(output) }
let(:level) { 'info' }
let(:logger) { Logger.new(output, level: Logger.const_get(level.upcase)) }

describe 'logging' do
it 'logs messages with correct format' do
Expand Down
Loading