Class: LoggingLibrary::CustomFormatter::LogMessage

Inherits:
Struct
  • Object
show all
Defined in:
lib/logging_library/custom_formatter.rb

Constant Summary collapse

LINE_PREPEND =
' ' * 8

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#logger_nameObject

Returns the value of attribute logger_name

Returns:

  • (Object)

    the current value of logger_name



11
12
13
# File 'lib/logging_library/custom_formatter.rb', line 11

def logger_name
  @logger_name
end

#messageObject

Returns the value of attribute message

Returns:

  • (Object)

    the current value of message



11
12
13
# File 'lib/logging_library/custom_formatter.rb', line 11

def message
  @message
end

#severityObject

Returns the value of attribute severity

Returns:

  • (Object)

    the current value of severity



11
12
13
# File 'lib/logging_library/custom_formatter.rb', line 11

def severity
  @severity
end

#timeObject

Returns the value of attribute time

Returns:

  • (Object)

    the current value of time



11
12
13
# File 'lib/logging_library/custom_formatter.rb', line 11

def time
  @time
end

Instance Method Details

#color_for_severityObject



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_messageObject



23
24
25
26
# File 'lib/logging_library/custom_formatter.rb', line 23

def colored_message
  return formatted_message unless Rainbow.enabled
  Rainbow(formatted_message).color(text_color_for_severity)
end

#formatted_colored_logger_nameObject



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_severityObject



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_timeObject



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_nameObject



74
75
76
# File 'lib/logging_library/custom_formatter.rb', line 74

def formatted_logger_name
  logger_name + ':'
end

#formatted_messageObject

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 formatted_message
  case message
  when ::String
    message
  when ::Exception
    "#{message.message} (#{message.class})\n" +
      LINE_PREPEND +
      (message.backtrace || []).join("\n#{LINE_PREPEND}")
  else
    message.inspect
  end
end

#formatted_severityObject



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

def formatted_severity
  format('%-5s', severity)
end

#formatted_timeObject



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

def formatted_time
  '[' + time.strftime(DATE_PATTERN) + ']'
end

#show_time?Boolean

Returns:

  • (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_severityObject



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_severityObject 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_stringObject



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, colored_message)
  else
    format("%-5s %s %s\n", formatted_colored_severity, formatted_colored_logger_name, colored_message)
  end
end

#tty?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/logging_library/custom_formatter.rb', line 118

def tty?
  $stderr.tty?
end