Class: Gloo::App::Log

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#loggerObject (readonly)

Returns the value of attribute logger.



26
27
28
# File 'lib/gloo/app/log.rb', line 26

def logger
  @logger
end

#quietObject

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?

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/gloo/app/log.rb', line 84

def self.is_level? str
  return false unless str.is_a? String
  return LEVELS.include? str.strip.downcase
end

Instance Method Details

#clearObject

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_loggersObject

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.



143
144
145
146
147
# File 'lib/gloo/app/log.rb', line 143

def debug( msg )
  return unless @debug

  @logger.debug msg
end

#err_fileObject

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.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/gloo/app/log.rb', line 172

def error( msg, ex = nil, engine = nil )
  engine&.heap&.error&.set_to msg
  @logger.error msg
  @error.error msg
  if ex
    @error.error ex.message
    @error.error ex.backtrace
    puts msg.red unless @quiet
    puts ex.message.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.



152
153
154
155
# File 'lib/gloo/app/log.rb', line 152

def info( msg )
  @logger.info msg
  # puts msg.blue unless @quiet
end

#log_fileObject

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_serializeObject

Prepare for serialization by removing the file reference. Without this, the engine cannot be serialized.



195
196
197
# File 'lib/gloo/app/log.rb', line 195

def prep_serialize
  @logger = nil
end

#restore_after_deserializationObject

Restore the logger after deserialization.



202
203
204
# File 'lib/gloo/app/log.rb', line 202

def restore_after_deserialization
  create_loggers
end

#show(msg, color = nil) ⇒ Object

Show a message unless we’re in quite mode.



111
112
113
114
115
116
117
118
119
# File 'lib/gloo/app/log.rb', line 111

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.



161
162
163
164
165
# File 'lib/gloo/app/log.rb', line 161

def warn( msg )
  @logger.warn msg
  @error.warn msg
  puts msg.yellow unless @quiet
end

#write(msg, level) ⇒ Object

Write to the specified level.



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/gloo/app/log.rb', line 128

def write( msg, level )
  if level == DEBUG
    debug msg
  elsif level == INFO
    info msg
  elsif level == WARN
    warn msg
  elsif level == ERROR
    error msg
  end
end