Class: KineticSdk::Utils::KLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/kinetic_sdk/utils/logger.rb

Overview

The KLogger class provides methods to output at different levels based on a configuration property. The default log level is off, but can be turned on by passing in the log_level option.

Available Levels: debug, info, warn, error, off

The default output is to STDOUT, but can also be set to STDERR by passing in the log_output option.

Available Outputs: STDOUT, STDERR

Instance Method Summary collapse

Constructor Details

#initialize(level, output = "stdout") ⇒ KLogger

Constructor

Parameters:

  • level (String)

    log level (off): debug, info, warn, error, off

  • output (String) (defaults to: "stdout")

    log output (stdout): stdout, stderr



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kinetic_sdk/utils/logger.rb', line 23

def initialize(level, output="stdout")
  log_level = level.to_s.downcase
  log_output = output.to_s.downcase == "stderr" ? STDERR : STDOUT

  @logger = Logger.new(log_output)

  case log_level
  when "error"
    @logger.level= Logger::ERROR
  when "warn"
    @logger.level= Logger::WARN
  when "info"
    @logger.level= Logger::INFO
  when "debug"
    @logger.level= Logger::DEBUG
  else
    @logger.level= Logger::FATAL
  end

  # define the string output format
  @logger.formatter = proc do |severity, datetime, progname, msg|
    date_format = datetime.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
    "[#{date_format}] #{severity}: #{msg}\n"
  end
end

Instance Method Details

#debug(msg) ⇒ Object

Print the message if level is Logger::DEBUG or lower

Parameters:

  • msg (String)

    the message to log



52
# File 'lib/kinetic_sdk/utils/logger.rb', line 52

def debug(msg); @logger.debug(msg); end

#debug?Boolean

Is the current log level set to Logger::DEBUG or lower?

Returns:

  • (Boolean)

    true if level is debug or lower



57
# File 'lib/kinetic_sdk/utils/logger.rb', line 57

def debug?; @logger.debug? end

#error(msg) ⇒ Object

Print the message if level is Logger::ERROR or lower

Parameters:

  • msg (String)

    the message to log



62
# File 'lib/kinetic_sdk/utils/logger.rb', line 62

def error(msg); @logger.error(msg); end

#error?Boolean

Is the current log level set to Logger::ERROR or lower?

Returns:

  • (Boolean)

    true if level is error or lower



67
# File 'lib/kinetic_sdk/utils/logger.rb', line 67

def error?; @logger.error? end

#fatal(msg) ⇒ Object

Print the message if level is Logger::FATAL or lower

Parameters:

  • msg (String)

    the message to log



72
# File 'lib/kinetic_sdk/utils/logger.rb', line 72

def fatal(msg); @logger.fatal(msg); end

#fatal?Boolean

Is the current log level set to Logger::FATAL or lower?

Returns:

  • (Boolean)

    true if level is fatal or lower



77
# File 'lib/kinetic_sdk/utils/logger.rb', line 77

def fatal?; @logger.fatal? end

#info(msg) ⇒ Object

Print the message if level is Logger::INFO or lower

Parameters:

  • msg (String)

    the message to log



82
# File 'lib/kinetic_sdk/utils/logger.rb', line 82

def info(msg); @logger.info(msg); end

#info?Boolean

Is the current log level set to Logger::INFO or lower?

Returns:

  • (Boolean)

    true if level is info or lower



87
# File 'lib/kinetic_sdk/utils/logger.rb', line 87

def info?; @logger.info? end

#levelInteger

Get the logger level

Returns:

  • (Integer)


102
# File 'lib/kinetic_sdk/utils/logger.rb', line 102

def level; @logger.level; end

#warn(msg) ⇒ Object

Print the message if level is Logger::WARN or lower

Parameters:

  • msg (String)

    the message to log



92
# File 'lib/kinetic_sdk/utils/logger.rb', line 92

def warn(msg); @logger.warn(msg); end

#warn?Boolean

Is the current log level set to Logger::WARN or lower?

Returns:

  • (Boolean)

    true if level is warn or lower



97
# File 'lib/kinetic_sdk/utils/logger.rb', line 97

def warn?; @logger.warn?; end