Class: SemanticLogger::Formatters::Syslog
- Includes:
- SyslogHeader
- Defined in:
- lib/semantic_logger/formatters/syslog.rb
Defined Under Namespace
Classes: LevelMap
Constant Summary collapse
- LOG_CRIT =
Syslog severities and facilities, with the same values as the matching
::Syslogconstants. Thesysloggem is only needed to log to a local syslog daemon, and since Ruby 3.4 it is a bundled gem, so remote syslog must not depend on it for these values. 2- LOG_ERR =
3- LOG_WARNING =
4- LOG_NOTICE =
5- LOG_INFO =
6- LOG_DEBUG =
7- LOG_KERN =
Syslog facilities, as syslog(3) defines them: the facility number shifted left by 3 bits.
0 << 3
- LOG_USER =
1 << 3
- LOG_MAIL =
2 << 3
- LOG_DAEMON =
3 << 3
- LOG_AUTH =
4 << 3
- LOG_SYSLOG =
5 << 3
- LOG_LPR =
6 << 3
- LOG_NEWS =
7 << 3
- LOG_UUCP =
8 << 3
- LOG_CRON =
9 << 3
- LOG_AUTHPRIV =
10 << 3
- LOG_FTP =
11 << 3
- LOG_LOCAL0 =
16 << 3
- LOG_LOCAL1 =
17 << 3
- LOG_LOCAL2 =
18 << 3
- LOG_LOCAL3 =
19 << 3
- LOG_LOCAL4 =
20 << 3
- LOG_LOCAL5 =
21 << 3
- LOG_LOCAL6 =
22 << 3
- LOG_LOCAL7 =
23 << 3
- DEFAULT_MAX_SIZE =
Default maximum size of a syslog packet, in bytes, as for the syslog appender.
1024- MAX_FACILITY =
The highest facility the syslog protocol can encode.
LOG_LOCAL7
Constants included from SyslogHeader
SemanticLogger::Formatters::SyslogHeader::DEFAULT_HOSTNAME, SemanticLogger::Formatters::SyslogHeader::DEFAULT_TAG, SemanticLogger::Formatters::SyslogHeader::MAX_TAG_SIZE
Constants inherited from Base
Base::CONTROL_CHARS, Base::CONTROL_CHAR_ESCAPES, Base::PRECISION, Base::UNICODE_CONTROL_CHARS
Instance Attribute Summary collapse
-
#facility ⇒ Object
Returns the value of attribute facility.
-
#level_map ⇒ Object
Returns the value of attribute level_map.
-
#max_size ⇒ Object
Returns the value of attribute max_size.
Attributes inherited from Base
#backtrace_cleaner, #escape_control_chars, #log, #log_application, #log_environment, #log_exception_details, #log_host, #logger, #precision, #time_format
Class Method Summary collapse
-
.facility_number(facility) ⇒ Object
Returns [Integer] the facility number (0 to 23) that the syslog protocol sends, for a facility given as documented: one of the
::Syslog::LOG_*facility constants, such as::Syslog::LOG_LOCAL0. -
.require_syslog_protocol ⇒ Object
Load the syslog_protocol gem, which builds the packets for remote syslog.
Instance Method Summary collapse
- #call(log, logger) ⇒ Object
-
#initialize(facility: LOG_USER, level_map: LevelMap.new, max_size: DEFAULT_MAX_SIZE, escape_control_chars: true, **args) ⇒ Syslog
constructor
Create a Syslog Log Formatter.
-
#time ⇒ Object
Time is part of the syslog packet and is not included in the formatted message.
Methods inherited from Default
#duration, #exception, #exception_details, #file_name_and_line, #level, #message, #method_name, #name, #named_tags, #payload, #process_info, #tags, #thread_name
Methods inherited from Base
Constructor Details
#initialize(facility: LOG_USER, level_map: LevelMap.new, max_size: DEFAULT_MAX_SIZE, escape_control_chars: true, **args) ⇒ Syslog
Create a Syslog Log Formatter
Parameters:
facility: [Integer]
One of the ::Syslog::LOG_* facility constants, for example ::Syslog::LOG_LOCAL0.
Default: LOG_USER
level_map: [Hash | SemanticLogger::Formatters::Syslog::LevelMap]
Supply a custom map of SemanticLogger levels to syslog levels.
escape_control_chars: [Boolean]
Replace control characters (newlines, the ANSI escape, etc.) in
untrusted log data with a printable escaped form so that they cannot
forge or split syslog records.
Default: true (unlike other formatters, since syslog frames records
with a separator)
max_size: [Integer]
Maximum size of each syslog packet, in bytes.
Default: DEFAULT_MAX_SIZE (1024)
Example:
# Change the warn level to LOG_NOTICE level instead of a the default of LOG_WARNING.
SemanticLogger.add_appender(appender: :syslog, level_map: {warn: ::Syslog::LOG_NOTICE})
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/semantic_logger/formatters/syslog.rb', line 134 def initialize(facility: LOG_USER, level_map: LevelMap.new, max_size: DEFAULT_MAX_SIZE, escape_control_chars: true, **args) self.class.require_syslog_protocol self.facility = facility self.level_map = level_map @max_size = max_size # Syslog frames records with a separator, so embedded newlines or other # control characters in untrusted log data can forge or split records. # Default to escaping them, overridable for backwards compatibility. super(escape_control_chars: escape_control_chars, **args) end |
Instance Attribute Details
#facility ⇒ Object
Returns the value of attribute facility.
7 8 9 |
# File 'lib/semantic_logger/formatters/syslog.rb', line 7 def facility @facility end |
#level_map ⇒ Object
Returns the value of attribute level_map.
7 8 9 |
# File 'lib/semantic_logger/formatters/syslog.rb', line 7 def level_map @level_map end |
#max_size ⇒ Object
Returns the value of attribute max_size.
6 7 8 |
# File 'lib/semantic_logger/formatters/syslog.rb', line 6 def max_size @max_size end |
Class Method Details
.facility_number(facility) ⇒ Object
Returns [Integer] the facility number (0 to 23) that the syslog protocol sends, for a facility
given as documented: one of the ::Syslog::LOG_* facility constants, such as
::Syslog::LOG_LOCAL0. syslog(3) defines each of those as the facility number shifted left
by 3 bits, so ::Syslog::LOG_USER is 8 and its facility number is 1.
Raises ArgumentError for any other value, including a bare facility number.
53 54 55 56 57 58 59 60 61 |
# File 'lib/semantic_logger/formatters/syslog.rb', line 53 def self.facility_number(facility) unless facility.is_a?(Integer) && (facility % 8).zero? && facility.between?(0, MAX_FACILITY) raise(ArgumentError, "Invalid syslog facility: #{facility.inspect}. Must be one of the ::Syslog::LOG_* facility " \ "constants, for example ::Syslog::LOG_USER or ::Syslog::LOG_LOCAL0.") end facility >> 3 end |
.require_syslog_protocol ⇒ Object
Load the syslog_protocol gem, which builds the packets for remote syslog. Called when a syslog formatter is created, so the gem is only needed by applications that log to a remote syslog server.
102 103 104 105 106 107 108 |
# File 'lib/semantic_logger/formatters/syslog.rb', line 102 def self.require_syslog_protocol require "syslog_protocol" rescue LoadError raise LoadError, "Gem syslog_protocol is required for remote logging using the Syslog protocol. " \ 'Please add the gem "syslog_protocol" to your Gemfile.' end |
Instance Method Details
#call(log, logger) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/semantic_logger/formatters/syslog.rb', line 165 def call(log, logger) # The individual fields are already escaped. Also escape the newlines that the # exception and its backtrace are laid out with, so that a record never spans more # than one line, which a receiver framing records by newline would split. = escape_control_characters(super) create_syslog_packet() end |
#time ⇒ Object
Time is part of the syslog packet and is not included in the formatted message.
161 162 163 |
# File 'lib/semantic_logger/formatters/syslog.rb', line 161 def time nil end |