Class: Scribbler
- Inherits:
-
Object
- Object
- Scribbler
- Defined in:
- lib/scribble-client/scribbler.rb
Overview
Scribbler provides support for logging structured data via syslog. The
Defined Under Namespace
Classes: Context
Class Method Summary collapse
Instance Method Summary collapse
-
#context(name = :def, &block) ⇒ Object
Returns the context of the given name, creating a new one if it does not exist.
-
#event(hash) ⇒ Object
def log(string,context) event(‘log’,:msg => string,context) end.
-
#initialize(target, context = nil) ⇒ Scribbler
constructor
Initializes a new Logger.
Constructor Details
#initialize(target, context = nil) ⇒ Scribbler
Initializes a new Logger.
target is a string, it is assumed to be <host>:<port> format of the target syslog server
81 82 83 84 85 86 |
# File 'lib/scribble-client/scribbler.rb', line 81 def initialize(target,context = nil) @syslogger = SyslogClient.new(target) @contexts = {} self.context.add(context) if context end |
Class Method Details
.process_context ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/scribble-client/scribbler.rb', line 22 def self.process_context @proc_scribbler ||= Context.new { { :host => Socket.gethostname, :pid => Process.pid } } end |
.thread_context ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/scribble-client/scribbler.rb', line 9 def self.thread_context t = Thread.current t[:_scribbler_context] ||= Context.new { sequence_num = 0 { :thuid => UUID.random_create.to_s, :thseq => lambda { |evt| sequence_num += 1 } } } end |
Instance Method Details
#context(name = :def, &block) ⇒ Object
Returns the context of the given name, creating a new one if it does not exist.
If a block is supplied, the context is created for the duration of the block and removed afterwards (in this case the method returns the result of the block).
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/scribble-client/scribbler.rb', line 57 def context(name = :def,&block) ctx = @contexts[name] if(ctx.nil?) delete_when_finished = true @contexts[name] = ctx = Context.new else delete_when_finished = false end if(block_given?) begin return (yield ctx) ensure @contexts.delete(name) if(delete_when_finished) end else return ctx end end |
#event(hash) ⇒ Object
def log(string,context)
event('log',:msg => string,context)
end
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/scribble-client/scribbler.rb', line 35 def event(hash) result = {} #Merge data from the system-wide contexts result[:procCtx] = self.class.process_context.apply_context result[:thdCtx] = self.class.thread_context.apply_context #Merge the contexts on this instance @contexts.each_pair { |cname,ctx| @contexts.delete(cname) if(ctx.empty?) #Automatically clears out useless contexts result[cname] = ctx.apply_context } result[:evt] = hash #21 == local5 facility, 6 == info priority @syslogger.syslog(21,6,result.to_json) end |