Class: Gloo::App::Log
- Inherits:
-
Object
- Object
- Gloo::App::Log
- Defined in:
- lib/gloo/app/log.rb
Constant Summary collapse
- DEBUG =
'debug'.freeze
- INFO =
'info'.freeze
- WARN =
'warn'.freeze
- ERROR =
'error'.freeze
- LEVELS =
[ DEBUG, INFO, WARN, ERROR ].freeze
- CLEARED =
"\n\n --- Log files cleared. --- \n\n".freeze
- LOG_FILE =
'gloo.log'.freeze
- ERROR_FILE =
'error.log'.freeze
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#quiet ⇒ Object
Returns the value of attribute quiet.
Class Method Summary collapse
-
.is_level?(str) ⇒ Boolean
Does the given str represent a logging level?.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear the log files.
-
#create_loggers ⇒ Object
Create the default [file] logger.
-
#debug(msg) ⇒ Object
Write a debug message to the log.
-
#err_file ⇒ Object
Get the error log file.
-
#error(msg, ex = nil, engine = nil) ⇒ Object
Write an error message to the log and set the error in the engine’s data heap.
-
#info(msg) ⇒ Object
Write an information message to the log.
-
#initialize(engine, quiet = true) ⇒ Log
constructor
Set up a logger.
-
#log_file ⇒ Object
Get the log file.
-
#prep_serialize ⇒ Object
Prepare for serialization by removing the file reference.
-
#restore_after_deserialization ⇒ Object
Restore the logger after deserialization.
-
#show(msg, color = nil) ⇒ Object
Show a message unless we’re in quite mode.
-
#warn(msg) ⇒ Object
Write a warning message to the log.
-
#write(msg, level) ⇒ Object
Write to the specified level.
Constructor Details
#initialize(engine, quiet = true) ⇒ Log
Set up a logger. If quiet is true, then message are written to the log but not to the console.
37 38 39 40 41 42 43 44 45 |
# File 'lib/gloo/app/log.rb', line 37 def initialize( engine, quiet = true ) @engine = engine @quite = quiet @debug = engine.settings.debug create_loggers debug 'log intialized...' end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
26 27 28 |
# File 'lib/gloo/app/log.rb', line 26 def logger @logger end |
#quiet ⇒ Object
Returns the value of attribute quiet.
25 26 27 |
# File 'lib/gloo/app/log.rb', line 25 def quiet @quiet end |
Class Method Details
.is_level?(str) ⇒ Boolean
Does the given str represent a logging level?
83 84 85 86 87 |
# File 'lib/gloo/app/log.rb', line 83 def self.is_level?( str ) return false unless str.is_a? String return LEVELS.include? str.strip.downcase end |
Instance Method Details
#clear ⇒ Object
Clear the log files.
96 97 98 99 100 101 |
# File 'lib/gloo/app/log.rb', line 96 def clear File.write( log_file, CLEARED ) File.write( err_file, CLEARED ) create_loggers end |
#create_loggers ⇒ Object
Create the default [file] logger.
50 51 52 53 54 55 56 |
# File 'lib/gloo/app/log.rb', line 50 def create_loggers @logger = Logger.new( log_file ) @logger.level = Logger::DEBUG @error = Logger.new( err_file ) @error.level = Logger::WARN end |
#debug(msg) ⇒ Object
Write a debug message to the log.
142 143 144 145 146 |
# File 'lib/gloo/app/log.rb', line 142 def debug( msg ) return unless @debug @logger.debug msg end |
#err_file ⇒ Object
Get the error log file.
72 73 74 |
# File 'lib/gloo/app/log.rb', line 72 def err_file return File.join( @engine.settings.log_path, ERROR_FILE ) end |
#error(msg, ex = nil, engine = nil) ⇒ Object
Write an error message to the log and set the error in the engine’s data heap. Also write to the console unless quiet.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/gloo/app/log.rb', line 171 def error( msg, ex = nil, engine = nil ) engine&.heap&.error&.set_to msg @logger.error msg @error.error msg if ex @error.error ex. @error.error ex.backtrace puts msg.red unless @quiet puts ex..red unless @quiet puts ex.backtrace unless @quiet else puts msg.red unless @quiet end end |
#info(msg) ⇒ Object
Write an information message to the log.
151 152 153 154 |
# File 'lib/gloo/app/log.rb', line 151 def info( msg ) @logger.info msg # puts msg.blue unless @quiet end |
#log_file ⇒ Object
Get the log file.
65 66 67 |
# File 'lib/gloo/app/log.rb', line 65 def log_file return File.join( @engine.settings.log_path, LOG_FILE ) end |
#prep_serialize ⇒ Object
Prepare for serialization by removing the file reference. Without this, the engine cannot be serialized.
194 195 196 |
# File 'lib/gloo/app/log.rb', line 194 def prep_serialize @logger = nil end |
#restore_after_deserialization ⇒ Object
Restore the logger after deserialization.
201 202 203 |
# File 'lib/gloo/app/log.rb', line 201 def restore_after_deserialization create_loggers end |
#show(msg, color = nil) ⇒ Object
Show a message unless we’re in quite mode.
110 111 112 113 114 115 116 117 118 |
# File 'lib/gloo/app/log.rb', line 110 def show( msg, color = nil ) return if @quiet if color puts ColorizedString[ msg ].colorize( color.to_sym ) else puts msg end end |
#warn(msg) ⇒ Object
Write a warning message to the log. Also write to the console unless quiet.
160 161 162 163 164 |
# File 'lib/gloo/app/log.rb', line 160 def warn( msg ) @logger.warn msg @error.warn msg puts msg.yellow unless @quiet end |