Module: StdLogger

Defined in:
lib/stdlogger.rb,
lib/stdlogger/error.rb,
lib/stdlogger/version.rb,
lib/stdlogger/multi_io.rb

Defined Under Namespace

Classes: Error, MultiIO

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.create(logdev = nil, options = {}) ⇒ Logger

When no target device available, this method raises StdLogger::Error without ‘:allow_nodev’ option.

Parameters:

  • logdev (String, IO) (defaults to: nil)

    path to logfile or writable IO object.

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

Options Hash (options):

  • :stdout (String) — default: true

    log to STDOUT or not.

  • :stderr (String) — default: false

    log to STDERR or not.

  • :shift_age (String) — default: 0

    ‘shift_age’ param for Logger#new

  • :shift_size (String) — default: 1048576

    ‘shift_size’ param for Logger#new

  • :allow_nodev (String) — default: nil

    don’t raise error even if no target device available

Returns:

  • (Logger)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/stdlogger.rb', line 22

def self.create logdev=nil, options={}
  stdout     = options[:stdout] || true
  stderr     = options[:stderr] || false
  shift_age  = options[:shift_age]  || 0
  shift_size = options[:shift_size] || 1048576

  targets = []
  if logdev
    if logdev.class == String
      targets << File.open(logdev, 'a')
    else
      targets << logdev
    end
  end
  targets << $stdout if (stdout and $stdout.tty?)
  targets << $stderr if (stderr and $stderr.tty?)
  if targets.empty? and !options[:allow_nodev]
    raise Error, "No output device found!"
  end
  multi_dev = MultiIO.new targets

  Logger.new multi_dev, shift_age, shift_size
end