Class: Pantheios::Services::ColouredConsoleLogService

Inherits:
Object
  • Object
show all
Defined in:
lib/pantheios/services/coloured_console_log_service.rb

Overview

A class that fulfils the Pantheios LogService protocol that allows all severities and logs to the console (via $stdout and $stderr)

NOTE: The LogService protocol is implemented by a class that provides the instance methods severity_logged?(severity : Object) : boolean and log(severity : Object, t : Time, prefix : String|Array, msg : String)

Defined Under Namespace

Modules: Constants

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.requires_prefix?Boolean

module Constants

Returns:

  • (Boolean)


70
71
72
73
74
75
# File 'lib/pantheios/services/coloured_console_log_service.rb', line 70

def self.requires_prefix?

  return @requires_prefix unless @requires_prefix.nil?

  @requires_prefix = ::Pantheios::Services::Common::Console::Internal_::SHOULD_COLOURIZE_ ? :parts : false
end

Instance Method Details

#infer_stream(sev) ⇒ Object

Overrideable method that determines which stream to write, based on a severity. This implementation always returns $stderr

Overrides must return an object that supports the puts(String) method



191
192
193
194
# File 'lib/pantheios/services/coloured_console_log_service.rb', line 191

def infer_stream sev

  $stderr
end

#log(sev, t, pref, msg) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/pantheios/services/coloured_console_log_service.rb', line 87

def log sev, t, pref, msg

  stm = infer_stream sev

  if requires_prefix?

    pref = pref.map do |part|

      bg = Constants::Background
      fg = Constants::Foreground

      if part.respond_to?(:severity)

        part = fg.bold part

        case sev
        when :violation

          part = bg.red part
          #part = fg.bright_magenta part
          part = fg.bright_yellow part
          part = fg.blinking part
        when :alert

          part = bg.red part
          part = fg.bright_cyan part
          part = fg.blinking part
        when :critical

          part = bg.red part
          part = fg.white part
        when :failure

          part = bg.yellow part
          part = fg.red part
        when :warning

          part = bg.yellow part
          part = fg.blue part
        when :notice

          part = bg.dark_grey part
          part = fg.white part
        when :informational

          part = bg.dark_grey part
          part = fg.light_grey part
        when :debug0

          part = bg.blue part
          part = fg.light_grey part
        when :debug1

          part = bg.blue part
          part = fg.light_grey part
        when :debug2

          part = bg.blue part
          part = fg.light_grey part
        when :debug3

          part = bg.blue part
          part = fg.light_grey part
        when :debug4

          part = bg.blue part
          part = fg.light_grey part
        when :debug5

          part = bg.blue part
          part = fg.light_grey part
        when :trace

          part = bg.blue part
          part = fg.light_grey part
        when :benchmark

          part = bg.black part
          part = fg.light_grey part
        else

          ;
        end
      else

        part = fg.dark_grey part
      end

      part
    end.join(', ')

    pref = '[' + pref + ']: '

    #pref = pref.map { |pp| pp.severity? ? map_sev_(sev) : sev }.join(
  end

  stm.puts "#{pref}#{msg}"
end

#requires_prefix?Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/pantheios/services/coloured_console_log_service.rb', line 77

def requires_prefix?

  self.class.requires_prefix?
end

#severity_logged?(severity) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
# File 'lib/pantheios/services/coloured_console_log_service.rb', line 82

def severity_logged? severity

  true
end