Class: BufferedLogger
- Inherits:
-
Object
- Object
- BufferedLogger
- Includes:
- Buffering, Formatting, Indentation, Severity
- Defined in:
- lib/buffered_logger.rb,
lib/buffered_logger/logger.rb
Defined Under Namespace
Modules: Buffering, Formatting, Indentation, Severity Classes: Formatter, Padding, ThreadHash
Constant Summary collapse
- SEVERITY_LEVELS =
Severity.constants.map { |c| c.downcase.to_sym }.freeze
- SEVERITY_MAP =
Severity.constants.inject({}) do |h,c| h[c.downcase.to_sym] = Severity.const_get(c); h end.freeze
Constants included from Severity
Severity::DEBUG, Severity::ERROR, Severity::FATAL, Severity::INFO, Severity::UNKNOWN, Severity::WARN
Constants included from Buffering
Constants included from Formatting
Constants included from Indentation
Instance Attribute Summary collapse
-
#level ⇒ Object
Returns the value of attribute level.
Instance Method Summary collapse
- #add(severity, message = nil, progname = nil, &block) ⇒ Object
-
#initialize(log, level = DEBUG, params = {}) ⇒ BufferedLogger
constructor
A new instance of BufferedLogger.
- #open_log(log, mode) ⇒ Object
- #print_blank_line ⇒ Object
-
#silence(temporary_level = ERROR) ⇒ Object
Silences the logger for the duration of the block.
-
#silencer ⇒ Object
:singleton-method: Set to false to disable the silencer.
Methods included from Buffering
#auto_flushing, #auto_flushing=, #buffer_text, #close, #flush
Methods included from Formatting
#color?, #disable_color, #enable_color, #toggle_color
Methods included from Indentation
Constructor Details
#initialize(log, level = DEBUG, params = {}) ⇒ BufferedLogger
Returns a new instance of BufferedLogger.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/buffered_logger/logger.rb', line 35 def initialize(log, level = DEBUG, params = {}) @master_thread = Thread.current @level = severity_to_const(level) if log.respond_to?(:write) @log = log elsif File.exist?(log) @log = open_log(log, (File::WRONLY | File::APPEND)) else FileUtils.mkdir_p(File.dirname(log)) @log = open_log(log, (File::WRONLY | File::APPEND | File::CREAT)) end super() params.each { |k,v| SEVERITY_LEVELS.include?(k) ? set_formatter(k, v) : next } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class BufferedLogger::Formatting
Instance Attribute Details
#level ⇒ Object
Returns the value of attribute level.
31 32 33 |
# File 'lib/buffered_logger/logger.rb', line 31 def level @level end |
Instance Method Details
#add(severity, message = nil, progname = nil, &block) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/buffered_logger/logger.rb', line 57 def add(severity, = nil, progname = nil, &block) return if @level > severity = ( || (block && block.call) || progname).to_s # If a newline is necessary then create a new message ending with a newline. # Ensures that the original message is not mutated. = "#{}\n" unless [-1] == ?\n buffer << auto_flush end |
#open_log(log, mode) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/buffered_logger/logger.rb', line 50 def open_log(log, mode) open(log, mode).tap do |open_log| open_log.set_encoding(Encoding::BINARY) if open_log.respond_to?(:set_encoding) open_log.sync = true end end |
#print_blank_line ⇒ Object
81 82 83 |
# File 'lib/buffered_logger/logger.rb', line 81 def print_blank_line add(0, "\n") end |
#silence(temporary_level = ERROR) ⇒ Object
Silences the logger for the duration of the block.
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/buffered_logger/logger.rb', line 86 def silence(temporary_level = ERROR) if silencer begin old_logger_level, self.level = level, temporary_level yield self ensure self.level = old_logger_level end else yield self end end |
#silencer ⇒ Object
:singleton-method: Set to false to disable the silencer
28 |
# File 'lib/buffered_logger/logger.rb', line 28 cattr_accessor :silencer |