lib/semantic_logger/appender/syslog.rb in semantic_logger-3.2.1 vs lib/semantic_logger/appender/syslog.rb in semantic_logger-3.3.0

- old
+ new

@@ -18,14 +18,12 @@ # appender: :syslog, # url: 'udp://myloghost:514' # ) module SemanticLogger module Appender - class Syslog < SemanticLogger::Appender::Base + class Syslog < SemanticLogger::Subscriber - attr_reader :remote_syslog, :url, :server, :port, :protocol, :facility, :host, :application - # Default mapping of ruby log levels to syslog log levels # # ::Syslog::LOG_EMERG - "System is unusable" # ::Syslog::LOG_ALERT - "Action needs to be taken immediately" # ::Syslog::LOG_CRIT - "A critical condition has occurred" @@ -40,10 +38,11 @@ warn: ::Syslog::LOG_WARNING, info: ::Syslog::LOG_NOTICE, debug: ::Syslog::LOG_INFO, trace: ::Syslog::LOG_DEBUG } + attr_reader :remote_syslog, :url, :server, :port, :protocol, :facility # Create a Syslog appender instance. # # Parameters # url: [String] @@ -147,21 +146,19 @@ # :json uses the CEE JSON Syslog format # Example: "@cee: #{JSON.dump(data)}" # Default: :syslog def initialize(options = {}, &block) options = options.dup - @application = options.delete(:application) || options.delete(:ident) || 'ruby' @options = options.delete(:options) || (::Syslog::LOG_PID | ::Syslog::LOG_CONS) @facility = options.delete(:facility) || ::Syslog::LOG_USER level_map = options.delete(:level_map) @url = options.delete(:url) || options.delete(:server) || 'syslog://localhost' uri = URI(@url) @server = uri.host || 'localhost' @protocol = (uri.scheme || :syslog).to_sym @port = uri.port || 514 @server = 'localhost' if @protocol == :syslog - @host = options.delete(:host) || options.delete(:local_hostname) || SemanticLogger.host @tcp_client_options = options.delete(:tcp_client) raise "Unknown protocol #{@protocol}!" unless [:syslog, :tcp, :udp].include?(@protocol) @level_map = DEFAULT_LEVEL_MAP.dup @@ -185,21 +182,20 @@ raise 'Missing gem: net_tcp_client. This gem is required when logging over TCP. To fix this error: gem install net_tcp_client' end end end - reopen - super(options, &block) + reopen end # After forking an active process call #reopen to re-open # open the handles to resources def reopen case @protocol when :syslog - ::Syslog.open(@application, @options, @facility) + ::Syslog.open(application, @options, @facility) when :tcp # Use the local logger for @remote_syslog so errors with the remote logger can be recorded locally. @tcp_client_options[:logger] = SemanticLogger::Logger.logger @remote_syslog = Net::TCPClient.new(@tcp_client_options) when :udp @@ -262,16 +258,35 @@ message << log.backtrace_to_s end message end + private + + # Extract Syslog formatter options + def format_options(options, protocol, &block) + opts = options.delete(:options) + facility = options.delete(:facility) + level_map = options.delete(:level_map) + if formatter = options.delete(:formatter) + extract_formatter(formatter) + else + case protocol + when :syslog + extract_formatter(syslog: {options: opts, facility: facility, level_map: level_map}) + when :tcp, :udp + extract_formatter(syslog: {options: opts, facility: facility, level_map: level_map}) + end + end + end + # Format the syslog packet so it can be sent over TCP or UDP def syslog_packet_formatter(log) packet = SyslogProtocol::Packet.new - packet.hostname = @host + packet.hostname = host packet.facility = @facility packet.severity = @level_map[log.level] - packet.tag = @application + packet.tag = application.gsub(' ', '') packet.content = formatter.call(log, self) packet.time = log.time packet.to_s end end