class Swiftcore::Analogger
Constants
- C_bar
- C_colon
- Ccull
- Cdaemonize
- Cdefault
- Cdefault_log
- Chost
- Cinterval
- Ckey
- Clevels
- Clogfile
- Clogs
- Cpidfile
- Cport
- Csecret
- Cservice
- Csyncinterval
- DefaultSeverityLevels
Public Class Methods
add_log(log)
click to toggle source
# File src/swiftcore/Analogger.rb, line 165 def add_log(log) @queue[log.first] << log @rcount += 1 end
check_config_settings()
click to toggle source
# File src/swiftcore/Analogger.rb, line 140 def check_config_settings raise NoPortProvided unless @config[Cport] raise BadPort.new(@config[Cport]) unless @config[Cport].to_i > 0 end
cleanup()
click to toggle source
# File src/swiftcore/Analogger.rb, line 83 def cleanup @logs.each do |service,l| l.logfile.fsync if !l.logfile.closed? and l.logfile.fileno > 2 l.logfile.close unless l.logfile.closed? or l.logfile.fileno < 3 end end
config()
click to toggle source
# File src/swiftcore/Analogger.rb, line 94 def config @config end
config=(conf)
click to toggle source
# File src/swiftcore/Analogger.rb, line 98 def config=(conf) @config = conf end
daemonize()
click to toggle source
# File src/swiftcore/Analogger.rb, line 68 def daemonize if (child_pid = fork) puts "PID #{child_pid}" unless @config[Cpidfile] exit! end Process.setsid rescue Exception puts "Platform (#{RUBY_PLATFORM}) does not appear to support fork/setsid; skipping" end
flush_queue()
click to toggle source
# File src/swiftcore/Analogger.rb, line 203 def flush_queue @logs.each_value {|l| l.logfile.fsync if l.logfile.fileno > 2} end
key()
click to toggle source
# File src/swiftcore/Analogger.rb, line 207 def key @config[Ckey].to_s end
logfile_destination(logfile)
click to toggle source
# File src/swiftcore/Analogger.rb, line 155 def logfile_destination(logfile) if logfile =~ /^STDOUT$/i $stdout elsif logfile =~ /^STDERR$/i $stderr else File.open(logfile,'ab+') end end
new_log(facility = Cdefault, levels = @config[Clevels] || DefaultSeverityLevels, log = @config[Cdefault_log], cull = true)
click to toggle source
# File src/swiftcore/Analogger.rb, line 79 def new_log(facility = Cdefault, levels = @config[Clevels] || DefaultSeverityLevels, log = @config[Cdefault_log], cull = true) Log.new({Cservice => facility, Clevels => levels, Clogfile => log, Ccull => cull}) end
normalize_levels(levels)
click to toggle source
# File src/swiftcore/Analogger.rb, line 126 def normalize_levels(levels) if String === levels and levels =~ /,/ levels.split(/,/).inject({}) {|h,k| h[k.to_s] = true; h} elsif Array === levels levels.inject({}) {|h,k| h[k.to_s] = true; h} elsif levels.nil? DefaultSeverityLevels elsif !(Hash === levels) [levels.to_s => true] else levels end end
populate_logs()
click to toggle source
# File src/swiftcore/Analogger.rb, line 102 def populate_logs @config[Clogs].each do |log| next unless log[Cservice] if Array === log[Cservice] log[Cservice].each do |loglog| @logs[loglog] = new_log(loglog,log[Clevels],logfile_destination(log[Clogfile]),log[Ccull]) end else @logs[log[Cservice]] = new_log(log[Cservice],log[Clevels],logfile_destination(log[Clogfile]),log[Ccull]) end end end
postprocess_config_load()
click to toggle source
# File src/swiftcore/Analogger.rb, line 115 def postprocess_config_load @config[Clogs] ||= [] if @config[Clevels] @config[Clevels] = normalize_levels(@config[Clevels]) end @config[Clogs].each do |log| log[Clevels] = normalize_levels(log[Clevels]) end end
set_config_defaults()
click to toggle source
# File src/swiftcore/Analogger.rb, line 145 def set_config_defaults @config[Chost] ||= '127.0.0.1' @config[Cinterval] ||= 1 @config[Csyncinterval] ||= 60 @config[Csyncinterval] = nil if @config[Csyncinterval] == 0 @config[Cdefault_log] = @config[Cdefault_log].nil? || @config[Cdefault_log] == '-' ? 'STDOUT' : @config[Cdefault_log] @config[Cdefault_log] = logfile_destination(@config[Cdefault_log]) @logs['default'] = new_log end
start(config,protocol = AnaloggerProtocol)
click to toggle source
# File src/swiftcore/Analogger.rb, line 45 def start(config,protocol = AnaloggerProtocol) @config = config daemonize if @config[Cdaemonize] File.open(@config[Cpidfile],'w+') {|fh| fh.puts $$} if @config[Cpidfile] @logs = Hash.new {|h,k| h[k] = new_log(k)} @queue = Hash.new {|h,k| h[k] = []} postprocess_config_load check_config_settings populate_logs set_config_defaults @rcount = 0 @wcount = 0 trap("INT") {cleanup;exit} trap("TERM") {cleanup;exit} trap("HUP") {cleanup;throw :hup} EventMachine.run { EventMachine.start_server @config[Chost], @config[Cport], protocol EventMachine.add_periodic_timer(1) {Analogger.update_now} EventMachine.add_periodic_timer(@config[Cinterval]) {write_queue} EventMachine.add_periodic_timer(@config[Csyncinterval]) {flush_queue} } end
update_now()
click to toggle source
# File src/swiftcore/Analogger.rb, line 90 def update_now @now = Time.now.strftime('%Y/%m/%d %H:%M:%S') end
write_queue()
click to toggle source
# File src/swiftcore/Analogger.rb, line 170 def write_queue @queue.each do |service,q| last_sv = nil last_m = nil last_count = 0 next unless log = @logs[service] lf = log.logfile cull = log.cull levels = log.levels q.each do |m| next unless levels.has_key?(m[1]) if cull if m.last == last_m and m[0..1] == last_sv last_count += 1 next elsif last_count > 0 lf.puts "#{@now}|#{last_sv.join(C_bar)}|Last message repeated #{last_count} times" last_sv = last_m = nil last_count = 0 end lf.puts "#{@now}|#{m.join(C_bar)}" last_m = m.last last_sv = m[0..1] else lf.puts "#{@now}|#{m.join(C_bar)}" end @wcount += 1 end lf.puts "#{@now}|#{last_sv.join(C_bar)}|Last message repeated #{last_count} times" if cull and last_count > 0 end @queue.each {|service,q| q.clear} end