Class: SyslogWrapper

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

Instance Method Summary collapse

Constructor Details

#initializeSyslogWrapper

Returns a new instance of SyslogWrapper.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/syslog.rb', line 16

def initialize()
  # facility is "user", since there is no "backup" facility
  # also, if we are running root, perhaps we should log with
  # "daemon" facility?
  # 
  # facilies and priorities are listed here:
  # http://docstore.mik.ua/orelly/networking/puis/ch10_05.htm
  @@log = Syslog.open('smarbs', Syslog::LOG_PID, Syslog::LOG_USER)
  
  # normally log up to INFO level
  # for debug, try DEBUG here (if we have any debug messages)
  @@log.mask = Syslog::LOG_UPTO(Syslog::LOG_INFO)
end

Instance Method Details

#finalizeObject



30
31
32
33
34
# File 'lib/syslog.rb', line 30

def finalize()
  # not sure if closing is really necessary,
  # nothing bad really happens if we pass this...
  @@log.close() if @@log
end

#log(msg = "", level = 1) ⇒ Object

logger function, does some input satitation, and logs to syslog



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/syslog.rb', line 39

def log(msg="", level=1)
  
  # Oh yes, we need to sanitaze here. 
  
  msg.gsub!(/%/, ' ') # '%' needs to be removed, else there will be runtimeError
  msg.gsub!(/\n/, ' ')
  msg.gsub!(/\0/, ' ')
  
  # Ideally we would split the msg, and post in multiple parts
  # but I guess this is enough, we should never have that long lines anyway
  if msg.length >= 1024
    msg = msg[0..1023]
  end
  
  # levels are:
  # * 0: debug
  # * 1: info
  # * 2: notice
  # * 3: warning
  # * 4: error
  
  case level
    when 1
	@@log.info(msg) unless msg.empty?
    when 2
	@@log.notice(msg) unless msg.empty?
    when 3
	@@log.warning(msg) unless msg.empty?
    when 4
	@@log.err(msg) unless msg.empty?
    when 0
    @@log.debug(msg) unless msg.empty?
    else
	# something tries to log with nonexisting level, or with too high level
	# its probably better to halt the execution than log with wrong level, or not log at all
	raise RuntimeError.new("Wrong loglevel defined somewhere! This is a bug.")
  end
end