Class: IronNails::Logging::BufferedLogger
- Inherits:
-
Object
- Object
- IronNails::Logging::BufferedLogger
- Includes:
- Severity
- Defined in:
- lib/ironnails/logging/buffered_logger.rb
Defined Under Namespace
Modules: Severity
Constant Summary collapse
- MAX_BUFFER_SIZE =
1000
Constants included from Severity
Severity::DEBUG, Severity::ERROR, Severity::FATAL, Severity::INFO, Severity::UNKNOWN, Severity::WARN
Instance Attribute Summary collapse
-
#auto_flushing ⇒ Object
Returns the value of attribute auto_flushing.
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#level ⇒ Object
Returns the value of attribute level.
Instance Method Summary collapse
- #add(severity, message = nil, progname = nil, &block) ⇒ Object
- #close ⇒ Object
- #flush ⇒ Object
-
#initialize(log, level = DEBUG) ⇒ BufferedLogger
constructor
A new instance of BufferedLogger.
- #set_non_blocking_io ⇒ Object
-
#silence(temporary_level = ERROR) ⇒ Object
Silences the logger for the duration of the block.
Constructor Details
#initialize(log, level = DEBUG) ⇒ BufferedLogger
Returns a new instance of BufferedLogger.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ironnails/logging/buffered_logger.rb', line 43 def initialize(log, level = DEBUG) @level = level @buffer = [] @auto_flushing = 1 @no_block = false if log.respond_to?(:write) @log = log elsif File.exist?(log) @log = File.open(log, (File::WRONLY | File::APPEND)) @log.sync = true else FileUtils.mkdir_p(File.dirname(log)) @log = File.open(log, (File::WRONLY | File::APPEND | File::CREAT)) @log.sync = true @log.write("# Logfile created on %s" % [Time.now.to_s]) end end |
Instance Attribute Details
#auto_flushing ⇒ Object
Returns the value of attribute auto_flushing.
40 41 42 |
# File 'lib/ironnails/logging/buffered_logger.rb', line 40 def auto_flushing @auto_flushing end |
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
41 42 43 |
# File 'lib/ironnails/logging/buffered_logger.rb', line 41 def buffer @buffer end |
#level ⇒ Object
Returns the value of attribute level.
39 40 41 |
# File 'lib/ironnails/logging/buffered_logger.rb', line 39 def level @level end |
Instance Method Details
#add(severity, message = nil, progname = nil, &block) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ironnails/logging/buffered_logger.rb', line 68 def add(severity, = nil, progname = nil, &block) from_framework = progname == IRONNAILS_FRAMEWORKNAME fw_logging = defined?(IronNails::Logging::FRAMEWORK_LOGGING) && IronNails::Logging::FRAMEWORK_LOGGING return if @level > severity || (from_framework && !fw_logging ) = ( || (block && block.call) || progname).to_s puts if defined?(IronNails::Logging::CONSOLE_LOGGING) && IronNails::Logging::CONSOLE_LOGGING # If a newline is necessary then create a new message ending with a newline. # Ensures that the original message is not mutated. = "IRONNAILS: #{Severity.constants[severity]}: #{}" if from_framework = "[#{Time.now.strftime("%d/%m/%Y %H:%M:%S.#{Time.now.usec}")}] #{}\n" unless [-1] == ?\n buffer << auto_flush end |
#close ⇒ Object
123 124 125 126 127 |
# File 'lib/ironnails/logging/buffered_logger.rb', line 123 def close flush @log.close if @log.respond_to?(:close) @log = nil end |
#flush ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/ironnails/logging/buffered_logger.rb', line 113 def flush unless buffer.empty? if @no_block @log.write_nonblock(buffer.slice!(0..-1).join) else @log.write(buffer.slice!(0..-1).join) end end end |
#set_non_blocking_io ⇒ Object
62 63 64 65 66 |
# File 'lib/ironnails/logging/buffered_logger.rb', line 62 def set_non_blocking_io if !RUBY_PLATFORM.match(/java|mswin/) && !(@log == STDOUT) && @log.respond_to?(:write_nonblock) @no_block = true end end |
#silence(temporary_level = ERROR) ⇒ Object
Silences the logger for the duration of the block.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ironnails/logging/buffered_logger.rb', line 26 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 |