Class: Worldline::Acquiring::SDK::Logging::RubyCommunicatorLogger

Inherits:
CommunicatorLogger show all
Defined in:
lib/worldline/acquiring/sdk/logging/ruby_communicator_logger.rb

Overview

Logging class that Logs messages and errors to a logger. Errors can be logged at a separate level compared to regular messages.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, log_level, error_level = nil) ⇒ RubyCommunicatorLogger

Creates a new RubyCommunicatorLogger instance.

Parameters:

  • logger (Logger)

    the logger to log messages to. Messages to log will be provided using logger#log(message level, message)

  • log_level (String)

    log level to use for non-error messages.

  • error_level (String, nil) (defaults to: nil)

    error logging level to use.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/worldline/acquiring/sdk/logging/ruby_communicator_logger.rb', line 17

def initialize(logger, log_level, error_level = nil)
  # implement the interface
  error_level ||= log_level
  raise ArgumentError unless logger
  raise ArgumentError unless log_level
  raise ArgumentError unless error_level

  @logger = logger
  @log_level = log_level
  @error_level = error_level
end

Class Method Details

.create_logfile(filename) ⇒ Object

Opens or creates a new file in write-only mode with filename.



40
41
42
43
44
45
46
47
48
49
# File 'lib/worldline/acquiring/sdk/logging/ruby_communicator_logger.rb', line 40

def self.create_logfile(filename)
  logdev = begin
             open(filename, (File::WRONLY | File::APPEND | File::CREAT | File::EXCL))
           rescue Errno::EEXIST
             # file is created by another process
             open_logfile(filename)
           end
  logdev.sync = true
  logdev
end

.open_logfile(filename) ⇒ Object

Opens or creates a new file in write-only mode with filename.



52
53
54
55
56
57
58
# File 'lib/worldline/acquiring/sdk/logging/ruby_communicator_logger.rb', line 52

def self.open_logfile(filename)
  begin
    open(filename, (File::WRONLY | File::APPEND))
  rescue Errno::ENOENT
    create_logfile(filename)
  end
end

Instance Method Details

#log(msg, thrown = nil) ⇒ Object

Logs a single error or non-error message to the logger.



30
31
32
33
34
35
36
37
# File 'lib/worldline/acquiring/sdk/logging/ruby_communicator_logger.rb', line 30

def log(msg, thrown = nil)
  # use Ruby Logger
  if thrown
    @logger.log(@error_level) { msg + $RS + thrown.to_s + $RS + thrown.backtrace.join($RS) }
  else
    @logger.log(@log_level, msg)
  end
end