Class: Vedeu::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/support/log.rb

Overview

Provides the ability to log anything to the Vedeu log file.

Class Method Summary collapse

Class Method Details

.enabled?Boolean (private)

Returns:

  • (Boolean)


130
131
132
# File 'lib/vedeu/support/log.rb', line 130

def enabled?
  Vedeu::Configuration.debug?
end

.formatted_message(message, time = Time.now) ⇒ String (private)

Returns:

  • (String)


125
126
127
# File 'lib/vedeu/support/log.rb', line 125

def formatted_message(message, time = Time.now)
  [timestamp(time.utc.iso8601), message, "\n"].join
end

.log(message:, force: false, type: :info) ⇒ TrueClass

Write a message to the Vedeu log file.

Examples:

Vedeu.log(message: 'A useful debugging message: Error!')

Parameters:

  • message (String)

    The message you wish to emit to the log file, useful for debugging.

  • force (Boolean) (defaults to: false)

    When evaluates to true will attempt to write to the log file regardless of the Configuration setting.

  • type (Symbol) (defaults to: :info)

    Colour code messages in the log file depending on their source.

Returns:

  • (TrueClass)


109
110
111
# File 'lib/vedeu/support/log.rb', line 109

def log(message:, force: false, type: :info)
  logger.debug([message_type(type), message]) if enabled? || force
end

.log_fileString (private)

Returns:

  • (String)


135
136
137
# File 'lib/vedeu/support/log.rb', line 135

def log_file
  Vedeu::Configuration.log
end

.loggerTrueClass

Returns:

  • (TrueClass)


114
115
116
117
118
119
120
# File 'lib/vedeu/support/log.rb', line 114

def logger
  MonoLogger.new(log_file).tap do |log|
    log.formatter = proc do |_, time, _, message|
      formatted_message(message, time)
    end
  end
end

.message_type(type) ⇒ String (private)

Returns:

  • (String)


140
141
142
# File 'lib/vedeu/support/log.rb', line 140

def message_type(type)
  Esc.send(message_types.fetch(type, :default)) { "[#{type}]".ljust(9) }
end

.message_typesHash (private)

Returns:

  • (Hash)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/vedeu/support/log.rb', line 145

def message_types
  {
    config: :yellow,
    create: :green,
    debug:  :red,
    drb:    :blue,
    event:  :magenta,
    info:   :default,
    input:  :yellow,
    output: :green,
    store:  :cyan,
    test:   :white,
    update: :cyan,
  }
end

.timestamp(utc_time) ⇒ String (private)

Returns a formatted (red, underlined) UTC timestamp, eg. 2014-10-24T12:34:56Z

Returns:

  • (String)


165
166
167
168
169
170
171
# File 'lib/vedeu/support/log.rb', line 165

def timestamp(utc_time)
  return '' if @last_seen == utc_time

  @last_seen = utc_time

  "\n\e[4m\e[31m" + utc_time + "\e[39m\e[24m\n"
end