Module: FormatLogs::ClassMethods

Defined in:
lib/format_logs.rb

Instance Method Summary collapse

Instance Method Details

#color(s, color) ⇒ Object



109
110
111
# File 'lib/format_logs.rb', line 109

def color(s, color)
  use_color? ? "\033[#{color}#{s}\033[0m" : s
end

#debug_sObject



85
86
87
# File 'lib/format_logs.rb', line 85

def debug_s
  "DEBUG "
end

#default_settingsObject



28
29
30
31
32
33
34
35
36
# File 'lib/format_logs.rb', line 28

def default_settings
  {
    :time_format => '%Y-%m-%d %H:%M:%S %z',
    :show_pid => true,
    :show_host => true,
    :show_time => true,
    :trim_leading_whitespace => true
  }
end

#error_sObject



101
102
103
# File 'lib/format_logs.rb', line 101

def error_s
  @error_s ||= color("ERROR ", '31m')
end

#fatal_sObject



105
106
107
# File 'lib/format_logs.rb', line 105

def fatal_s
  @fatal_s ||= color("FATAL ", '1;30;41m')
end

#flush_cached_settings!Object



24
25
26
# File 'lib/format_logs.rb', line 24

def flush_cached_settings!
  [@time_format, @show_time, @show_pid, @show_host, @trim_leading_whitespace].each {|a| a = nil}
end

#hostnameObject



70
71
72
# File 'lib/format_logs.rb', line 70

def hostname
  @hostname ||= "#{`hostname -s`.strip rescue ''} - " if show_host?
end

#info_sObject



93
94
95
# File 'lib/format_logs.rb', line 93

def info_s
  @info_s ||= color("INFO  ", '32m')
end

#pidObject



66
67
68
# File 'lib/format_logs.rb', line 66

def pid
  "#{$$} - " if show_pid?
end

#settingsObject



15
16
17
# File 'lib/format_logs.rb', line 15

def settings
  @settings ||= default_settings
end

#settings=(new_settings) ⇒ Object



19
20
21
22
# File 'lib/format_logs.rb', line 19

def settings=(new_settings)
  @settings = default_settings + new_settings
  flush_attributes!
end

#show_host?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/format_logs.rb', line 58

def show_host?
  @show_host ||= settings[:show_host]
end

#show_pid?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/format_logs.rb', line 50

def show_pid?
  @show_pid ||= settings[:show_pid]
end

#show_time?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/format_logs.rb', line 42

def show_time?
  @show_time ||= settings[:show_time]
end

#time_formatObject



38
39
40
# File 'lib/format_logs.rb', line 38

def time_format
  @time_format ||= settings[:time_format]
end

#timestampObject



62
63
64
# File 'lib/format_logs.rb', line 62

def timestamp
  "#{Time.now.utc.strftime(time_format)} - " if show_time?
end

#to_color(severity) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/format_logs.rb', line 74

def to_color(severity)
  return case severity
    when ActiveSupport::BufferedLogger::INFO  then info_s
    when ActiveSupport::BufferedLogger::WARN  then warn_s
    when ActiveSupport::BufferedLogger::ERROR then error_s
    when ActiveSupport::BufferedLogger::FATAL then fatal_s
    when ActiveSupport::BufferedLogger::DEBUG then debug_s
  else unknown_s
  end
end

#trim_leading_whitespace?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/format_logs.rb', line 54

def trim_leading_whitespace?
  @trim_leading_whitespace ||= settings[:trim_leading_whitespace]
end

#unknown_SObject



89
90
91
# File 'lib/format_logs.rb', line 89

def unknown_S
  "UNKNOWN"
end

#use_color?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/format_logs.rb', line 46

def use_color?
  @use_color ||= (Rails.configuration.colorize_logging != false)
end

#warn_sObject



97
98
99
# File 'lib/format_logs.rb', line 97

def warn_s
  @warn_s ||= color("WARN  ", '33m')
end