Module: Hammock::Logging::Methods

Included in:
ModelLogging::ClassMethods, ModelLogging::InstanceMethods
Defined in:
lib/hammock/logging.rb

Instance Method Summary collapse

Instance Method Details

#dlog(*args) ⇒ Object



59
60
61
62
63
64
# File 'lib/hammock/logging.rb', line 59

def dlog *args
  unless production?
    opts = args.extract_options!
    log *(args << opts.merge(:skip => (opts[:skip] || 0) + 1))
  end
end

#log(*args) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hammock/logging.rb', line 72

def log *args
  opts = {
    :skip => 0
  }.merge(args.extract_options!)

  msg = if opts[:error]
    "#{ErrorPrefix}: #{opts[:error]}"
  elsif args.first.is_a? String
    args.first
  elsif args.all? {|i| i.is_a?(ActiveRecord::Base) }
    @errorModels = args unless opts[:errorModels] == false
    args.map {|record| "#{record.inspect}: #{record.errors.full_messages.inspect}" }.join(', ')
  else
    args.map(&:inspect).join(', ')
  end

  msg = msg.colorize('on red') if opts[:error] || opts[:report]

  callpoint = caller[opts[:skip]].sub(rails_root.end_with('/'), '')
  entry = "#{callpoint}#{msg.blank? ? (opts[:report] ? ' <-- something broke here' : '.') : ' | '}#{msg}"

  Rails.logger.send opts[:error].blank? ? :info : :error, entry # Write to the Rails log
  log_concise entry, opts[:report] # Also write to the concise log
end

#log_concise(msg, report = false) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/hammock/logging.rb', line 43

def log_concise msg, report = false
  buf = "#{Time.now.strftime('%Y-%m-%d %H:%M:%S %Z')} | #{msg}\n"
  path = File.join RAILS_ROOT, 'log', rails_env

  File.open("#{path}.concise.log", 'a') {|f| f << buf }
  File.open("#{path}.report.log", 'a') {|f| f << buf } if report

  nil
end

#log_fail(*args) ⇒ Object



66
67
68
69
70
# File 'lib/hammock/logging.rb', line 66

def log_fail *args
  opts = args.extract_options!
  log *(args << opts.merge(:skip => (opts[:skip] || 0) + 1))
  false
end

#log_hitObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hammock/logging.rb', line 16

def log_hit
  log_concise [
    request.remote_ip.colorize('green'),
    (@current_site.subdomain unless @current_site.nil?),
    (request.session_options[:id].nil? ? 'nil' : ('...' + request.session_options[:id][-8, 8])),
    (current_user.nil? ? "unauthed" : "User<#{current_user.id}> #{current_user.name}").colorize('green'),
    response.status,
    log_hit_request_info,
    log_hit_route_info
  ].squash.join(' | ')
end

#log_hit_request_infoObject



28
29
30
31
32
33
# File 'lib/hammock/logging.rb', line 28

def log_hit_request_info
  (request.xhr? ? 'XHR/' : '') +
  (params[:_method] || request.method).to_s.upcase +
  ' ' +
  request.request_uri.colorize('grey', '?')
end

#log_hit_route_infoObject



35
36
37
38
39
40
41
# File 'lib/hammock/logging.rb', line 35

def log_hit_route_info
  params[:controller] +
  '#' +
  params[:action] +
  ' ' +
  params.discard(:controller, :action).inspect.gsub("\n", '\n').colorize('grey')
end

#report(*args) ⇒ Object



53
54
55
56
57
# File 'lib/hammock/logging.rb', line 53

def report *args
  opts = args.extract_options!
  log *(args << opts.merge(:report => true, :skip => (opts[:skip] || 0) + 1))
  log caller.remove_framework_backtrace.join("\n")
end