lib/influxdb-logger/logger.rb in influxdb-logger-1.0.2 vs lib/influxdb-logger/logger.rb in influxdb-logger-1.0.3
- old
+ new
@@ -31,26 +31,26 @@
def to_ms
(self.to_r * 1000).to_i
end
end
-module InfluxdbLogger
- def log_to_file(message) # for test
- open("#{Rails.root}/log/my.log", 'a') { |f|
- f.puts message.inspect
- }
- end
+def log_to_file(message) # for test
+ open("#{Rails.root}/log/my.log", 'a') { |f|
+ f.puts message.inspect
+ }
+end
+module InfluxdbLogger
module Logger
-
# Severity label for logging. (max 5 char)
SEV_LABEL = %w(DEBUG INFO WARN ERROR FATAL ANY)
- def self.new(async: true, log_tags: {}, settings: {}, batch_size: 1000, interval: 1000)
- Rails.application.config.log_tags = log_tags.values
+ def self.new(async: true, tags: {}, fields: {}, settings: {}, batch_size: 1000, interval: 1000)
+ all_tags = tags.values + fields.values
+ Rails.application.config.log_tags = all_tags
if Rails.application.config.respond_to?(:action_cable)
- Rails.application.config.action_cable.log_tags = log_tags.values.map do |x|
+ Rails.application.config.action_cable.log_tags = all_tags.map do |x|
case
when x.respond_to?(:call)
x
when x.is_a?(Symbol)
-> (request) { request.send(x) }
@@ -67,11 +67,11 @@
settings[:batch_size] ||= batch_size
settings[:interval] ||= interval
settings[:async] = async
level = SEV_LABEL.index(Rails.application.config.log_level.to_s.upcase)
- logger = InfluxdbLogger::InnerLogger.new(settings, level, log_tags)
+ logger = InfluxdbLogger::InnerLogger.new(settings, level, tags, fields)
logger = ActiveSupport::TaggedLogging.new(logger)
logger.extend self
end
def self.parse_url(influxdb_url)
@@ -98,11 +98,11 @@
flush
end
end
class InnerLogger < ActiveSupport::Logger
- def initialize(options, level, log_tags)
+ def initialize(options, level, tags, fields)
self.level = level
@messages_type = (options[:messages_type] || :array).to_sym
@tag = options[:tag]
@severity_key = (options[:severity_key] || :severity).to_sym
@batch_size = options[:batch_size]
@@ -120,11 +120,12 @@
discard_write_errors: true
)
@severity = 0
@messages = []
- @log_tags = log_tags
+ @all_tags = tags.merge(fields)
+ @fields = fields
after_initialize if respond_to? :after_initialize
end
def [](key)
@global_tags[key]
@@ -181,19 +182,19 @@
end
tags = @global_tags.clone
if @tags
- @log_tags.keys.zip(@tags).each do |k, v|
+ @all_tags.keys.zip(@tags).each do |k, v|
tags[k] = v
end
end
message = {
series: @series,
timestamp: Time.now.to_precision(@time_precision),
- tags: tags,
- values: values.merge({
+ tags: tags.except(@fields.keys),
+ values: values.merge(tags.slice(@fields.keys)).merge({
severity: format_severity(@severity)
}).transform_values {|value|
case value
when ::Numeric, ::String
value