Class: Resat::Log
- Inherits:
-
Object
- Object
- Resat::Log
- Defined in:
- lib/log.rb
Constant Summary collapse
- LEVELS =
%w{ debug info warn error fatal }
Class Method Summary collapse
- .debug(debug) ⇒ Object
- .error(error) ⇒ Object
- .fatal(fatal) ⇒ Object
- .info(info) ⇒ Object
-
.init(options) ⇒ Object
Initialize singleton instance.
- .request(request) ⇒ Object
- .response(response, succeeded = true) ⇒ Object
- .warn(warning) ⇒ Object
Class Method Details
.debug(debug) ⇒ Object
60 61 62 63 |
# File 'lib/log.rb', line 60 def Log.debug(debug) @logger.debug { debug } if @logger puts "\n#{debug}".grey if @level == Logger::DEBUG end |
.error(error) ⇒ Object
75 76 77 78 |
# File 'lib/log.rb', line 75 def Log.error(error) puts "\nError: #{error}".dark_red @logger.error { error } if @logger end |
.fatal(fatal) ⇒ Object
80 81 82 83 |
# File 'lib/log.rb', line 80 def Log.fatal(fatal) puts "\nCrash: #{fatal}".red @logger.fatal { fatal } if @logger end |
.info(info) ⇒ Object
65 66 67 68 |
# File 'lib/log.rb', line 65 def Log.info(info) puts "\n#{info}".dark_green if @verbose @logger.info { info } if @logger end |
.init(options) ⇒ Object
Initialize singleton instance
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/log.rb', line 44 def Log.init() File.delete(.logfile) rescue nil if .dry_run .logfile = STDOUT else .logfile = 'resat.log' unless File.directory?(File.dirname(.logfile)) end @logger = Logger.new(.logfile) @logger.formatter = LogFormatter.new @level = LEVELS.index(.loglevel.downcase) if .loglevel @level = Logger::WARN unless @level # default to warning @logger.level = @level @verbose = .verbose @quiet = .quiet end |
.request(request) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/log.rb', line 85 def Log.request(request) msg = "REQUEST #{request.method} #{request.path}" if request.size > 0 msg << "\nheaders:" request.each_header do |name, value| msg << "\n #{name}: #{value}" end end msg << "\nbody: #{request.body}" unless request.body.nil? Log.info(msg) end |
.response(response, succeeded = true) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/log.rb', line 97 def Log.response(response, succeeded = true) msg = "RESPONSE #{response.code} #{response.}" if response.size > 0 msg << "\nheaders:" response.each_header do |name, value| msg << "\n #{name}: #{value}" end end msg << "\nbody: #{response.body}" unless response.body.nil? if succeeded Log.info(msg) else Log.warn(msg) end end |
.warn(warning) ⇒ Object
70 71 72 73 |
# File 'lib/log.rb', line 70 def Log.warn(warning) puts "\nWarning: #{warning}".yellow unless @quiet @logger.warn { warning } if @logger end |