Class: Vedeu::Logging::Log
- Inherits:
-
Object
- Object
- Vedeu::Logging::Log
- Defined in:
- lib/vedeu/logging/log.rb
Overview
Provides the ability to log anything to the Vedeu log file.
Class Method Summary collapse
-
.formatted_message(message) ⇒ String
private
Returns the message with timestamp.
-
.log(message:, force: false, type: :info) ⇒ TrueClass
Write a message to the Vedeu log file.
-
.log_entry(type, message) ⇒ String
private
Returns the message:.
-
.log_file ⇒ String
(also: enabled?)
private
Fetches the filename from the configuration.
-
.log_stderr(type: :info, message:) ⇒ TrueClass
Write a message to STDERR.
-
.log_stdout(type: :info, message:) ⇒ TrueClass
Write a message to STDOUT.
- .logger ⇒ TrueClass private
-
.message_body(type, body) ⇒ String
private
Displays the message body using the colour specified in the last element of Log.message_types.
-
.message_type(type) ⇒ String
private
Displays the message type using the colour specified in the first element of Log.message_types.
-
.message_types ⇒ Hash<Symbol => Array<Symbol>>
private
The defined message types for Vedeu with their respective colours.
-
.timestamp ⇒ String
private
Returns a formatted timestamp.
Class Method Details
.formatted_message(message) ⇒ String (private)
Returns the message with timestamp.
[ 0.0987] [debug] Something happened.
75 76 77 |
# File 'lib/vedeu/logging/log.rb', line 75 def () "#{timestamp}#{message}\n".freeze if end |
.log(message:, force: false, type: :info) ⇒ TrueClass
Write a message to the Vedeu log file.
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, ) 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]
87 88 89 |
# File 'lib/vedeu/logging/log.rb', line 87 def log_entry(type, ) "#{message_type(type)}#{message_body(type, message)}".freeze end |
.log_file ⇒ String (private) Also known as: enabled?
Fetches the filename from the configuration.
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.
53 54 55 |
# File 'lib/vedeu/logging/log.rb', line 53 def log_stderr(type: :info, message:) $stderr.puts log_entry(type, ) end |
.log_stdout(type: :info, message:) ⇒ TrueClass
Write a message to STDOUT.
43 44 45 |
# File 'lib/vedeu/logging/log.rb', line 43 def log_stdout(type: :info, message:) $stdout.puts log_entry(type, ) end |
.logger ⇒ TrueClass (private)
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 |_, _, _, | () end end end |
.message_body(type, body) ⇒ String (private)
Displays the message body using the colour specified in the last element of message_types.
105 106 107 108 109 110 |
# File 'lib/vedeu/logging/log.rb', line 105 def (type, body) Vedeu::EscapeSequences::Esc .send(.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.
117 118 119 120 121 122 |
# File 'lib/vedeu/logging/log.rb', line 117 def (type) Vedeu::EscapeSequences::Esc .send(.fetch(type, :default)[0]) do "[#{type}]".ljust(9) end end |
.message_types ⇒ Hash<Symbol => Array<Symbol>> (private)
The defined message types for Vedeu with their respective colours. When used, produces a log entry of the format:
[type]
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
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 { 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 |
.timestamp ⇒ String (private)
Returns a formatted timestamp. eg. [137.7824]
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/vedeu/logging/log.rb', line 169 def @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 |