lib/lorekeeper/fast_logger.rb in lorekeeper-2.3.1 vs lib/lorekeeper/fast_logger.rb in lorekeeper-2.3.2

- old
+ new

@@ -3,13 +3,13 @@ require 'logger' module Lorekeeper # Very simple, very fast logger class FastLogger - include ::Logger::Severity # contains the levels constants: DEBUG, ERROR, etc. - attr_accessor :level # Current level, default: DEBUG - attr_accessor :formatter # Just for compatibility with Logger, not used + include ::Logger::Severity # contains the levels constants: DEBUG, ERROR, etc. + attr_accessor :level, # Current level, default: DEBUG + :formatter # Just for compatibility with Logger, not used def debug?; level <= DEBUG; end def info?; level <= INFO; end def warn?; level <= WARN; end def error?; level <= ERROR; end @@ -19,16 +19,16 @@ @level = DEBUG @iodevice = LogDevice.new(file) @file = file # We only keep this so we can inspect where we are sending the logs end - LOGGING_METHODS = [ - :debug, - :info, - :warn, - :error, - :fatal + LOGGING_METHODS = %i[ + debug + info + warn + error + fatal ].freeze METHOD_SEVERITY_MAP = { debug: DEBUG, info: INFO, @@ -54,25 +54,26 @@ end # This is part of the standard Logger API, we need this to be compatible def add(severity, message_param = nil, progname = nil, &block) return true if severity < @level - message = (block && block.call) || message_param || progname + + message = block&.call || message_param || progname log_data(severity, message.freeze) end # Some gems like to add this method. For instance: # https://github.com/rails/activerecord-session_store # To avoid needing to monkey-patch Lorekeeper just to get this method, we are adding a simple # non-functional version here. def silence_logger(&block) - yield if block_given? + yield if block end # activerecord-session_store v2 is now simply calling silence instead of silence_logger def silence(&block) - yield if block_given? + yield if block end # inherited classes probably want to reimplement this def log_data(_severity, message) write(message) @@ -80,12 +81,10 @@ def write(message) @iodevice.write(message) end - private - require 'monitor' # Mutex to avoid broken lines when multiple threads access the log file class LogDeviceMutex include MonitorMixin end @@ -108,15 +107,16 @@ private def to_iodevice(file) return nil unless file - iodevice = if file.respond_to?(:write) && file.respond_to?(:close) - file - else - open_logfile(file) - end + iodevice = + if file.respond_to?(:write) && file.respond_to?(:close) + file + else + open_logfile(file) + end iodevice.sync = true if iodevice.respond_to?(:sync=) iodevice end @@ -130,7 +130,9 @@ File.open(filename, (File::WRONLY | File::APPEND | File::CREAT)) rescue Errno::EEXIST open_logfile(filename) end end + + private_constant :LogDeviceMutex, :LogDevice end end