2020-08-13 22:01:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ImapSyncLog < ActiveRecord::Base
|
2020-09-09 23:41:46 -04:00
|
|
|
RETAIN_LOGS_DAYS ||= 5
|
2020-08-13 22:01:31 -04:00
|
|
|
|
|
|
|
belongs_to :group
|
|
|
|
|
|
|
|
def self.levels
|
2020-09-09 23:41:46 -04:00
|
|
|
@levels ||= Enum.new(:debug, :info, :warn, :error)
|
2020-08-13 22:01:31 -04:00
|
|
|
end
|
|
|
|
|
2021-01-20 20:37:47 -05:00
|
|
|
def self.log(message, level, group_id = nil, db = true)
|
2020-08-13 22:01:31 -04:00
|
|
|
now = Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")
|
2021-01-20 20:37:47 -05:00
|
|
|
|
|
|
|
new_log = if db
|
|
|
|
create(message: message, level: ImapSyncLog.levels[level], group_id: group_id)
|
|
|
|
end
|
|
|
|
|
2021-01-05 00:32:04 -05:00
|
|
|
if ENV["DEBUG_IMAP"]
|
|
|
|
Rails.logger.send(:warn, "#{level[0].upcase}, [#{now}] [IMAP] (group_id #{group_id}) #{message}")
|
|
|
|
else
|
|
|
|
Rails.logger.send(level, "#{level[0].upcase}, [#{now}] [IMAP] (group_id #{group_id}) #{message}")
|
|
|
|
end
|
2021-01-20 20:37:47 -05:00
|
|
|
|
2020-08-13 22:01:31 -04:00
|
|
|
new_log
|
|
|
|
end
|
|
|
|
|
2021-01-20 20:37:47 -05:00
|
|
|
def self.debug(message, group_or_id, db: true)
|
2020-08-13 22:01:31 -04:00
|
|
|
group_id = group_or_id.is_a?(Integer) ? group_or_id : group_or_id.id
|
2021-01-20 20:37:47 -05:00
|
|
|
log(message, :debug, group_id, db)
|
2020-08-13 22:01:31 -04:00
|
|
|
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
|
2020-10-27 14:12:33 -04:00
|
|
|
# level :integer not null
|
|
|
|
# message :string not null
|
2020-08-13 22:01:31 -04:00
|
|
|
# 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)
|
|
|
|
#
|