Class: EventMachine::Logger
- Inherits:
-
Object
- Object
- EventMachine::Logger
- Defined in:
- lib/em-syslog/logger.rb
Overview
Generic Logger Class, providing simple to use statements auto fills in the most likely config DEFAULTS
Constant Summary collapse
- DEFAULTS =
{:idenity => $PROGRAM_NAME, :include_hostname => false, :resource => "udp:/dev/log" }
- TYPE =
save out needed information from config, and start up the connection
0
- HOST =
1
- IPC =
1
- PORT =
2
- @@connection_cache =
Hash.new
Instance Attribute Summary collapse
-
#idenity ⇒ Object
readonly
Returns the value of attribute idenity.
-
#include_hostname ⇒ Object
readonly
Returns the value of attribute include_hostname.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Class Method Summary collapse
-
.new(config = {}) ⇒ Object
hack new class method for caching connections, making it safe to keep variables out of scope.
-
.timestamp(time = Time.now) ⇒ Object
you must fix up the timestamp so that a space is injected in place of a leading 0 - returns “Dec 6 12:12:12”.
Instance Method Summary collapse
-
#facility ⇒ Object
Meta program our facility/severity keys and methods.
-
#initialize(config) ⇒ Logger
constructor
A new instance of Logger.
-
#log(msg, facility, severity) ⇒ Object
When called directly we need to make sure we were given valid facilities and severity.
Constructor Details
#initialize(config) ⇒ Logger
Returns a new instance of Logger.
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 |
# File 'lib/em-syslog/logger.rb', line 41 def initialize( config) @idenity = config[:idenity].to_s + "[" + Process.pid.to_s + "]" @resource = config[:resource].dup @include_hostname = config[:include_hostname] resource = self.class.parse_resource( config[:resource]) @connection = nil if resource[TYPE] == :tcp # resource[PORT] should return nil considering we only define a pair (resource_type|path) @connection = EM.connect( resource[HOST], resource[PORT], Syslog::ConnectionTCP) raise "unable to create connection" if @connection.nil? @connection.setup( resource[HOST], resource[PORT]) elsif resource.length == 3 @connection = EM.open_datagram_socket( '0.0.0.0', 0, Syslog::ConnectionUDP) raise "unable to create connection" if @connection.nil? @connection.setup( resource[HOST], resource[PORT]) else # need better checking here raise "unix domain socket #{resource[IPC]} does not exist!" unless ::File.exists?( resource[IPC]) c = Syslog::ConnectionUDP::UNIX.create_unix @connection = EM.watch( c, Syslog::ConnectionUDP::UNIX) raise "unable to create connection" if @connection.nil? @connection.setup( c, resource[IPC]) end end |
Instance Attribute Details
#idenity ⇒ Object (readonly)
Returns the value of attribute idenity.
14 15 16 |
# File 'lib/em-syslog/logger.rb', line 14 def idenity @idenity end |
#include_hostname ⇒ Object (readonly)
Returns the value of attribute include_hostname.
14 15 16 |
# File 'lib/em-syslog/logger.rb', line 14 def include_hostname @include_hostname end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
14 15 16 |
# File 'lib/em-syslog/logger.rb', line 14 def resource @resource end |
Class Method Details
.new(config = {}) ⇒ Object
hack new class method for caching connections, making it safe to keep variables out of scope.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/em-syslog/logger.rb', line 22 def self.new( config = {}) config = DEFAULTS.merge config # See if we have a connection already in our cache key = self.mk_cache_index_key( config[:idenity], config[:resource]) return @@connection_cache[key] if @@connection_cache.has_key? key and @@connection_cache[key].error? == false # Otherwise allocate a new object to do the work instance = self.allocate instance.send( :initialize, config) @@connection_cache[key] = instance end |
.timestamp(time = Time.now) ⇒ Object
you must fix up the timestamp so that a space is injected in place of a leading 0
-
returns “Dec 6 12:12:12”
97 98 99 100 101 |
# File 'lib/em-syslog/logger.rb', line 97 def self.( time=Time.now) day = time.strftime("%d") day = day.sub(/^0/, ' ') if day =~ /^0\d/ time.strftime("%b #{day} %H:%M:%S") end |
Instance Method Details
#facility ⇒ Object
Meta program our facility/severity keys and methods
79 80 81 82 83 84 85 86 |
# File 'lib/em-syslog/logger.rb', line 79 Syslog::FACILITIES.each {|facility,facility_int| Syslog::SEVERITIES.each {|severity,severity_int| define_method( "#{facility}_#{severity}".to_sym) do |msg| send_log( msg, facility, severity) end class_variable_set("@@syskey_#{facility}_#{severity}".to_sym, (facility_int * 8 + severity_int)) } } |
#log(msg, facility, severity) ⇒ Object
When called directly we need to make sure we were given valid facilities and severity
70 71 72 73 74 |
# File 'lib/em-syslog/logger.rb', line 70 def log( msg, facility, severity) raise "Invalid log severity!" unless Syslog::SEVERITIES.has_key? severity raise "Invalid log facility!" unless Syslog::FACILITIES.has_key? facility send_log( msg, facility, severity) end |