discourse/app/models/imap_sync_log.rb
Martin Brennan 4670b62969
DEV: IMAP log to database (#10435)
Convert all IMAP logging to write to a database table for easier inspection. These logs are cleaned up daily if they are > 5 days old.

Logs can easily be watched in dev by setting DISCOURSE_DEV_LOG_LEVEL=\"debug\" and running tail -f development.log | grep IMAP
2020-08-14 12:01:31 +10:00

61 lines
1.5 KiB
Ruby

# frozen_string_literal: true
class ImapSyncLog < ActiveRecord::Base
RETAIN_LOGS_DAYS = 5
belongs_to :group
def self.levels
@levels ||= Enum.new(
debug: 1,
info: 2,
warn: 3,
error: 4
)
end
def self.log(message, level, group_id = nil)
now = Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")
new_log = create(message: message, level: ImapSyncLog.levels[level], group_id: group_id)
Rails.logger.send(level, "#{level[0].upcase}, [#{now}] [IMAP] (group_id #{group_id}) #{message}")
new_log
end
def self.debug(message, group_or_id)
group_id = group_or_id.is_a?(Integer) ? group_or_id : group_or_id.id
log(message, :debug, group_id)
end
def self.info(message, group_or_id)
group_id = group_or_id.is_a?(Integer) ? group_or_id : group_or_id.id
log(message, :info, group_id)
end
def self.warn(message, group_or_id)
group_id = group_or_id.is_a?(Integer) ? group_or_id : group_or_id.id
log(message, :warn, group_id)
end
def self.error(message, group_or_id)
group_id = group_or_id.is_a?(Integer) ? group_or_id : group_or_id.id
log(message, :error, group_id)
end
end
# == Schema Information
#
# Table name: imap_sync_logs
#
# id :bigint not null, primary key
# level :integer
# message :string
# group_id :bigint
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_imap_sync_logs_on_group_id (group_id)
# index_imap_sync_logs_on_level (level)
#