Module: RDF::Util::Logger

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.

Since:

  • 2.0.0

Defined Under Namespace

Modules: LoggerBehavior

Instance Method Summary collapse

Instance Method Details

#log_debug(*args, **options, &block)

Debug message.

This method returns an undefined value.

Parameters:

Options Hash (**options):

  • :depth (Integer)

    Recursion depth for indenting output

  • :lineno (Integer)

    associated with message

  • :logger (Logger, #<<)

Yield Returns:

  • (String)

    added to message

Since:

  • 2.0.0



172
173
174
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/logger.rb', line 172

def log_debug(*args, level: :debug, **options, &block)
  logger_common(*args, level: level, **options, &block)
end

#log_depth(options) { ... } ⇒ Object #log_depthInteger

Overloads:

  • #log_depth(options) { ... } ⇒ Object

    Increase depth around a method invocation

    Parameters:

    Options Hash (options):

    • :depth (Integer)

      Additional recursion depth

    • :logger (Logger, #<<)

    Yields:

    • Yields with no arguments

    Yield Returns:

    • (Object)

      returns the result of yielding

    Returns:

  • #log_depthInteger

    Return the current log depth

    Returns:

Since:

  • 2.0.0



190
191
192
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/logger.rb', line 190

def log_depth(**options, &block)
  self.logger(options).log_depth(&block)
end

#log_error(*args, **options, &block)

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.

This method returns an undefined value.

Parameters:

Options Hash (**options):

  • :depth (Integer)

    Recursion depth for indenting output

  • level (:fatal, :error, :warn, :info, :debug) — default: :<<
  • :lineno (Integer)

    associated with message

  • :logger (Logger, #<<)
  • :exception, (Class) — default: StandardError

    Exception class used for raising error

Yield Returns:

  • (String)

    added to message

Raises:

  • Raises the provided exception class using the first element from args as the message component, if :exception option is provided.

Raises:

  • (options[:exception])

Since:

  • 2.0.0



82
83
84
85
86
87
88
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/logger.rb', line 82

def log_error(*args, level: :error, **options, &block)
  logger = self.logger(options)
  return if logger.recovering
  logger.recovering = true
  logger_common(*args, level: level, **options, &block)
  raise options[:exception], args.first if options[:exception]
end

#log_fatal(*args, **options, &block)

Used for fatal errors where processing cannot continue. If logger is not configured, it logs to $stderr.

This method returns an undefined value.

Parameters:

Options Hash (**options):

  • :depth (Integer)

    Recursion depth for indenting output

  • :lineno (Integer)

    associated with message

  • :logger (Logger, #<<)
  • :exception, (Class) — default: StandardError

    Exception class used for raising error

Yield Returns:

  • (String)

    added to message

Raises:

  • Raises the provided exception class using the first element from args as the message component.

Raises:

  • (options.fetch(:exception, StandardError))

Since:

  • 2.0.0



57
58
59
60
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/logger.rb', line 57

def log_fatal(*args, level: :fatal, **options, &block)
  logger_common(*args, "Called from #{Gem.location_of_caller.join(':')}", level: level, **options, &block)
  raise options.fetch(:exception, StandardError), args.first
end

#log_info(*args, **options, &block)

Informational message.

This method returns an undefined value.

Parameters:

Options Hash (**options):

  • :depth (Integer)

    Recursion depth for indenting output

  • :lineno (Integer)

    associated with message

  • :logger (Logger, #<<)

Yield Returns:

  • (String)

    added to message

Since:

  • 2.0.0



154
155
156
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/logger.rb', line 154

def log_info(*args, level: :info, **options, &block)
  logger_common(*args, level: level, **options, &block)
end

#log_recover(*args, **options, &block)

Recovers from an error condition. If args are passed, sent as an informational message

As a side-effect of clearing @logger_in_error.

This method returns an undefined value.

Parameters:

Options Hash (**options):

  • :depth (Integer)

    Recursion depth for indenting output

  • :lineno (Integer)

    associated with message

  • :logger (Logger, #<<)

Yield Returns:

  • (String)

    added to message

Since:

  • 2.0.0



133
134
135
136
137
138
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/logger.rb', line 133

def log_recover(*args, level: :info, **options, &block)
  logger = self.logger(options)
  logger.recovering = false
  return if args.empty? && !block_given?
  logger_common(*args, level: level, **options, &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.

Parameters:

Options Hash (**options):

Returns:

  • (Boolean)

Since:

  • 2.0.0



94
95
96
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/logger.rb', line 94

def log_recovering?(**options)
  self.logger(options).recovering
end

#log_statistics(**options) ⇒ Hash{Symbol => Integer}

Number of times logger has been called at each level

Parameters:

Options Hash (**options):

Returns:

Since:

  • 2.0.0



36
37
38
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/logger.rb', line 36

def log_statistics(**options)
  logger(options).log_statistics
end

#log_warn(*args, **options, &block)

Warning message.

This method returns an undefined value.

Parameters:

Options Hash (**options):

  • :depth (Integer)

    Recursion depth for indenting output

  • level (:fatal, :error, :warn, :info, :debug) — default: :<<
  • :lineno (Integer)

    associated with message

  • :logger (Logger, #<<)

Yield Returns:

  • (String)

    added to message

Since:

  • 2.0.0



113
114
115
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/logger.rb', line 113

def log_warn(*args, level: :warn, **options, &block)
  logger_common(*args, level: level, **options, &block)
end

#logger(**options) ⇒ Logger, ...

Logger instance, found using options[:logger], @logger, or @options[:logger]

Parameters:

Options Hash (**options):

Returns:

Since:

  • 2.0.0



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/logger.rb', line 16

def logger(**options)
  logger = options.fetch(:logger, @logger)
  logger = @options[:logger] if logger.nil? && @options
  if logger.nil?
    # Unless otherwise specified, use $stderr
    logger = (@options || options)[:logger] = $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 = (@options || options)[:logger] = ::Logger.new(::File.open(::File::NULL, "w"))  unless logger # Incase false was used, which is frozen
  logger.extend(LoggerBehavior) unless logger.is_a?(LoggerBehavior)
  logger
end