Class: RubyExts::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/rubyexts/logger.rb

Instance Method Summary collapse

Constructor Details

#initialize(output = STDERR) ⇒ Logger

Returns a new instance of Logger.

Since:

  • 0.0.1



11
12
13
14
# File 'lib/rubyexts/logger.rb', line 11

def initialize(output = STDERR)
  super(output)
  self.setup
end

Instance Method Details

#debug(*args) ⇒ Object

Logger methods can takes more arguments and they returns array with arguments NOTE: why we dup args each time? Because we colorize messages, but if logger method is used for handling requests, we just need to send noncolored messages as response.

Since:

  • 0.0.1



37
38
39
40
41
# File 'lib/rubyexts/logger.rb', line 37

def debug(*args)
  original = args.dup
  args.map { |arg| super(arg) }
  return original
end

#error(*args) ⇒ Object

Since:

  • 0.0.1



58
59
60
61
62
# File 'lib/rubyexts/logger.rb', line 58

def error(*args)
  original = args.dup
  args.map { |arg| super(arg) }
  return original
end

#exception(exception) ⇒ Object

Since:

  • 0.0.1



24
25
26
27
28
29
30
31
# File 'lib/rubyexts/logger.rb', line 24

def exception(exception)
  self.error("#{exception.message} (#{exception.class})")
  exception.backtrace.each do |line|
    unless line.match(/thin|eventmachine/)
      STDERR.puts("- #{line.colorize.cyan}")
    end
  end
end

#fatal(*args) ⇒ Object

Since:

  • 0.0.1



65
66
67
68
69
# File 'lib/rubyexts/logger.rb', line 65

def fatal(*args)
  original = args.dup
  args.map { |arg| super(arg) }
  return original
end

#info(*args) ⇒ Object

Since:

  • 0.0.1



44
45
46
47
48
# File 'lib/rubyexts/logger.rb', line 44

def info(*args)
  original = args.dup
  args.map { |arg| super(arg) }
  return original
end

#inspect(*args) ⇒ Object

Project.logger.inspect(@posts, item) Project.logger.inspect(“@post” => @post)

Since:

  • 0.0.1



74
75
76
77
78
79
80
81
82
83
# File 'lib/rubyexts/logger.rb', line 74

def inspect(*args)
  if args.first.is_a?(Hash) && args.length.eql?(1)
    args.first.each do |name, value|
      self.debug("#{name}: #{value.inspect}")
    end
  else
    args = args.map { |arg| arg.inspect }
    self.debug(*args)
  end
end

#setupObject

Since:

  • 0.0.1



17
18
19
20
21
# File 'lib/rubyexts/logger.rb', line 17

def setup
  self.level = ::Logger::DEBUG
  self.datetime_format = "%H:%M:%S"
  self.setup_format
end

#setup_formatObject

Since:

  • 0.0.1



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rubyexts/logger.rb', line 86

def setup_format
  self.formatter = lambda do |severity, datetime, progname, msg|
    # logger.debug(object_to_inspect)
    msg = msg.inspect unless msg.is_a?(String)
    datetime = datetime.strftime("[%H:%M:%S]")
    case severity
    when "DEBUG" then "#{datetime} #{msg.colorize.yellow}\n"
    when "INFO"  then "#{datetime} #{msg.colorize.green}\n"
    when "WARN"  then "#{datetime} #{msg.colorize.yellow}\n"
    when "ERROR" then "#{datetime} #{msg.colorize.red}\n"
    when "FATAL" then "#{datetime} #{msg.colorize.red.bold}\n"
    end
  end
end

#warn(*args) ⇒ Object

Since:

  • 0.0.1



51
52
53
54
55
# File 'lib/rubyexts/logger.rb', line 51

def warn(*args)
  original = args.dup
  args.map { |arg| super(arg) }
  return original
end