Module: OsCtl::Lib::Logger

Defined in:
lib/libosctl/logger.rb

Class Method Summary collapse

Class Method Details

.getLogger?

Returns:



35
36
37
# File 'lib/libosctl/logger.rb', line 35

def self.get
  @logger == :none ? nil : @logger
end

.log(severity, msg) ⇒ Object



39
40
41
42
43
44
# File 'lib/libosctl/logger.rb', line 39

def self.log(severity, msg)
  return if @logger == :none

  @logger.send(severity, msg)
  $stdout.flush if @logger.is_a?(::Logger)
end

.setup(type, opts = {}) ⇒ Object

Parameters:

  • type (Symbol)

    ‘:stdout`, `:io`, `:syslog` or `:none`

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :io (IO)
  • :name (String)

    program name for syslog

  • :facility (String)

    syslog facility, see man syslog(3), in lower case, without ‘LOG_` prefix



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/libosctl/logger.rb', line 9

def self.setup(type, opts = {})
  case type
  when :stdout
    require 'logger'
    @logger = ::Logger.new($stdout)

  when :io
    require 'logger'
    @logger = ::Logger.new(opts[:io])

  when :syslog
    require 'syslog/logger'
    @logger = Syslog::Logger.new(
      opts[:name] || File.basename($0),
      Syslog.const_get(:"LOG_#{(opts[:facility] || 'daemon').upcase}")
    )

  when :none
    @logger = :none

  else
    raise "unsupported logger type '#{type}'"
  end
end