Class: Resat::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/log.rb

Constant Summary collapse

LEVELS =
%w{ debug info warn error fatal }

Class Method Summary collapse

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(options)
  File.delete(options.logfile) rescue nil
  if options.dry_run
    options.logfile = STDOUT
  else
    options.logfile = 'resat.log' unless File.directory?(File.dirname(options.logfile))
  end
  @logger = Logger.new(options.logfile)
  @logger.formatter = LogFormatter.new
  @level = LEVELS.index(options.loglevel.downcase) if options.loglevel
  @level = Logger::WARN unless @level # default to warning
  @logger.level = @level
  @verbose = options.verbose
  @quiet = options.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.message}"
  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