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)


92
93
94
# File 'lib/vedeu/logging/log.rb', line 92

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

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

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:

  • (String)


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

def log(message:, force: false, type: :info)
  if (Vedeu.config.log? || force) && Vedeu.config.loggable?(type)
    output = log_entry(type, message)
    logger.debug(output)
    output
  end
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)


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

def log_entry(type, message)
  colours = message_types.fetch(type, [:default, :default])

  [
    Vedeu.esc.colour(colours[0]) { "[#{type}]".ljust(11) },
    Vedeu.esc.colour(colours[1]) { message },
  ].join
end

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

Write a message to STDERR.

Examples:

Vedeu.log_stderr

Returns:

  • (String)


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

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

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

Write a message to STDOUT.

Examples:

Vedeu.log_stdout

Returns:

  • (String)


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

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

.loggerBoolean (private)

Returns:



77
78
79
80
81
82
83
# File 'lib/vedeu/logging/log.rb', line 77

def logger
  MonoLogger.new(Vedeu.config.log).tap do |log|
    log.formatter = proc do |_, _, _, message|
      formatted_message(message)
    end
  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.

Returns:

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


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

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_blue, :blue],

    info:     [:white, :light_grey],
    test:     [:white, :light_grey],
    debug:    [:white, :light_grey],
    compress: [:white, :light_grey],

    input:    [:light_yellow, :yellow],
    output:   [:light_yellow, :yellow],

    cursor:   [:light_green, :green],
    buffer:   [:light_green, :green],
    render:   [:light_green, :green],

    error:    [:light_red, :red],

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

    blue:     [:light_blue,    :blue],
    cyan:     [:light_cyan,    :cyan],
    green:    [:light_green,   :green],
    magenta:  [:light_magenta, :magenta],
    red:      [:light_red,     :red],
    white:    [:white,         :light_grey],
    yellow:   [:light_yellow,  :yellow],
  }
end

.timestampString Also known as: log_timestamp

Returns a formatted timestamp. eg. [137.7824]

Returns:

  • (String)


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vedeu/logging/log.rb', line 60

def timestamp
  @now  = Vedeu.clock_time
  @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