Class: SyslogRuby::Logger

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

Constant Summary collapse

@@facilities =
{}
@@severities =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ident = 'ruby', facility = Facility::LOCAL6, options = {}) ⇒ Logger



26
27
28
29
30
31
32
33
34
35
# File 'lib/syslog_ruby/logger.rb', line 26

def initialize(ident = 'ruby',
               facility = Facility::LOCAL6,
               options = {})
  @ident = "#{ident}[#{$$}]"
  @facility = _configure_facility(facility)
  @mask = LOG_UPTO(Severity::DEBUG)
  @log_uri = options.fetch(:uri, 'testing')
  @hostname = Socket.gethostname.split('.').first
  _open
end

Instance Attribute Details

#facilityObject

Returns the value of attribute facility.



5
6
7
# File 'lib/syslog_ruby/logger.rb', line 5

def facility
  @facility
end

#hostnameObject

Returns the value of attribute hostname.



5
6
7
# File 'lib/syslog_ruby/logger.rb', line 5

def hostname
  @hostname
end

#identObject

Returns the value of attribute ident.



5
6
7
# File 'lib/syslog_ruby/logger.rb', line 5

def ident
  @ident
end

#log_uriObject

Returns the value of attribute log_uri.



5
6
7
# File 'lib/syslog_ruby/logger.rb', line 5

def log_uri
  @log_uri
end

#maskObject

Returns the value of attribute mask.



5
6
7
# File 'lib/syslog_ruby/logger.rb', line 5

def mask
  @mask
end

#socketObject

Returns the value of attribute socket.



5
6
7
# File 'lib/syslog_ruby/logger.rb', line 5

def socket
  @socket
end

Class Method Details

.setup_severity_methodsObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/syslog_ruby/logger.rb', line 7

def setup_severity_methods
  SyslogRuby::Severity.constants.each do |level_name|
    level = SyslogRuby::Severity.const_get level_name

    define_method(level_name.downcase) do |*args|
      message = args.shift
      facility = args.shift || send(:facility)
      _log(facility, level, message)
    end
  end
end

Instance Method Details

#_configure_facility(facility) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/syslog_ruby/logger.rb', line 37

def _configure_facility(facility)
  case facility
  when Integer || Fixnum
    facility
  when Symbol || String
    Facility[Facility]
  else
    Facility::NONE
  end
end

#closeObject



60
61
62
63
# File 'lib/syslog_ruby/logger.rb', line 60

def close
  raise RuntimeError.new "syslog not open" if socket.closed?
  socket.close
end

#log(level, message, *message_args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/syslog_ruby/logger.rb', line 48

def log(level, message, *message_args)
  level = case level
  when Integer || Fixnum
    level
  when Symbol || String
    Severity[level]
  else
    Severity::INFO
  end
  _log(facility, level, message, *message_args)
end

#LOG_MASK(level) ⇒ Object



69
70
71
# File 'lib/syslog_ruby/logger.rb', line 69

def LOG_MASK(level)
  Severity[level]
end

#LOG_UPTO(level) ⇒ Object



73
74
75
76
77
# File 'lib/syslog_ruby/logger.rb', line 73

def LOG_UPTO(level)
  (0...Severity[level]).inject(Integer(0)) do |mask, i|
    mask|i
  end
end

#opened?Boolean



79
80
81
# File 'lib/syslog_ruby/logger.rb', line 79

def opened?
  socket && !socket.closed?
end

#optionsObject



83
84
85
# File 'lib/syslog_ruby/logger.rb', line 83

def options
  0
end

#reopen(*args) ⇒ Object



65
66
67
# File 'lib/syslog_ruby/logger.rb', line 65

def reopen(*args)
  close && _open
end