Module: RDF::Util::Logger
- Included in:
- NTriples::Reader, NTriples::Writer, Reader, Vocabulary::Writer, Writer
- Defined in:
- lib/rdf/util/logger.rb
Overview
Helpers for logging errors, warnings and debug information.
Modules must provide ‘@logger`, which returns an instance of `Logger`, or something responding to `#<<`. Logger may also be specified using an `@options` hash containing a `:logger` entry.
Defined Under Namespace
Modules: LoggerBehavior
Constant Summary collapse
- IOWrapper =
The IOWrapper class is used to store per-logger state while wrapping an IO such as $stderr.
DelegateClass(IO)
Instance Method Summary collapse
-
#log_debug(*args, **options, &block) ⇒ void
Debug message.
- #log_depth(depth: 1, **options, &block) ⇒ Object
-
#log_error(*args, **options, &block) ⇒ void
Used for non-fatal errors where processing can continue.
-
#log_fatal(*args, **options, &block) ⇒ void
Used for fatal errors where processing cannot continue.
-
#log_info(*args, **options, &block) ⇒ void
Informational message.
-
#log_recover(*args, **options, &block) ⇒ void
Recovers from an error condition.
-
#log_recovering?(**options) ⇒ Boolean
In recovery mode? When ‘log_error` is called, we enter recovery mode.
-
#log_statistics(**options) ⇒ Hash{Symbol => Integer}
Number of times logger has been called at each level.
-
#log_warn(*args, **options, &block) ⇒ void
Warning message.
- #logger(logger: nil, **options) ⇒ Logger, ...
Instance Method Details
#log_debug(*args, **options, &block) ⇒ void
Debug message.
179 180 181 |
# File 'lib/rdf/util/logger.rb', line 179 def log_debug(*args, level: :debug, **, &block) logger_common(*args, level: level, **, &block) end |
#log_depth(depth: 1, **options) { ... } ⇒ Object #log_depth ⇒ Integer
197 198 199 |
# File 'lib/rdf/util/logger.rb', line 197 def log_depth(depth: 1, **, &block) self.logger(**).log_depth(depth: depth, &block) end |
#log_error(*args, **options, &block) ⇒ void
Used for non-fatal errors where processing can continue. If ‘logger` is not configured, it logs to `$stderr`.
As a side-effect of setting ‘@logger_in_error`, which will suppress further error messages until cleared using #log_recover.
89 90 91 92 93 94 95 |
# File 'lib/rdf/util/logger.rb', line 89 def log_error(*args, level: :error, **, &block) logger = self.logger(**) return if logger.recovering logger.recovering = true logger_common(*args, level: level, **, &block) raise [:exception], args.first if [:exception] end |
#log_fatal(*args, **options, &block) ⇒ void
Used for fatal errors where processing cannot continue. If ‘logger` is not configured, it logs to `$stderr`.
64 65 66 67 |
# File 'lib/rdf/util/logger.rb', line 64 def log_fatal(*args, level: :fatal, **, &block) logger_common(*args, "Called from #{Gem.location_of_caller.join(':')}", level: level, **, &block) raise .fetch(:exception, StandardError), args.first end |
#log_info(*args, **options, &block) ⇒ void
Informational message.
161 162 163 |
# File 'lib/rdf/util/logger.rb', line 161 def log_info(*args, level: :info, **, &block) logger_common(*args, level: level, **, &block) end |
#log_recover(*args, **options, &block) ⇒ void
Recovers from an error condition. If ‘args` are passed, sent as an informational message
As a side-effect of clearing ‘@logger_in_error`.
140 141 142 143 144 145 |
# File 'lib/rdf/util/logger.rb', line 140 def log_recover(*args, level: :info, **, &block) logger = self.logger(**) logger.recovering = false return if args.empty? && !block_given? logger_common(*args, level: level, **, &block) end |
#log_recovering?(**options) ⇒ Boolean
In recovery mode? When ‘log_error` is called, we enter recovery mode. This is cleared when `log_recover` is called.
101 102 103 |
# File 'lib/rdf/util/logger.rb', line 101 def log_recovering?(**) self.logger(**).recovering end |
#log_statistics(**options) ⇒ Hash{Symbol => Integer}
Number of times logger has been called at each level
43 44 45 |
# File 'lib/rdf/util/logger.rb', line 43 def log_statistics(**) logger(**).log_statistics end |
#log_warn(*args, **options, &block) ⇒ void
Warning message.
120 121 122 |
# File 'lib/rdf/util/logger.rb', line 120 def log_warn(*args, level: :warn, **, &block) logger_common(*args, level: level, **, &block) end |
#logger(logger: nil, **options) ⇒ Logger, ...
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rdf/util/logger.rb', line 20 def logger(logger: nil, **) # Guard against undefined instance variables, which may be a warning if used. @options = {} unless instance_variable_defined?(:@options) || frozen? logger ||= @logger if instance_variable_defined?(:@logger) logger = @options[:logger] if logger.nil? && instance_variable_defined?(:@options) && @options if logger.nil? # Unless otherwise specified, use $stderr logger = IOWrapper.new($stderr) # Reset log_statistics so that it's not inherited across different instances logger.log_statistics.clear if logger.respond_to?(:log_statistics) end logger = ::Logger.new(::File.open(::File::NULL, "w")) unless logger # Incase false was used, which is frozen @options[:logger] ||= logger if instance_variable_defined?(:@options) logger.extend(LoggerBehavior) unless logger.is_a?(LoggerBehavior) logger end |