syslog-ml-logger 1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/syslog-ml-logger.rb +165 -0
  2. metadata +64 -0
@@ -0,0 +1,165 @@
1
+ require 'syslog'
2
+ require 'logger'
3
+ require 'syslog-formatter'
4
+
5
+ class Logger::Syslog
6
+ include Logger::Severity
7
+
8
+ # The version of Logger::Syslog you are using.
9
+ VERSION = '1.6.8'
10
+
11
+ # From 'man syslog.h':
12
+ # LOG_EMERG A panic condition was reported to all processes.
13
+ # LOG_ALERT A condition that should be corrected immediately.
14
+ # LOG_CRIT A critical condition.
15
+ # LOG_ERR An error message.
16
+ # LOG_WARNING A warning message.
17
+ # LOG_NOTICE A condition requiring special handling.
18
+ # LOG_INFO A general information message.
19
+ # LOG_DEBUG A message useful for debugging programs.
20
+
21
+ # From logger rdoc:
22
+ # FATAL: an unhandleable error that results in a program crash
23
+ # ERROR: a handleable error condition
24
+ # WARN: a warning
25
+ # INFO: generic (useful) information about system operation
26
+ # DEBUG: low-level information for developers
27
+
28
+ # Maps Logger warning types to syslog(3) warning types.
29
+ LOGGER_MAP = {
30
+ :unknown => :alert,
31
+ :fatal => :crit,
32
+ :error => :err,
33
+ :warn => :warning,
34
+ :info => :info,
35
+ :debug => :debug
36
+ }
37
+
38
+ # Maps Logger log levels to their values so we can silence.
39
+ LOGGER_LEVEL_MAP = {}
40
+
41
+ LOGGER_MAP.each_key do |key|
42
+ LOGGER_LEVEL_MAP[key] = Logger.const_get key.to_s.upcase
43
+ end
44
+
45
+ # Maps Logger log level values to syslog log levels.
46
+ LEVEL_LOGGER_MAP = {}
47
+
48
+ LOGGER_LEVEL_MAP.invert.each do |level, severity|
49
+ LEVEL_LOGGER_MAP[level] = LOGGER_MAP[severity]
50
+ end
51
+
52
+ # Builds a methods for level +meth+.
53
+ for severity in Logger::Severity.constants
54
+ class_eval <<-EOT, __FILE__, __LINE__
55
+ def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block)
56
+ add(#{severity}, message, progname, &block) # add(DEBUG, message, progname, &block)
57
+ end # end
58
+ #
59
+ def #{severity.downcase}? # def debug?
60
+ @level <= #{severity} # @level <= DEBUG
61
+ end # end
62
+ EOT
63
+ end
64
+
65
+ # Log level for Logger compatibility.
66
+ attr_accessor :level
67
+
68
+ # Logging program name.
69
+ attr_accessor :progname
70
+
71
+ # Logging date-time format (string passed to +strftime+).
72
+ def datetime_format=(datetime_format)
73
+ @default_formatter.datetime_format = datetime_format
74
+ end
75
+
76
+ def datetime_format
77
+ @default_formatter.datetime_format
78
+ end
79
+
80
+ # Logging formatter. formatter#call is invoked with 4 arguments; severity,
81
+ # time, progname and msg for each log. Bear in mind that time is a Time and
82
+ # msg is an Object that user passed and it could not be a String. It is
83
+ # expected to return a logdev#write-able Object. Default formatter is used
84
+ # when no formatter is set.
85
+ attr_accessor :formatter
86
+
87
+ alias sev_threshold level
88
+ alias sev_threshold= level=
89
+
90
+ # Fills in variables for Logger compatibility. If this is the first
91
+ # instance of Logger::Syslog, +program_name+ may be set to change the logged
92
+ # program name and +facility+ may be set to specify a custom facility
93
+ # with your syslog daemon.
94
+ #
95
+ # Due to the way syslog works, only one program name may be chosen.
96
+ def initialize(program_name = 'rails', facility = Syslog::LOG_USER, logopts=nil)
97
+ @default_formatter = Logger::SyslogFormatter.new
98
+ @formatter = nil
99
+ @progname = nil
100
+ @level = Logger::DEBUG
101
+
102
+ return if defined? SYSLOG
103
+ self.class.const_set :SYSLOG, Syslog.open(program_name, logopts, facility)
104
+ end
105
+
106
+ # Almost duplicates Logger#add. +progname+ is ignored.
107
+ def add(severity, message = nil, progname = nil, &block)
108
+ severity ||= Logger::UNKNOWN
109
+ if severity < @level
110
+ return true
111
+ end
112
+ if message.nil?
113
+ if block_given?
114
+ message = yield
115
+ else
116
+ message = progname
117
+ progname = @progname
118
+ end
119
+ end
120
+
121
+ # breakup multiple lines into multiple syslog messages
122
+ message.each_line do | msg |
123
+ SYSLOG.send(LEVEL_LOGGER_MAP[severity], format_message(format_severity(severity), Time.now, progname, clean(msg.chop)))
124
+ end
125
+ true
126
+ end
127
+
128
+ # Allows messages of a particular log level to be ignored temporarily.
129
+ def silence(temporary_level = Logger::ERROR)
130
+ old_logger_level = @level
131
+ @level = temporary_level
132
+ yield
133
+ ensure
134
+ @level = old_logger_level
135
+ end
136
+
137
+ # In Logger, this dumps the raw message; the closest equivalent
138
+ # would be Logger::UNKNOWN
139
+ def <<(message)
140
+ add(Logger::UNKNOWN, message)
141
+ end
142
+
143
+ private
144
+
145
+ # Severity label for logging. (max 5 char)
146
+ SEV_LABEL = %w(DEBUG INFO WARN ERROR FATAL ANY)
147
+
148
+ def format_severity(severity)
149
+ SEV_LABEL[severity] || 'ANY'
150
+ end
151
+
152
+ def format_message(severity, datetime, progname, msg)
153
+ (@formatter || @default_formatter).call(severity, datetime, progname, msg)
154
+ end
155
+
156
+ # Clean up messages so they're nice and pretty.
157
+ def clean(message)
158
+ message = message.to_s.dup
159
+ message.strip!
160
+ message.gsub!(/%/, '%%') # syslog(3) freaks on % (printf)
161
+ message.gsub!(/\e\[[^m]*m/, '') # remove useless ansi color codes
162
+ return message
163
+ end
164
+
165
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: syslog-ml-logger
3
+ version: !ruby/object:Gem::Version
4
+ hash: 13
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 1
9
+ version: "1.1"
10
+ platform: ruby
11
+ authors:
12
+ - Daniel van den Oord
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-07-31 00:00:00 Z
18
+ dependencies: []
19
+
20
+ description: A frok of ngmoco/syslog_logger adding support for multiple lines
21
+ email: daniel304@chello.nl
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - lib/syslog-ml-logger.rb
30
+ homepage: http://rubygems.org/gems/syslog-ml-logger
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ hash: 3
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.24
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Multiline syslog logger
63
+ test_files: []
64
+