Class: DuckMap::Logger

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

Overview

Logger class generates logging information in log/duckmap.log

Constant Summary collapse

CLEAR =
"\e[0m"
BOLD =
"\e[1m"
BLACK =

Colors

"\e[30m"
RED =
"\e[31m"
GREEN =
"\e[32m"
YELLOW =
"\e[33m"
BLUE =
"\e[34m"
MAGENTA =
"\e[35m"
CYAN =
"\e[36m"
WHITE =
"\e[37m"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.full_exceptionObject



85
86
87
88
89
90
# File 'lib/duck_map/logger.rb', line 85

def self.full_exception
  unless defined?(@@full_exception)
    @@full_exception = false
  end
  return @@full_exception
end

.full_exception=(value) ⇒ Object



75
76
77
78
# File 'lib/duck_map/logger.rb', line 75

def self.full_exception=(value)
  @@full_exception = value
  return @@full_exception
end

.is_show_console?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/duck_map/logger.rb', line 117

def self.is_show_console?
  return self.show_console
end

.log_levelNumber

Gets the current logging level.

Returns:

  • (Number)


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

def self.log_level
  @@log_level ||= self.logger.level
  return @@log_level
end

.log_level=(key) ⇒ Number

Sets the logging level.

Parameters:

  • key (Symbol, Number)

    A value representing the desired logging level. Valid values: :debug, :info, :warn, :error, :fatal, :unknown DEBUG, INFO, WARN, ERROR, FATAL, UNKNOWN

Returns:

  • (Number)


48
49
50
51
52
53
54
55
56
# File 'lib/duck_map/logger.rb', line 48

def self.log_level=(key)
  value = self.to_severity(key)

  unless value.blank?
    @@log_level = value
    self.logger.level = value
  end
  return self.logger.level
end

.loggerObject

Instance of DuckMap::Logger



21
22
23
24
# File 'lib/duck_map/logger.rb', line 21

def self.logger
  dir = defined?(Rails) ? Rails.root : "."
  return @@logger ||= self.new("#{dir}/log/duckmap.log", INFO)
end

.show_consoleObject



106
107
108
109
110
111
# File 'lib/duck_map/logger.rb', line 106

def self.show_console
  unless defined?(@@show_console)
    @@show_console = true
  end
  return @@show_console
end

.show_console=(value) ⇒ Object



113
114
115
# File 'lib/duck_map/logger.rb', line 113

def self.show_console=(value)
  @@show_console = value
end

.to_severity(key) ⇒ Object

Converts a Symbol to a valid log level and vise versa.

Returns:

  • (Object)

    The return value is based on the argument :key.

    • If you pass a Symbol, you get a log level.

    • If you pass a log level, you get a Symbol.



31
32
33
34
35
36
# File 'lib/duck_map/logger.rb', line 31

def self.to_severity(key)
  key = key.kind_of?(Fixnum) || key.kind_of?(Symbol) ? key : :badkey
  values = {debug: DEBUG, info: INFO, warn: WARN, error: ERROR, fatal: FATAL, unknown: UNKNOWN}
  value = values.map.find {|value| value[key.kind_of?(Symbol) ? 0 : 1].eql?(key)}
  return value.blank? ? nil : value[key.kind_of?(Symbol) ? 1 : 0]
end

Instance Method Details

#color(text, color, bold = false) ⇒ Object



185
186
187
188
189
# File 'lib/duck_map/logger.rb', line 185

def color(text, color, bold=false)
  color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
  bold  = bold ? BOLD : ""
  return "#{bold}#{color}#{text}#{CLEAR}"
end

#console(msg = nil, progname = nil, &block) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/duck_map/logger.rb', line 97

def console(msg = nil, progname = nil, &block)
  if self.is_show_console?
    STDOUT.puts msg
  end
  info(msg, progname, &block)
  return nil
end

#debug(msg = nil, progname = nil, &block) ⇒ Object



149
150
151
152
# File 'lib/duck_map/logger.rb', line 149

def debug(msg = nil, progname = nil, &block)
  add(DEBUG, color("#{msg}", CYAN), progname, &block)
  return nil
end

#error(msg = nil, progname = nil, &block) ⇒ Object



155
156
157
158
# File 'lib/duck_map/logger.rb', line 155

def error(msg = nil, progname = nil, &block)
  add(ERROR, color("#{msg}", RED), progname, &block)
  return nil
end

#exception(exception, progname = nil, &block) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/duck_map/logger.rb', line 134

def exception(exception, progname = nil, &block)

  self.error(exception.message, progname, &block)

  base_dir = Rails.root.to_s
  exception.backtrace.each do |x|
    if x.include?(base_dir) || self.full_exception
      self.error(x, progname, &block)
    end
  end

  return nil
end

#fatal(msg = nil, progname = nil, &block) ⇒ Object



161
162
163
164
# File 'lib/duck_map/logger.rb', line 161

def fatal(msg = nil, progname = nil, &block)
  add(FATAL, color("#{msg}", RED), progname, &block)
  return nil
end

#full_exceptionObject



92
93
94
# File 'lib/duck_map/logger.rb', line 92

def full_exception
  return self.class.full_exception
end

#full_exception=(value) ⇒ Object



80
81
82
# File 'lib/duck_map/logger.rb', line 80

def full_exception=(value)
  self.class.full_exception = value
end

#info(msg = nil, progname = nil, &block) ⇒ Object



167
168
169
170
# File 'lib/duck_map/logger.rb', line 167

def info(msg = nil, progname = nil, &block)
  add(INFO, "#{msg}", progname, &block)
  return nil
end

#is_show_console?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/duck_map/logger.rb', line 129

def is_show_console?
  return self.class.is_show_console?
end

#log_levelObject



70
71
72
# File 'lib/duck_map/logger.rb', line 70

def log_level
  return self.class.log_level
end

#log_level=(key) ⇒ Object



58
59
60
# File 'lib/duck_map/logger.rb', line 58

def log_level=(key)
  self.class.log_level = key
end

#show_consoleObject



121
122
123
# File 'lib/duck_map/logger.rb', line 121

def show_console
  return self.class.show_console
end

#show_console=(value) ⇒ Object



125
126
127
# File 'lib/duck_map/logger.rb', line 125

def show_console=(value)
  self.class.show_console = value
end

#to_severity(key) ⇒ Object



38
39
40
# File 'lib/duck_map/logger.rb', line 38

def to_severity(key)
  return self.class.to_severity(key)
end

#unknown(msg = nil, progname = nil, &block) ⇒ Object



179
180
181
182
# File 'lib/duck_map/logger.rb', line 179

def unknown(msg = nil, progname = nil, &block)
  add(INFO, color("#{msg}", WHITE), progname, &block)
  return nil
end

#warn(msg = nil, progname = nil, &block) ⇒ Object



173
174
175
176
# File 'lib/duck_map/logger.rb', line 173

def warn(msg = nil, progname = nil, &block)
  add(WARN, color("#{msg}", YELLOW), progname, &block)
  return nil
end