Project

General

Profile

Bug #7065 ยป syslog_formatter.patch

tenderlovemaking (Aaron Patterson), 09/25/2012 07:14 AM

View differences:

ChangeLog
1
Tue Sep 25 06:25:44 2012  Aaron Patterson <aaron@tenderlovemaking.com>
2

  
3
	* ext/syslog/lib/syslog/logger.rb: add a formatter to the
4
	  Syslog::Logger object.
5
	* test/syslog/test_syslog_logger.rb: corresponding test
6

  
1 7
Mon Sep 24 17:36:51 2012  Nobuyoshi Nakada  <nobu@ruby-lang.org>
2 8

  
3 9
	* compile.c (defined_expr), insns.def (defined): share single frozen
ext/syslog/lib/syslog/logger.rb
37 37
# newsyslog.conf(5) and newsyslog(8) man pages.
38 38

  
39 39
class Syslog::Logger
40
  # Default formatter for log messages.
41
  class Formatter
42
    def call severity, time, progname, msg
43
      clean msg
44
    end
45

  
46
    private
47

  
48
    ##
49
    # Clean up messages so they're nice and pretty.
50

  
51
    def clean message
52
      message = message.to_s.strip
53
      message.gsub!(/\e\[[0-9;]*m/, '') # remove useless ansi color codes
54
      return message
55
    end
56
  end
40 57

  
41 58
  ##
42 59
  # The version of Syslog::Logger you are using.
......
136 153

  
137 154
  attr_accessor :level
138 155

  
156
  # Logging formatter, as a +Proc+ that will take four arguments and
157
  # return the formatted message. The arguments are:
158
  #
159
  # +severity+:: The Severity of the log message.
160
  # +time+:: A Time instance representing when the message was logged.
161
  # +progname+:: The #progname configured, or passed to the logger method.
162
  # +msg+:: The _Object_ the user passed to the log message; not necessarily a
163
  #         String.
164
  #
165
  # The block should return an Object that can be written to the logging
166
  # device via +write+.  The default formatter is used when no formatter is
167
  # set.
168
  attr_accessor :formatter
169

  
139 170
  ##
140 171
  # Fills in variables for Logger compatibility.  If this is the first
141 172
  # instance of Syslog::Logger, +program_name+ may be set to change the logged
......
145 176

  
146 177
  def initialize program_name = 'ruby'
147 178
    @level = ::Logger::DEBUG
179
    @formatter = Formatter.new
148 180

  
149 181
    @@syslog ||= Syslog.open(program_name)
150 182
  end
......
155 187
  def add severity, message = nil, progname = nil, &block
156 188
    severity ||= ::Logger::UNKNOWN
157 189
    @level <= severity and
158
      @@syslog.log LEVEL_MAP[severity], '%s', clean(message || block.call)
190
      @@syslog.log LEVEL_MAP[severity], '%s', formatter.call(severity, Time.now, progname, (message || block.call))
159 191
    true
160 192
  end
161

  
162
  private
163

  
164
  ##
165
  # Clean up messages so they're nice and pretty.
166

  
167
  def clean message
168
    message = message.to_s.strip
169
    message.gsub!(/\e\[[0-9;]*m/, '') # remove useless ansi color codes
170
    return message
171
  end
172

  
173 193
end
174 194

  
test/syslog/test_syslog_logger.rb
29 29
      end
30 30

  
31 31
      def log(level, format, *args)
32
        @line = "#{LEVEL_LABEL_MAP[level]} - \#{format % args}"
32
        @line = "#{LEVEL_LABEL_MAP[level]} - #{format % args}"
33 33
      end
34 34

  
35 35
      attr_reader :line
......
92 92
    assert_equal Logger::DEBUG, @logger.level
93 93
  end
94 94

  
95
  def test_custom_formatter
96
    @logger.formatter = Class.new {
97
      def call severity, time, progname, msg
98
        "hi mom!"
99
      end
100
    }.new
101

  
102
    assert_match(/hi mom!/, log_raw(:fatal, 'fatal level message'))
103
  end
104

  
95 105
  def test_add
96 106
    msg = log_add nil,           'unknown level message' # nil == unknown
97 107
    assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity