Class: LoggingLibrary::CustomFormatter::LogMessage
- Inherits:
-
Struct
- Object
- Struct
- LoggingLibrary::CustomFormatter::LogMessage
- Defined in:
- lib/logging_library/custom_formatter.rb
Constant Summary collapse
- LINE_PREPEND =
' ' * 8
Instance Attribute Summary collapse
-
#logger_name ⇒ Object
Returns the value of attribute logger_name.
-
#message ⇒ Object
Returns the value of attribute message.
-
#severity ⇒ Object
Returns the value of attribute severity.
-
#time ⇒ Object
Returns the value of attribute time.
Instance Method Summary collapse
- #color_for_severity ⇒ Object
- #colored_message ⇒ Object
- #formatted_colored_logger_name ⇒ Object
- #formatted_colored_severity ⇒ Object
- #formatted_colored_time ⇒ Object
- #formatted_logger_name ⇒ Object
-
#formatted_message ⇒ Object
Converts some argument to a Logger.severity() call to a string.
- #formatted_severity ⇒ Object
- #formatted_time ⇒ Object
- #show_time? ⇒ Boolean
- #text_color_for_severity ⇒ Object
-
#time_color_for_severity ⇒ Object
(also: #color_for_severity_lighter)
Returns a somewhat lighter color for the time, to make it stand out in the presentation.
- #to_formatted_string ⇒ Object
- #tty? ⇒ Boolean
Instance Attribute Details
#logger_name ⇒ Object
Returns the value of attribute logger_name
11 12 13 |
# File 'lib/logging_library/custom_formatter.rb', line 11 def logger_name @logger_name end |
#message ⇒ Object
Returns the value of attribute message
11 12 13 |
# File 'lib/logging_library/custom_formatter.rb', line 11 def @message end |
#severity ⇒ Object
Returns the value of attribute severity
11 12 13 |
# File 'lib/logging_library/custom_formatter.rb', line 11 def severity @severity end |
#time ⇒ Object
Returns the value of attribute time
11 12 13 |
# File 'lib/logging_library/custom_formatter.rb', line 11 def time @time end |
Instance Method Details
#color_for_severity ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/logging_library/custom_formatter.rb', line 78 def color_for_severity if show_time? text_color_for_severity else color_for_severity_lighter end end |
#colored_message ⇒ Object
23 24 25 26 |
# File 'lib/logging_library/custom_formatter.rb', line 23 def return unless Rainbow.enabled Rainbow().color(text_color_for_severity) end |
#formatted_colored_logger_name ⇒ Object
60 61 62 63 64 |
# File 'lib/logging_library/custom_formatter.rb', line 60 def formatted_colored_logger_name return formatted_logger_name unless Rainbow.enabled Rainbow(formatted_logger_name).color(text_color_for_severity) end |
#formatted_colored_severity ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/logging_library/custom_formatter.rb', line 44 def formatted_colored_severity if Rainbow.enabled Rainbow(formatted_severity).color(color_for_severity) else formatted_severity end end |
#formatted_colored_time ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/logging_library/custom_formatter.rb', line 52 def formatted_colored_time if Rainbow.enabled Rainbow(formatted_time).color(time_color_for_severity) else formatted_severity end end |
#formatted_logger_name ⇒ Object
74 75 76 |
# File 'lib/logging_library/custom_formatter.rb', line 74 def formatted_logger_name logger_name + ':' end |
#formatted_message ⇒ Object
Converts some argument to a Logger.severity() call to a string. Regular strings pass through like normal, Exceptions get formatted as "message (class)\nbacktrace", and other random stuff gets put through "object.inspect"
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/logging_library/custom_formatter.rb', line 31 def case when ::String when ::Exception "#{.} (#{.class})\n" + LINE_PREPEND + (.backtrace || []).join("\n#{LINE_PREPEND}") else .inspect end end |
#formatted_severity ⇒ Object
66 67 68 |
# File 'lib/logging_library/custom_formatter.rb', line 66 def formatted_severity format('%-5s', severity) end |
#formatted_time ⇒ Object
70 71 72 |
# File 'lib/logging_library/custom_formatter.rb', line 70 def formatted_time '[' + time.strftime(DATE_PATTERN) + ']' end |
#show_time? ⇒ Boolean
112 113 114 115 116 |
# File 'lib/logging_library/custom_formatter.rb', line 112 def show_time? # When STDERR is redirected, we are likely running as a service with a syslog daemon already appending a timestamp to the # line (and two timestamps is redundant). tty? end |
#text_color_for_severity ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/logging_library/custom_formatter.rb', line 86 def text_color_for_severity case severity.downcase.to_sym when :fatal then :magenta when :error then :red when :warn then :yellow when :info then :gray when :debug then '#606060' else :gray end end |
#time_color_for_severity ⇒ Object Also known as: color_for_severity_lighter
Returns a somewhat lighter color for the time, to make it stand out in the presentation.
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/logging_library/custom_formatter.rb', line 98 def time_color_for_severity case severity.downcase.to_sym when :fatal then '#FF00FF' when :error then '#FF0000' when :warn then '#FFFF00' when :info then '#FFFFFF' when :debug then '#808080' else :gray end end |
#to_formatted_string ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/logging_library/custom_formatter.rb', line 14 def to_formatted_string if show_time? format("%s %s %s %s\n", formatted_colored_severity, formatted_colored_time, formatted_colored_logger_name, ) else format("%-5s %s %s\n", formatted_colored_severity, formatted_colored_logger_name, ) end end |
#tty? ⇒ Boolean
118 119 120 |
# File 'lib/logging_library/custom_formatter.rb', line 118 def tty? $stderr.tty? end |