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

.colours(type) ⇒ Array<Symbol> (private)



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

def colours(type)
  Vedeu::LOG_TYPES.fetch(type, [:default, :default])
end

.formatted_message(message) ⇒ String (private)



90
91
92
# File 'lib/vedeu/logging/log.rb', line 90

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

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

Write a message to the Vedeu log file.

Examples:

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


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

def log(message:, type: :info)
  if 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


101
102
103
# File 'lib/vedeu/logging/log.rb', line 101

def log_entry(type, message)
  log_type(type) + log_message(type, message)
end

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



114
115
116
# File 'lib/vedeu/logging/log.rb', line 114

def log_message(type, message)
  Vedeu.esc.colour(colours(type)[1]) { message }
end

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



54
55
56
57
58
# File 'lib/vedeu/logging/log.rb', line 54

def log_stderr(message:, type: :error)
  log(message: message, type: type)

  $stderr.puts log_entry(type, message)
end

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



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

def log_stdout(message:, type: :info)
  log(message: message, type: type)

  $stdout.puts log_entry(type, message)
end

.log_type(type) ⇒ String (private)



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

def log_type(type)
  Vedeu.esc.colour(colours(type)[0]) { "[#{type}]".ljust(11) }
end

.loggerBoolean (private)



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

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

.timestampString Also known as: log_timestamp



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

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