Class: LwesLogger
- Inherits:
-
Logger
- Object
- Logger
- LwesLogger
- Defined in:
- lib/lwes_logger.rb
Overview
Lwes based ruby logger for real-time logging.
Constant Summary collapse
- VERSION =
'1.0.4'
- HOSTNAME =
Socket.gethostname
- FORMAT =
"%s [%s#%d] %5s -- %s: %s\n"
- DATETIME_FORMAT =
"%b %d %H:%M:%S"
Instance Attribute Summary collapse
-
#datetime_format ⇒ Object
Format of the timestamp in the lwes events.
-
#emitter ⇒ Object
readonly
The lwes event emitter.
-
#full_logs_event ⇒ Object
The name of the event to emit all severity types to.
-
#full_logs_only ⇒ Object
Don’t segregate by severity and only emit events to Namespace::Full.
-
#meta_event ⇒ Object
readonly
The default event data to pass along with the lwes event.
-
#namespace ⇒ Object
Base lwes namespace for events.
Instance Method Summary collapse
-
#<<(msg) ⇒ Object
Dump given message to the log device without any formatting.
-
#add(severity, message = nil, progname = nil, &block) ⇒ Object
(also: #log)
Log to both lwes and the log device, if given.
-
#build_log_event(severity, message = nil, progname = nil, data = {}, &block) ⇒ Object
Creates an lwes event hash with log data.
-
#emit_log(severity, message = nil, progname = nil, data = {}, &block) ⇒ Object
Emits an lwes logging event.
-
#initialize(ip_address, options = {}) ⇒ LwesLogger
constructor
A new instance of LwesLogger.
-
#meta_event_attr(key, value = nil, &block) ⇒ Object
Access the meta-event hash to set default values.
Constructor Details
#initialize(ip_address, options = {}) ⇒ LwesLogger
Returns a new instance of LwesLogger.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/lwes_logger.rb', line 55 def initialize ip_address, ={} args = [[:log_device]].flatten super(*args) @meta_event = { :hostname => HOSTNAME, :pid => $$.to_s } @datetime_format = [:datetime_format] || DATETIME_FORMAT @full_logs_event = "Full" @full_logs_only = false @formatter = method(:call_format) @namespace = camelize [:namespace] || "LwesLogger" @emitter = lwes_emitter ip_address, end |
Instance Attribute Details
#datetime_format ⇒ Object
Format of the timestamp in the lwes events. Note: If using a custom formatter, this property will only apply to lwes events. Defaults to DATETIME_FORMAT.
41 42 43 |
# File 'lib/lwes_logger.rb', line 41 def datetime_format @datetime_format end |
#emitter ⇒ Object (readonly)
The lwes event emitter.
36 37 38 |
# File 'lib/lwes_logger.rb', line 36 def emitter @emitter end |
#full_logs_event ⇒ Object
The name of the event to emit all severity types to. Will not emit to the full logs event if set to nil or false. Defaults to “Full”
26 27 28 |
# File 'lib/lwes_logger.rb', line 26 def full_logs_event @full_logs_event end |
#full_logs_only ⇒ Object
Don’t segregate by severity and only emit events to Namespace::Full. Defaults to false.
30 31 32 |
# File 'lib/lwes_logger.rb', line 30 def full_logs_only @full_logs_only end |
#meta_event ⇒ Object (readonly)
The default event data to pass along with the lwes event.
21 22 23 |
# File 'lib/lwes_logger.rb', line 21 def @meta_event end |
#namespace ⇒ Object
Base lwes namespace for events.
33 34 35 |
# File 'lib/lwes_logger.rb', line 33 def namespace @namespace end |
Instance Method Details
#<<(msg) ⇒ Object
Dump given message to the log device without any formatting. Creates an LwesLogger::Any event.@options.
79 80 81 82 |
# File 'lib/lwes_logger.rb', line 79 def << msg emit_log nil, msg super end |
#add(severity, message = nil, progname = nil, &block) ⇒ Object Also known as: log
Log to both lwes and the log device, if given.
88 89 90 91 92 93 |
# File 'lib/lwes_logger.rb', line 88 def add severity, =nil, progname=nil, &block return true if severity < @level emit_log severity, , progname, &block super end |
#build_log_event(severity, message = nil, progname = nil, data = {}, &block) ⇒ Object
Creates an lwes event hash with log data.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/lwes_logger.rb', line 116 def build_log_event severity, =nil, progname=nil, data={}, &block severity ||= UNKNOWN severity = format_severity(severity) progname ||= @progname ||= block.call if block_given? ||= progname event_id = "#{@namespace}::#{severity.capitalize}-" + UUID.generate event_hash = @meta_event.merge \ :message => .to_s.gsub(/\e\[.*?m/, ''), :progname => progname.to_s, :severity => severity, :timestamp => Time.now.strftime(@datetime_format), :event_id => event_id event_hash.merge! data event_hash.each do |key, val| val = val.call if Proc === val event_hash[key] = val.to_s end event_hash end |
#emit_log(severity, message = nil, progname = nil, data = {}, &block) ⇒ Object
Emits an lwes logging event. Setting the data argument will pass additional data to the emitted event.
102 103 104 105 106 107 108 109 110 |
# File 'lib/lwes_logger.rb', line 102 def emit_log severity, =nil, progname=nil, data={}, &block event_hash = build_log_event severity, , progname, data, &block dump_event = [@namespace, @full_logs_event].join("::") @emitter.emit dump_event, event_hash if @full_logs_event event_name = [@namespace, event_hash[:severity].capitalize].join("::") @emitter.emit event_name, event_hash unless @full_logs_only end |
#meta_event_attr(key, value = nil, &block) ⇒ Object
Access the meta-event hash to set default values. If given a key and block, the block will be run on every log output.
157 158 159 |
# File 'lib/lwes_logger.rb', line 157 def key, value=nil, &block @meta_event[key] = value || (block if block_given?) end |