Module: LogStuff
Constant Summary collapse
- NAMESPACE =
:log
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#logger_path ⇒ Object
Returns the value of attribute logger_path.
-
#source ⇒ Object
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #get_thread_current(name) ⇒ Object
- #metadata(*pairs, &block) ⇒ Object
- #new_logger(path, loglevel) ⇒ Object
- #set_thread_current(name, value) ⇒ Object
- #setup(params) ⇒ Object
- #tag(*tags, &block) ⇒ Object
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
10 11 12 |
# File 'lib/logstuff.rb', line 10 def logger @logger end |
#logger_path ⇒ Object
Returns the value of attribute logger_path.
9 10 11 |
# File 'lib/logstuff.rb', line 9 def logger_path @logger_path end |
#source ⇒ Object
Returns the value of attribute source.
10 11 12 |
# File 'lib/logstuff.rb', line 10 def source @source end |
Class Method Details
.log(severity = 'info', *args, &block) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/logstuff.rb', line 33 def self.log(severity = 'info', *args, &block) return unless block_given? # Ignore if we are not logging this severity return unless logger.send("#{severity}?") local_fields = {} = Set.new args.each do |arg| case arg when Hash local_fields.merge!(arg) when Symbol .add(arg) when Array .merge(arg) end end msg = yield event = LogStash::Event.new('@source' => source, '@severity' => severity, 'message' => msg, '@tags' => get_thread_current(:current_tags).merge().to_a, '@fields' => get_thread_current(:current_fields).merge(local_fields) ) logger << event.to_json + "\n" end |
Instance Method Details
#get_thread_current(name) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/logstuff.rb', line 15 def get_thread_current(name) Thread.current[NAMESPACE] ||= { :current_fields => {}, :current_tags => Set.new } Thread.current[NAMESPACE][name].dup end |
#metadata(*pairs, &block) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/logstuff.rb', line 83 def (*pairs, &block) original_fields = get_thread_current(:current_fields) || {} current_fields = original_fields.dup pairs.flatten.each do |pair| pair.each do |k, v| current_fields[k.to_sym] = v end end set_thread_current(:current_fields, current_fields) yield set_thread_current(:current_fields, original_fields) end |
#new_logger(path, loglevel) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/logstuff.rb', line 105 def new_logger(path, loglevel) if path.class == String FileUtils.touch path # prevent autocreate messages in log end newlogger = Logger.new path newlogger.level = loglevel unless loglevel.nil? newlogger end |
#set_thread_current(name, value) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/logstuff.rb', line 24 def set_thread_current(name, value) Thread.current[NAMESPACE] ||= { :current_fields => {}, :current_tags => Set.new } Thread.current[NAMESPACE][name] = value.dup end |
#setup(params) ⇒ Object
97 98 99 100 101 102 |
# File 'lib/logstuff.rb', line 97 def setup(params) require 'logstash-event' self.logger_path = params[:path] || "logstuff.json" self.logger = params[:logger] || new_logger(self.logger_path, params[:loglevel]) self.source = params[:source] || 'logstuff' end |
#tag(*tags, &block) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/logstuff.rb', line 74 def tag(*, &block) = get_thread_current(:current_tags) = .dup + .flatten set_thread_current(:current_tags, ) yield set_thread_current(:current_tags, ) end |