lib/semantic_logger/appender/syslog.rb in semantic_logger-2.15.0 vs lib/semantic_logger/appender/syslog.rb in semantic_logger-2.16.0

- old
+ new

@@ -46,16 +46,16 @@ # ::Syslog::LOG_WARNING - "Warning of a possible problem" # ::Syslog::LOG_NOTICE - "A normal but significant condition occurred" # ::Syslog::LOG_INFO - "Informational message" # ::Syslog::LOG_DEBUG - "Debugging information" DEFAULT_LEVEL_MAP = { - :fatal => ::Syslog::LOG_CRIT, - :error => ::Syslog::LOG_ERR, - :warn => ::Syslog::LOG_WARNING, - :info => ::Syslog::LOG_NOTICE, - :debug => ::Syslog::LOG_INFO, - :trace => ::Syslog::LOG_DEBUG + fatal: ::Syslog::LOG_CRIT, + error: ::Syslog::LOG_ERR, + warn: ::Syslog::LOG_WARNING, + info: ::Syslog::LOG_NOTICE, + debug: ::Syslog::LOG_INFO, + trace: ::Syslog::LOG_DEBUG } # For more information on the Syslog constants used below see http://ruby-doc.org/stdlib-2.0.0/libdoc/syslog/rdoc/Syslog.html # Parameters # @@ -104,22 +104,22 @@ # Default: SemanticLogger's log level. # The minimum level at which this appender will write logs. Any log messages below this level will be ignored. # # :level_map [Hash] # Supply a custom map of SemanticLogger levels to syslog levels. - # For example, passing in { :warn => ::Syslog::LOG_NOTICE } + # For example, passing in { warn: ::Syslog::LOG_NOTICE } # would result in a log mapping that matches the default level map, # except for :warn, which ends up with a LOG_NOTICE level instead of a # LOG_WARNING one. # Without overriding any parameters, the level map will be # LEVEL_MAP = { - # :fatal => ::Syslog::LOG_CRIT, - # :error => ::Syslog::LOG_ERR, - # :warn => ::Syslog::LOG_WARNING, - # :info => ::Syslog::LOG_NOTICE, - # :debug => ::Syslog::LOG_INFO, - # :trace => ::Syslog::LOG_DEBUG + # fatal: ::Syslog::LOG_CRIT, + # error: ::Syslog::LOG_ERR, + # warn: ::Syslog::LOG_WARNING, + # info: ::Syslog::LOG_NOTICE, + # debug: ::Syslog::LOG_INFO, + # trace: ::Syslog::LOG_DEBUG # } # # :local_hostname [String] # Default: Socket.gethostname || `hostname`.strip # Hostname to provide to the remote syslog. @@ -141,31 +141,31 @@ # Only used with the TCP protocol. # Specify custom parameters to pass into Net::TCPClient.new # For a list of options see the net_tcp_client documentation: # https://www.omniref.com/ruby/gems/net_tcp_client/1.0.0/symbols/Net::TCPClient/initialize def initialize(params = {}, &block) - params = params.dup - @ident = params.delete(:ident) || 'ruby' - @options = params.delete(:options) || (::Syslog::LOG_PID | ::Syslog::LOG_CONS) - @facility = params.delete(:facility) || ::Syslog::LOG_USER - filter = params.delete(:filter) - level = params.delete(:level) - level_map = params.delete(:level_map) - @level_map = DEFAULT_LEVEL_MAP.dup + params = params.dup + @ident = params.delete(:ident) || 'ruby' + @options = params.delete(:options) || (::Syslog::LOG_PID | ::Syslog::LOG_CONS) + @facility = params.delete(:facility) || ::Syslog::LOG_USER + filter = params.delete(:filter) + level = params.delete(:level) + level_map = params.delete(:level_map) + @level_map = DEFAULT_LEVEL_MAP.dup @level_map.update(level_map) if level_map - @server = params.delete(:server) || 'syslog://localhost' - uri = URI(@server) - @host = uri.host || 'localhost' - @protocol = (uri.scheme || :syslog).to_sym + @server = params.delete(:server) || 'syslog://localhost' + uri = URI(@server) + @host = uri.host || 'localhost' + @protocol = (uri.scheme || :syslog).to_sym raise "Unknown protocol #{@protocol}!" unless [:syslog, :tcp, :udp].include?(@protocol) @host = 'localhost' if @protocol == :syslog @port = URI(@server).port || 514 @local_hostname = params.delete(:local_hostname) || Socket.gethostname || `hostname`.strip @tcp_client_options = params.delete(:tcp_client) # Warn about any unknown configuration options. - params.each_pair { |key,val| SemanticLogger::Logger.logger.warn "Ignoring unknown configuration option: #{key.inspect} => #{val.inspect}" } + params.each_pair { |key, val| SemanticLogger::Logger.logger.warn "Ignoring unknown configuration option: #{key.inspect} => #{val.inspect}" } # The syslog_protocol gem is required when logging over TCP or UDP. if [:tcp, :udp].include?(@protocol) begin require 'syslog_protocol' @@ -173,11 +173,11 @@ raise 'Missing gem: syslog_protocol. This gem is required when logging over TCP or UDP. To fix this error: gem install syslog_protocol' end # The net_tcp_client gem is required when logging over TCP. if protocol == :tcp - @tcp_client_options ||= {} + @tcp_client_options ||= {} @tcp_client_options[:server] = "#{@host}:#{@port}" begin require 'net/tcp_client' rescue LoadError raise 'Missing gem: net_tcp_client. This gem is required when logging over TCP. To fix this error: gem install net_tcp_client' @@ -197,11 +197,11 @@ when :syslog ::Syslog.open(@ident, @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) + @remote_syslog = Net::TCPClient.new(@tcp_client_options) when :udp @remote_syslog = UDPSocket.new else raise "Unsupported protocol: #{@protocol}" end @@ -213,11 +213,11 @@ return false if (level_index > (log.level_index || 0)) || !include_message?(log) case @protocol when :syslog # Since the Ruby Syslog API supports sprintf format strings, double up all existing '%' - message = formatter.call(log).gsub "%", "%%" + message = formatter.call(log).gsub '%', '%%' ::Syslog.log @level_map[log.level], message when :tcp @remote_syslog.retry_on_connection_failure { @remote_syslog.write("#{syslog_packet_formatter(log)}\r\n") } when :udp @remote_syslog.send syslog_packet_formatter(log), 0, @host, @port @@ -236,24 +236,24 @@ def default_formatter Proc.new do |log| tags = log.tags.collect { |tag| "[#{tag}]" }.join(" ") + " " if log.tags && (log.tags.size > 0) message = log.message.to_s - message << " -- " << log.payload.inspect if log.payload - message << " -- " << "#{log.exception.class}: #{log.exception.message}\n#{(log.exception.backtrace || []).join("\n")}" if log.exception + message << ' -- ' << log.payload.inspect if log.payload + message << ' -- ' << "#{log.exception.class}: #{log.exception.message}\n#{(log.exception.backtrace || []).join("\n")}" if log.exception duration_str = log.duration ? "(#{'%.1f' % log.duration}ms) " : '' "#{log.level.to_s[0..0].upcase} [#{$$}:#{log.thread_name}] #{tags}#{duration_str}#{log.name} -- #{message}" 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 = SyslogProtocol::Packet.new packet.hostname = @local_hostname packet.facility = @facility - packet.severity = @level_map[log.level] + packet.severity = @level_map[log.level] packet.tag = @ident packet.content = default_formatter.call(log) packet.to_s end end