Module: Bauk::Utils::Log
- Included in:
- BaseParser
- Defined in:
- lib/bauk/utils/log.rb
Overview
This mixin ia a wrapper around log4r and enables logging functionality. It auto-names loggers in relation to the class they are included in,
unless a name is specified.
Constant Summary collapse
- @@loggers_list =
Stores the list of created loggers
nil
Class Method Summary collapse
-
.check_level(level) ⇒ Object
FATAL = 5, DEBUG = 1, so high level is less logging…
-
.create(name) ⇒ Object
Creates a new Log4r instance with the provided name Unless one already exists, in which case it returns it It also adds it to the list of loggers for future verboseness changing.
-
.decrease_logging(name = '') ⇒ Object
Decreases logging from all loggers, unless a name is specified (See set_log_level for runes on name provided).
-
.increase_logging(name = '') ⇒ Object
Increases logging from all loggers, unless a name is specified (See set_log_level for runes on name provided).
-
.log_level(name = '') ⇒ Object
Returns the log_level of the provided logger If no name provided, returns the default log level.
-
.logger_name(name) ⇒ Object
Function to turn a name into a more valid logger name e.g.
- .loggers ⇒ Object
-
.set_log_level(level, name = '', add = false) ⇒ Object
Sets the log level for all loggers matching the name.
Instance Method Summary collapse
-
#decrease_logging ⇒ Object
Decreases logging globally for the name of this logger.
-
#increase_logging ⇒ Object
Increases logging globally for the name of this logger.
-
#log ⇒ Object
Used to access log4r inside a class which includes this class.
-
#log_level=(level) ⇒ Object
Setts the log level of this logger only.
-
#test_logging ⇒ Object
Prints a variety of log messages at each log level.
-
#title(string) ⇒ Object
Prints the designated title to the screen with an underline.
Class Method Details
.check_level(level) ⇒ Object
FATAL = 5, DEBUG = 1, so high level is less logging…
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/bauk/utils/log.rb', line 92 def self.check_level(level) if level > Log4r::FATAL puts "Log level #{level} too high, setting to max(#{Log4r::FATAL})" level = Log4r::FATAL elsif level < Log4r::DEBUG puts "Log level #{level} too low, setting to min (#{Log4r::DEBUG})" level = Log4r::DEBUG end level end |
.create(name) ⇒ Object
Creates a new Log4r instance with the provided name Unless one already exists, in which case it returns it It also adds it to the list of loggers for future verboseness changing
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/bauk/utils/log.rb', line 15 def self.create(name) log = loggers[name][:instance] if log.nil? log = Log4r::Logger.new(logger_name name) log.level = Log.loggers[name][:level] Log.loggers[name][:outputters].each { |o| log.add o } log.debug("Initialized loger #{name} to log level: #{log.level}") loggers[name] = loggers[name].clone # Take default values loggers[name][:instance] = log end log end |
.decrease_logging(name = '') ⇒ Object
Decreases logging from all loggers, unless a name is specified (See set_log_level for runes on name provided).
Due to the inverse of log4r, less logging means a higher log level
49 50 51 |
# File 'lib/bauk/utils/log.rb', line 49 def self.decrease_logging(name = '') set_log_level(1, name, true) end |
.increase_logging(name = '') ⇒ Object
Increases logging from all loggers, unless a name is specified (See set_log_level for runes on name provided).
Due to the inverse of log4r, more logging means a lower log level
42 43 44 |
# File 'lib/bauk/utils/log.rb', line 42 def self.increase_logging(name = '') set_log_level(-1, name, true) end |
.log_level(name = '') ⇒ Object
Returns the log_level of the provided logger If no name provided, returns the default log level
78 79 80 |
# File 'lib/bauk/utils/log.rb', line 78 def self.log_level(name = '') loggers[name][:level] end |
.logger_name(name) ⇒ Object
Function to turn a name into a more valid logger name e.g. The::Best::Class -> TB.class
30 31 32 33 34 35 36 37 |
# File 'lib/bauk/utils/log.rb', line 30 def self.logger_name(name) logger_parts = name.split '::' logger_name = logger_parts.pop unless logger_parts.empty? logger_name = [logger_parts.map {|p| p[0]}.join(''), logger_name].join('.') end logger_name end |
.loggers ⇒ Object
85 86 87 88 89 |
# File 'lib/bauk/utils/log.rb', line 85 def self.loggers @@loggers_list ||= Hash.new(level: Log4r::WARN, outputters: [Log4r::Outputter.stderr]) @@loggers_list end |
.set_log_level(level, name = '', add = false) ⇒ Object
Sets the log level for all loggers matching the name. If the add flag is set, it increases/decreases the log level by the specified amount. The name is as follows:
- ”
-
Blank to edit all loggers
- logger_name
-
To edit that logger and any child loggers
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/bauk/utils/log.rb', line 58 def self.set_log_level(level, name = '', add = false) level += loggers[name][:level] if add level = check_level(level) if name != '' # In case not set yet, set the logger to default values # (If name = false, we want to edit the default directly) loggers[name] = loggers[name].clone end loggers[name][:level] = level # Change all active Child loggers loggers.each do |n, logger| next unless n =~ /^#{name}(::.*)*$/ || name == '' logger[:level] = level logger[:instance].level = level if logger[:instance] end end |
Instance Method Details
#decrease_logging ⇒ Object
Decreases logging globally for the name of this logger. See ::decrease_logging
131 132 133 |
# File 'lib/bauk/utils/log.rb', line 131 def decrease_logging Log.decrease_logging(log.name) end |
#increase_logging ⇒ Object
Increases logging globally for the name of this logger. See ::increase_logging
125 126 127 |
# File 'lib/bauk/utils/log.rb', line 125 def increase_logging Log.increase_logging(log.name) end |
#log ⇒ Object
Used to access log4r inside a class which includes this class
106 107 108 109 110 111 112 113 |
# File 'lib/bauk/utils/log.rb', line 106 def log if @log.nil? logger_name = self.class.name logger_name = "#{self.name}@" if self.respond_to? "name" @log = Log.create(logger_name || 'anonymous') if @log.nil? end @log end |
#log_level=(level) ⇒ Object
Setts the log level of this logger only
136 137 138 |
# File 'lib/bauk/utils/log.rb', line 136 def log_level=(level) log.level = Log.check_level(level) end |
#test_logging ⇒ Object
Prints a variety of log messages at each log level.
141 142 143 144 145 146 147 |
# File 'lib/bauk/utils/log.rb', line 141 def test_logging log.debug 'DEBUG' log.info 'INFO' log.warn 'WARN' log.error 'ERROR' log.fatal 'FATAL' end |
#title(string) ⇒ Object
Prints the designated title to the screen with an underline
116 117 118 119 120 121 |
# File 'lib/bauk/utils/log.rb', line 116 def title(string) space = 4 overhang = 1 puts ' ' * space + string puts((' ' * space + string.gsub(/./, '=')).sub(' ' * overhang + '=', '=' * overhang + '=').sub(/$/, '=' * overhang)) end |