Class: ServerEngine::DaemonLogger

Inherits:
Logger
  • Object
show all
Includes:
Severity
Defined in:
lib/serverengine/daemon_logger.rb

Defined Under Namespace

Modules: RotationAware, Severity

Constant Summary collapse

SEVERITY_FORMATS_ =
%w(TRACE DEBUG INFO WARN ERROR FATAL ANY)

Constants included from Severity

Severity::TRACE

Instance Method Summary collapse

Constructor Details

#initialize(logdev, config = {}) ⇒ DaemonLogger

Returns a new instance of DaemonLogger.



32
33
34
35
36
37
38
39
40
41
# File 'lib/serverengine/daemon_logger.rb', line 32

def initialize(logdev, config={})
  @rotate_age = config[:log_rotate_age] || 5
  @rotate_size = config[:log_rotate_size] || 1048576
  @file_dev = nil

  super(nil)

  self.level = config[:log_level] || 'debug'
  self.logdev = logdev
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object

override add method



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/serverengine/daemon_logger.rb', line 68

def add(severity, message = nil, progname = nil, &block)
  if severity < @level
    return true
  end
  if message.nil?
    if block_given?
      message = yield
    else
      message = progname
      progname = nil
    end
  end
  progname ||= @progname
  self << format_message(SEVERITY_FORMATS_[severity+1], Time.now, progname, message)
  true
end

#closeObject



131
132
133
134
# File 'lib/serverengine/daemon_logger.rb', line 131

def close
  @file_dev.close if @file_dev
  nil
end

#level=(expr) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/serverengine/daemon_logger.rb', line 93

def level=(expr)
  case expr.to_s
  when 'fatal', FATAL.to_s
    e = FATAL
  when 'error', ERROR.to_s
    e = ERROR
  when 'warn', WARN.to_s
    e = WARN
  when 'info', INFO.to_s
    e = INFO
  when 'debug', DEBUG.to_s
    e = DEBUG
  when 'trace', TRACE.to_s
    e = TRACE
  else
    raise ArgumentError, "invalid log level: #{expr}"
  end

  super(e)
end

#logdev=(logdev) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/serverengine/daemon_logger.rb', line 43

def logdev=(logdev)
  # overwrites Logger's @logdev variable
  if logdev.respond_to?(:write) and logdev.respond_to?(:close)
    # IO
    @logdev = logdev
    @logdev.sync = true if @logdev.respond_to?(:sync=)
    if @file_dev
      old_file_dev = @file_dev
      @file_dev = nil
      old_file_dev.close
    end
  elsif !@file_dev || @file_dev.filename != logdev
    # update path string
    old_file_dev = @file_dev
    @file_dev = LogDevice.new(logdev, shift_age: @rotate_age, shift_size: @rotate_size)
    # Enable to detect rotation done by external tools.
    # Otherwise it continues writing logs to old file unexpectedly.
    @file_dev.extend(RotationAware)
    old_file_dev.close if old_file_dev
    @logdev = @file_dev
  end
  logdev
end

#reopenObject



121
122
123
124
125
126
127
128
129
# File 'lib/serverengine/daemon_logger.rb', line 121

def reopen
  begin
    reopen!
    return true
  rescue
    # TODO log?
    return false
  end
end

#reopen!Object



116
117
118
119
# File 'lib/serverengine/daemon_logger.rb', line 116

def reopen!
  @file_dev.reopen! if @file_dev
  nil
end

#trace?Boolean

Returns:

  • (Boolean)


114
# File 'lib/serverengine/daemon_logger.rb', line 114

def trace?; @level <= TRACE; end