/usr/lib/ruby/2.3.0/syslog/logger.rb is in libruby2.3 2.3.0-5ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | # frozen_string_literal: false
require 'syslog'
require 'logger'
##
# Syslog::Logger is a Logger work-alike that logs via syslog instead of to a
# file. You can use Syslog::Logger to aggregate logs between multiple
# machines.
#
# By default, Syslog::Logger uses the program name 'ruby', but this can be
# changed via the first argument to Syslog::Logger.new.
#
# NOTE! You can only set the Syslog::Logger program name when you initialize
# Syslog::Logger for the first time. This is a limitation of the way
# Syslog::Logger uses syslog (and in some ways, a limitation of the way
# syslog(3) works). Attempts to change Syslog::Logger's program name after
# the first initialization will be ignored.
#
# === Example
#
# The following will log to syslogd on your local machine:
#
# require 'syslog/logger'
#
# log = Syslog::Logger.new 'my_program'
# log.info 'this line will be logged via syslog(3)'
#
# Also the facility may be set to specify the facility level which will be used:
#
# log.info 'this line will be logged using Syslog default facility level'
#
# log_local1 = Syslog::Logger.new 'my_program', Syslog::LOG_LOCAL1
# log_local1.info 'this line will be logged using local1 facility level'
#
#
# You may need to perform some syslog.conf setup first. For a BSD machine add
# the following lines to /etc/syslog.conf:
#
# !my_program
# *.* /var/log/my_program.log
#
# Then touch /var/log/my_program.log and signal syslogd with a HUP
# (killall -HUP syslogd, on FreeBSD).
#
# If you wish to have logs automatically roll over and archive, see the
# newsyslog.conf(5) and newsyslog(8) man pages.
class Syslog::Logger
# Default formatter for log messages.
class Formatter
def call severity, time, progname, msg
clean msg
end
private
##
# Clean up messages so they're nice and pretty.
def clean message
message = message.to_s.strip
message.gsub!(/\e\[[0-9;]*m/, '') # remove useless ansi color codes
return message
end
end
##
# The version of Syslog::Logger you are using.
VERSION = '2.1.0'
##
# Maps Logger warning types to syslog(3) warning types.
#
# Messages from Ruby applications are not considered as critical as messages
# from other system daemons using syslog(3), so most messages are reduced by
# one level. For example, a fatal message for Ruby's Logger is considered
# an error for syslog(3).
LEVEL_MAP = {
::Logger::UNKNOWN => Syslog::LOG_ALERT,
::Logger::FATAL => Syslog::LOG_ERR,
::Logger::ERROR => Syslog::LOG_WARNING,
::Logger::WARN => Syslog::LOG_NOTICE,
::Logger::INFO => Syslog::LOG_INFO,
::Logger::DEBUG => Syslog::LOG_DEBUG,
}
##
# Returns the internal Syslog object that is initialized when the
# first instance is created.
def self.syslog
@@syslog
end
##
# Specifies the internal Syslog object to be used.
def self.syslog= syslog
@@syslog = syslog
end
##
# Builds a methods for level +meth+.
def self.make_methods meth
level = ::Logger.const_get(meth.upcase)
eval <<-EOM, nil, __FILE__, __LINE__ + 1
def #{meth}(message = nil, &block)
add(#{level}, message, &block)
end
def #{meth}?
@level <= #{level}
end
EOM
end
##
# :method: unknown
#
# Logs a +message+ at the unknown (syslog alert) log level, or logs the
# message returned from the block.
##
# :method: fatal
#
# Logs a +message+ at the fatal (syslog err) log level, or logs the message
# returned from the block.
##
# :method: error
#
# Logs a +message+ at the error (syslog warning) log level, or logs the
# message returned from the block.
##
# :method: warn
#
# Logs a +message+ at the warn (syslog notice) log level, or logs the
# message returned from the block.
##
# :method: info
#
# Logs a +message+ at the info (syslog info) log level, or logs the message
# returned from the block.
##
# :method: debug
#
# Logs a +message+ at the debug (syslog debug) log level, or logs the
# message returned from the block.
Logger::Severity::constants.each do |severity|
make_methods severity.downcase
end
##
# Log level for Logger compatibility.
attr_accessor :level
# Logging formatter, as a +Proc+ that will take four arguments and
# return the formatted message. The arguments are:
#
# +severity+:: The Severity of the log message.
# +time+:: A Time instance representing when the message was logged.
# +progname+:: The #progname configured, or passed to the logger method.
# +msg+:: The _Object_ the user passed to the log message; not necessarily a
# String.
#
# The block should return an Object that can be written to the logging
# device via +write+. The default formatter is used when no formatter is
# set.
attr_accessor :formatter
##
# The facility argument is used to specify what type of program is logging the message.
attr_accessor :facility
##
# Fills in variables for Logger compatibility. If this is the first
# instance of Syslog::Logger, +program_name+ may be set to change the logged
# program name. The +facility+ may be set to specify the facility level which will be used.
#
# Due to the way syslog works, only one program name may be chosen.
def initialize program_name = 'ruby', facility = nil
@level = ::Logger::DEBUG
@formatter = Formatter.new
@@syslog ||= Syslog.open(program_name)
@facility = (facility || @@syslog.facility)
end
##
# Almost duplicates Logger#add. +progname+ is ignored.
def add severity, message = nil, progname = nil, &block
severity ||= ::Logger::UNKNOWN
@level <= severity and
@@syslog.log( (LEVEL_MAP[severity] | @facility), '%s', formatter.call(severity, Time.now, progname, (message || block.call)) )
true
end
end
|