Module: FormatLogs::ClassMethods
- Defined in:
- lib/format_logs.rb
Instance Method Summary collapse
- #color(s, color) ⇒ Object
- #debug_s ⇒ Object
- #default_settings ⇒ Object
- #error_s ⇒ Object
- #fatal_s ⇒ Object
- #flush_cached_settings! ⇒ Object
- #hostname ⇒ Object
- #info_s ⇒ Object
- #pid ⇒ Object
- #settings ⇒ Object
- #settings=(new_settings) ⇒ Object
- #show_host? ⇒ Boolean
- #show_pid? ⇒ Boolean
- #show_time? ⇒ Boolean
- #time_format ⇒ Object
- #timestamp ⇒ Object
- #to_color(severity) ⇒ Object
- #trim_leading_whitespace? ⇒ Boolean
- #unknown_S ⇒ Object
- #use_color? ⇒ Boolean
- #warn_s ⇒ Object
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_s ⇒ Object
85 86 87 |
# File 'lib/format_logs.rb', line 85 def debug_s "DEBUG " end |
#default_settings ⇒ Object
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_s ⇒ Object
101 102 103 |
# File 'lib/format_logs.rb', line 101 def error_s @error_s ||= color("ERROR ", '31m') end |
#fatal_s ⇒ Object
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 |
#hostname ⇒ Object
70 71 72 |
# File 'lib/format_logs.rb', line 70 def hostname @hostname ||= "#{`hostname -s`.strip rescue ''} - " if show_host? end |
#info_s ⇒ Object
93 94 95 |
# File 'lib/format_logs.rb', line 93 def info_s @info_s ||= color("INFO ", '32m') end |
#pid ⇒ Object
66 67 68 |
# File 'lib/format_logs.rb', line 66 def pid "#{$$} - " if show_pid? end |
#settings ⇒ Object
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
58 59 60 |
# File 'lib/format_logs.rb', line 58 def show_host? @show_host ||= settings[:show_host] end |
#show_pid? ⇒ Boolean
50 51 52 |
# File 'lib/format_logs.rb', line 50 def show_pid? @show_pid ||= settings[:show_pid] end |
#show_time? ⇒ Boolean
42 43 44 |
# File 'lib/format_logs.rb', line 42 def show_time? @show_time ||= settings[:show_time] end |
#time_format ⇒ Object
38 39 40 |
# File 'lib/format_logs.rb', line 38 def time_format @time_format ||= settings[:time_format] end |
#timestamp ⇒ Object
62 63 64 |
# File 'lib/format_logs.rb', line 62 def "#{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
54 55 56 |
# File 'lib/format_logs.rb', line 54 def trim_leading_whitespace? @trim_leading_whitespace ||= settings[:trim_leading_whitespace] end |
#unknown_S ⇒ Object
89 90 91 |
# File 'lib/format_logs.rb', line 89 def unknown_S "UNKNOWN" end |
#use_color? ⇒ Boolean
46 47 48 |
# File 'lib/format_logs.rb', line 46 def use_color? @use_color ||= (Rails.configuration.colorize_logging != false) end |
#warn_s ⇒ Object
97 98 99 |
# File 'lib/format_logs.rb', line 97 def warn_s @warn_s ||= color("WARN ", '33m') end |