Class: Vedeu::Logging::Log

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

Overview

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

Class Method Summary collapse

Class Method Details

.formatted_message(message) ⇒ String (private)

Returns the message with timestamp.

[ 0.0987] [debug]  Something happened.

Parameters:

  • message (String)

    The message type and message coloured and combined.

Returns:

  • (String)


75
76
77
# File 'lib/vedeu/logging/log.rb', line 75

def formatted_message(message)
  "#{timestamp}#{message}\n".freeze if message
end

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

Write a message to the Vedeu log file.

Examples:

Vedeu.log(type:    :debug,
          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. See message_types

Returns:

  • (TrueClass)


26
27
28
29
30
31
32
33
34
35
# File 'lib/vedeu/logging/log.rb', line 26

def log(message:, force: false, type: :info)
  output = log_entry(type, message)

  if (enabled? || force) && (Vedeu::Configuration.log_only.empty? ||
             Vedeu::Configuration.log_only.include?(type))
    logger.debug(output)
  end

  output
end

.log_entry(type, message) ⇒ String (private)

Returns the message:

[type] message

Parameters:

  • type (Symbol)

    The type of log message.

  • message (String)

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

Returns:

  • (String)


87
88
89
# File 'lib/vedeu/logging/log.rb', line 87

def log_entry(type, message)
  "#{message_type(type)}#{message_body(type, message)}".freeze
end

.log_fileString (private) Also known as: enabled?

Fetches the filename from the configuration.

Returns:

  • (String)


94
95
96
# File 'lib/vedeu/logging/log.rb', line 94

def log_file
  Vedeu::Configuration.log
end

.log_stderr(type: :info, message:) ⇒ TrueClass

Write a message to STDERR.

Examples:

Vedeu.log_stderr

Returns:

  • (TrueClass)


53
54
55
# File 'lib/vedeu/logging/log.rb', line 53

def log_stderr(type: :info, message:)
  $stderr.puts log_entry(type, message)
end

.log_stdout(type: :info, message:) ⇒ TrueClass

Write a message to STDOUT.

Examples:

Vedeu.log_stdout

Returns:

  • (TrueClass)


43
44
45
# File 'lib/vedeu/logging/log.rb', line 43

def log_stdout(type: :info, message:)
  $stdout.puts log_entry(type, message)
end

.loggerTrueClass (private)

Returns:

  • (TrueClass)


60
61
62
63
64
65
66
# File 'lib/vedeu/logging/log.rb', line 60

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

.message_body(type, body) ⇒ String (private)

Displays the message body using the colour specified in the last element of message_types.

Parameters:

  • type (Symbol)

    The type of log message.

  • body (String)

    The log message itself.

Returns:

  • (String)


105
106
107
108
109
110
# File 'lib/vedeu/logging/log.rb', line 105

def message_body(type, body)
  Vedeu::EscapeSequences::Esc
    .send(message_types.fetch(type, :default)[-1]) do
    body
  end
end

.message_type(type) ⇒ String (private)

Displays the message type using the colour specified in the first element of message_types.

Parameters:

  • type (Symbol)

    The type of log message.

Returns:

  • (String)


117
118
119
120
121
122
# File 'lib/vedeu/logging/log.rb', line 117

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

.message_typesHash<Symbol => Array<Symbol>> (private)

The defined message types for Vedeu with their respective colours. When used, produces a log entry of the format:

[type] message

The ‘type’ will be shown as the first colour defined in the value array, whilst the ‘message’ will be shown using the last colour.

Valid types available:

:config, :create, :debug, :error, :drb, :event, :info,
:input, :output, :reset, :store, :test, :timer, :update

Returns:

  • (Hash<Symbol => Array<Symbol>>)


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
# File 'lib/vedeu/logging/log.rb', line 139

def message_types
  {
    create: [:light_cyan,    :cyan],
    store:  [:light_cyan,    :cyan],
    update: [:light_cyan,    :cyan],
    reset:  [:light_cyan,    :cyan],

    event:  [:light_magenta, :magenta],

    timer:  [:light_yellow,  :yellow],

    info:   [:white,   :default],
    test:   [:white,   :default],
    debug:  [:white,   :default],

    input:  [:light_red,     :red],
    output: [:light_red,     :red],
    error:  [:light_red,     :red],

    config: [:light_blue,    :blue],
    dsl:    [:light_blue,    :blue],
    editor: [:light_blue,    :blue],
    drb:    [:light_blue,    :blue],
  }
end

.timestampString (private)

Returns a formatted timestamp. eg. [137.7824]

Returns:

  • (String)


169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/vedeu/logging/log.rb', line 169

def timestamp
  @now  = Time.now.to_f
  @time ||= 0.0
  @last ||= @now

  unless @last == @time
    @time += (@now - @last).round(4)
    @last = @now
  end

  "[#{format('%7.4f', @time.to_s)}] ".rjust(7)
end