class SemanticLogger::Appender::Syslog
def default_formatter
def default_formatter if protocol == :syslog # Format is text output without the time SemanticLogger::Formatters::Default.new(time_format: :notime) else SemanticLogger::Formatters::Syslog.new(facility: facility, level_map: level_map, max_size: max_size) end end
def flush
def flush @remote_syslog.flush if @remote_syslog.respond_to?(:flush) end
def initialize(url: "syslog://localhost",
# Change the warn level to LOG_NOTICE level instead of a the default of LOG_WARNING.
Example:
Supply a custom map of SemanticLogger levels to syslog levels.
level_map: [Hash | SemanticLogger::Formatters::Syslog::LevelMap]
::Syslog::LOG_LOCAL7
::Syslog::LOG_LOCAL6
::Syslog::LOG_LOCAL5
::Syslog::LOG_LOCAL4
::Syslog::LOG_LOCAL3
::Syslog::LOG_LOCAL2
::Syslog::LOG_LOCAL1
::Syslog::LOG_LOCAL0
::Syslog::LOG_UUCP
::Syslog::LOG_USER
::Syslog::LOG_SYSLOG
::Syslog::LOG_SECURITY
::Syslog::LOG_NTP
::Syslog::LOG_NEWS
::Syslog::LOG_MAIL
::Syslog::LOG_LRP
::Syslog::LOG_KERN
::Syslog::LOG_FTP
::Syslog::LOG_DAEMON
::Syslog::LOG_CRON
::Syslog::LOG_CONSOLE
::Syslog::LOG_AUTHPRIV
::Syslog::LOG_AUTH
Type of program (can be logically OR'd together)
Default: ::Syslog::LOG_USER
facility: [Integer]
I.e. When `url: 'syslog://localhost'`
- Only applicable when logging to a local syslog instance.
Note:
::Syslog::LOG_PID
::Syslog::LOG_PERROR
::Syslog::LOG_ODELAY
::Syslog::LOG_NOWAIT
::Syslog::LOG_NDELAY
::Syslog::LOG_CONS
Any of the following (options can be logically OR'd together)
Default: ::Syslog::LOG_PID | ::Syslog::LOG_CONS
options: [Integer]
Default: 1024 bytes
Set your own packet size.
max_size: [Integer]
Default: SemanticLogger.application
Identity of the program.
application: [String]
The Proc must return true or false.
Proc: Only include log messages where the supplied Proc returns true
regular expression. All other messages will be ignored.
RegExp: Only include log messages where the class name matches the supplied.
filter: [Regexp|Proc]
Default: SemanticLogger.default_level
Override the log level for this appender.
level: [:trace | :debug | :info | :warn | :error | :fatal]
https://github.com/reidmorrison/net_tcp_client/blob/master/lib/net/tcp_client/tcp_client.rb
For a list of options see the net_tcp_client documentation:
Specify custom parameters to pass into Net::TCPClient.new
Only used with the TCP protocol.
Default: {}
tcp_client: [Hash]
Default: SemanticLogger.host
Host name to provide to the remote syslog.
host: [String]
When using the :syslog protocol, logs will always be sent to the localhost syslog
UDP example with custom port: 'udp://logger:8514'
UDP example with default port: 'udp://logger'
TCP example with custom port: 'tcp://logger:8514'
TCP example with default port: 'tcp://logger'
local syslog example: 'syslog://localhost'
Uses port 514 by default for TCP and UDP.
URL of server: protocol://host:port
For writing logs to a remote syslog server
Default: 'syslog://localhost'
url: [String]
Parameters
Create a Syslog appender instance.
def initialize(url: "syslog://localhost", facility: ::Syslog::LOG_USER, max_size: 1024, level_map: SemanticLogger::Formatters::Syslog::LevelMap.new, options: ::Syslog::LOG_PID | ::Syslog::LOG_CONS, tcp_client: {}, **args, &block) @options = options @facility = facility @max_size = max_size @level_map = level_map @url = url uri = URI(@url) @server = uri.host || "localhost" @protocol = (uri.scheme || :syslog).to_sym @port = uri.port || 514 @server = "localhost" if @protocol == :syslog @tcp_client_options = tcp_client raise "Unknown protocol #{@protocol}!" unless %i[syslog tcp udp].include?(@protocol) # The syslog_protocol gem is required when logging over TCP or UDP. if %i[tcp udp].include?(@protocol) begin require "syslog_protocol" rescue LoadError raise LoadError, "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 begin require "net/tcp_client" rescue LoadError raise LoadError, "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 super(**args, &block) reopen end
def log(log)
def log(log) case @protocol when :syslog # Since the Ruby Syslog API supports sprintf format strings, double up all existing '%' message = formatter.call(log, self).gsub "%", "%%" ::Syslog.log @level_map[log.level], message when :tcp @remote_syslog.retry_on_connection_failure { @remote_syslog.write("#{formatter.call(log, self)}\r\n") } when :udp @remote_syslog.send(formatter.call(log, self), 0, @server, @port) else raise "Unsupported protocol: #{protocol}" end true end
def reopen
After forking an active process call #reopen to re-open
def reopen case @protocol when :syslog method = ::Syslog.opened? ? :reopen : :open ::Syslog.send(method, application, options, facility) when :tcp @tcp_client_options[:server] = "#{@server}:#{@port}" @remote_syslog = Net::TCPClient.new(**@tcp_client_options) # Use the local logger for @remote_syslog so errors with the remote logger can be recorded locally. @remote_syslog.logger = logger when :udp @remote_syslog = UDPSocket.new else raise "Unsupported protocol: #{@protocol}" end end