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)


117
118
119
# File 'lib/vedeu/support/log.rb', line 117

def enabled?
  Vedeu::Configuration.debug?
end

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

Returns:

  • (String)


112
113
114
# File 'lib/vedeu/support/log.rb', line 112

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)


96
97
98
# File 'lib/vedeu/support/log.rb', line 96

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

.log_fileString (private)

Returns:

  • (String)


122
123
124
# File 'lib/vedeu/support/log.rb', line 122

def log_file
  Vedeu::Configuration.log
end

.loggerTrueClass

Returns:

  • (TrueClass)


101
102
103
104
105
106
107
# File 'lib/vedeu/support/log.rb', line 101

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)


127
128
129
130
131
# File 'lib/vedeu/support/log.rb', line 127

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

.message_typesHash<Symbol => Symbol> (private)

Returns:

  • (Hash<Symbol => Symbol>)


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/vedeu/support/log.rb', line 134

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)


154
155
156
157
158
159
160
# File 'lib/vedeu/support/log.rb', line 154

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