Class: Aspera::Log

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/aspera/log.rb

Overview

Singleton object for logging

Constant Summary collapse

LOG_TYPES =

where logs are sent to

%i[stderr stdout syslog].freeze
@@format =

rubocop:disable Style/ClassVars

:json

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



93
94
95
# File 'lib/aspera/log.rb', line 93

def logger
  @logger
end

#logger_typeObject

Returns the value of attribute logger_type.



93
94
95
# File 'lib/aspera/log.rb', line 93

def logger_type
  @logger_type
end

#program_name=(value) ⇒ Object (writeonly)

Sets the attribute program_name

Parameters:

  • value

    the value to set the attribute program_name to.



94
95
96
# File 'lib/aspera/log.rb', line 94

def program_name=(value)
  @program_name = value
end

Class Method Details

.capture_stderrObject

Capture the output of $stderr and log it at debug level



83
84
85
86
87
88
89
90
# File 'lib/aspera/log.rb', line 83

def capture_stderr
  real_stderr = $stderr
  $stderr = StringIO.new
  yield
  log.debug($stderr.string)
ensure
  $stderr = real_stderr
end

.dump(name, object) ⇒ Object

dump object suitable for Log.log.debug

Parameters:

  • name

    string or symbol

  • format

    either pp or json format



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/aspera/log.rb', line 69

def dump(name, object)
  result =
    case @@format
    when :json
      JSON.pretty_generate(object) rescue PP.pp(object, +'')
    when :ruby
      PP.pp(object, +'')
    else
      raise 'wrong parameter, expect ruby or json'
    end
  "#{name.to_s.green} (#{@@format})=\n#{result}"
end

.levelsObject

levels are :debug,:info,:warn,:error,fatal,:unknown



61
# File 'lib/aspera/log.rb', line 61

def levels; Logger::Severity.constants.sort{|a, b|Logger::Severity.const_get(a) <=> Logger::Severity.const_get(b)}.map{|c|c.downcase.to_sym}; end

.logObject

get the logger object of singleton



64
# File 'lib/aspera/log.rb', line 64

def log; instance.logger; end

Instance Method Details

#levelObject

get symbol of debug level of underlying logger



102
103
104
105
106
107
# File 'lib/aspera/log.rb', line 102

def level
  Logger::Severity.constants.each do |name|
    return name.downcase.to_sym if @logger.level.eql?(Logger::Severity.const_get(name))
  end
  Aspera.error_unexpected_value(@logger.level){'log level'}
end

#level=(new_level) ⇒ Object

set log level of underlying logger given symbol level



97
98
99
# File 'lib/aspera/log.rb', line 97

def level=(new_level)
  @logger.level = Logger::Severity.const_get(new_level.to_sym.upcase)
end