Class: SrLog::Log

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/sr_log/log.rb

Defined Under Namespace

Classes: SrLogger

Instance Method Summary collapse

Instance Method Details

#log(log_key, msg, opts = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sr_log/log.rb', line 11

def log log_key, msg, opts = {}
  @logfiles ||= {}
  log_month = Date.current.strftime '%Y.%m'

  unless @logfiles.has_key?(log_key) && @logfiles[log_key][:log_month] == log_month
    filename = "#{log_month}_#{log_key.to_s}.log"

    if opts.has_key?(:dir)
      log_path = File.join opts[:dir], filename
      FileUtils.mkdir_p(opts[:dir]) unless File.directory?(opts[:dir])
    else
      log_path = Rails.root.join 'log', filename # TODO: remove undocumented dependency of Rails
    end

    @logfiles[log_key] = {log: SrLogger.new(log_path), log_month: log_month}
  end

  msg = "Logged by user: #{opts[:current_user]}\n#{msg}" if opts.has_key?(:current_user)

  @logfiles[log_key][:log].info msg
end