syslogger 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/syslogger.rb +29 -24
  2. data/spec/syslogger_spec.rb +6 -0
  3. metadata +4 -8
data/lib/syslogger.rb CHANGED
@@ -2,11 +2,11 @@ require 'syslog'
2
2
  require 'logger'
3
3
 
4
4
  class Syslogger
5
-
6
- VERSION = "1.2.2"
7
-
5
+
6
+ VERSION = "1.2.3"
7
+
8
8
  attr_reader :level, :ident, :options, :facility
9
-
9
+
10
10
  MAPPING = {
11
11
  Logger::DEBUG => Syslog::LOG_DEBUG,
12
12
  Logger::INFO => Syslog::LOG_INFO,
@@ -15,15 +15,15 @@ class Syslogger
15
15
  Logger::FATAL => Syslog::LOG_ERR,
16
16
  Logger::UNKNOWN => Syslog::LOG_ALERT
17
17
  }
18
-
19
- #
18
+
19
+ #
20
20
  # Initializes default options for the logger
21
21
  # <tt>ident</tt>:: the name of your program [default=$0].
22
- # <tt>options</tt>:: syslog options [default=<tt>Syslog::LOG_PID | Syslog::LOG_CONS</tt>].
23
- # Correct values are:
24
- # LOG_CONS : writes the message on the console if an error occurs when sending the message;
25
- # LOG_NDELAY : no delay before sending the message;
26
- # LOG_PERROR : messages will also be written on STDERR;
22
+ # <tt>options</tt>:: syslog options [default=<tt>Syslog::LOG_PID | Syslog::LOG_CONS</tt>].
23
+ # Correct values are:
24
+ # LOG_CONS : writes the message on the console if an error occurs when sending the message;
25
+ # LOG_NDELAY : no delay before sending the message;
26
+ # LOG_PERROR : messages will also be written on STDERR;
27
27
  # LOG_PID : adds the process number to the message (just after the program name)
28
28
  # <tt>facility</tt>:: the syslog facility [default=nil] Correct values include:
29
29
  # Syslog::LOG_DAEMON
@@ -32,55 +32,60 @@ class Syslogger
32
32
  # Syslog::LOG_LOCAL2
33
33
  # Syslog::LOG_NEWS
34
34
  # etc.
35
- #
35
+ #
36
36
  # Usage:
37
37
  # logger = Syslogger.new("my_app", Syslog::LOG_PID | Syslog::LOG_CONS, Syslog::LOG_LOCAL0)
38
38
  # logger.level = Logger::INFO # use Logger levels
39
39
  # logger.warn "warning message"
40
40
  # logger.debug "debug message"
41
- #
41
+ #
42
42
  def initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil)
43
43
  @ident = ident
44
44
  @options = options || (Syslog::LOG_PID | Syslog::LOG_CONS)
45
45
  @facility = facility
46
46
  @level = Logger::INFO
47
47
  end
48
-
48
+
49
49
  %w{debug info warn error fatal unknown}.each do |logger_method|
50
50
  define_method logger_method.to_sym do |message|
51
51
  add(Logger.const_get(logger_method.upcase), message)
52
52
  end
53
-
53
+
54
54
  unless logger_method == 'unknown'
55
55
  define_method "#{logger_method}?".to_sym do
56
56
  @level <= Logger.const_get(logger_method.upcase)
57
57
  end
58
58
  end
59
59
  end
60
-
60
+
61
61
  # Logs a message at the Logger::INFO level.
62
62
  def <<(msg)
63
63
  add(Logger::INFO, msg)
64
64
  end
65
-
65
+
66
66
  # Low level method to add a message.
67
67
  # +severity+:: the level of the message. One of Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR, Logger::FATAL, Logger::UNKNOWN
68
68
  # +message+:: the message string. If nil, the method will call the block and use the result as the message string.
69
69
  # +progname+:: optionally, a overwrite the program name that appears in the log message.
70
70
  def add(severity, message = nil, progname = nil, &block)
71
71
  progname ||= @ident
72
- Syslog.open(progname, @options, @facility) { |s|
72
+ Syslog.open(progname, @options, @facility) { |s|
73
73
  s.mask = Syslog::LOG_UPTO(MAPPING[@level])
74
- # substitute '%' for '%%' before logging
75
- # so that syslog won't complain about malformed characters
76
- s.log(MAPPING[severity], (message || block.call).to_s.gsub(/%/, '%%'))
74
+ s.log(MAPPING[severity], clean(message || block.call))
77
75
  }
78
76
  end
79
-
77
+
80
78
  # Sets the minimum level for messages to be written in the log.
81
79
  # +level+:: one of <tt>Logger::DEBUG</tt>, <tt>Logger::INFO</tt>, <tt>Logger::WARN</tt>, <tt>Logger::ERROR</tt>, <tt>Logger::FATAL</tt>, <tt>Logger::UNKNOWN</tt>
82
80
  def level=(level)
83
81
  @level = level
84
82
  end
85
-
86
- end
83
+
84
+ protected
85
+
86
+ # Borrowed from SyslogLogger.
87
+ def clean(message)
88
+ # syslog(3) freaks on % (printf)
89
+ message.strip.gsub(/%/, '%%')
90
+ end
91
+ end
@@ -57,6 +57,12 @@ describe "Syslogger" do
57
57
  syslog.should_receive(:log).with(Syslog::LOG_INFO, "%%me%%ssage%%")
58
58
  @logger.add(Logger::INFO, "%me%ssage%")
59
59
  end
60
+
61
+ it "should strip the :message" do
62
+ Syslog.stub(:open).and_yield(syslog=mock("syslog", :mask= => true))
63
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "message")
64
+ @logger.add(Logger::INFO, "\n\nmessage ")
65
+ end
60
66
  end # describe "add"
61
67
 
62
68
  describe ":level? methods" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 2
8
- - 2
9
- version: 1.2.2
8
+ - 3
9
+ version: 1.2.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Cyril Rohr
@@ -14,14 +14,13 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-06 00:00:00 +01:00
17
+ date: 2011-02-07 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
24
  requirements:
26
25
  - - ~>
27
26
  - !ruby/object:Gem::Version
@@ -35,7 +34,6 @@ dependencies:
35
34
  name: rspec
36
35
  prerelease: false
37
36
  requirement: &id002 !ruby/object:Gem::Requirement
38
- none: false
39
37
  requirements:
40
38
  - - ~>
41
39
  - !ruby/object:Gem::Version
@@ -71,7 +69,6 @@ rdoc_options:
71
69
  require_paths:
72
70
  - lib
73
71
  required_ruby_version: !ruby/object:Gem::Requirement
74
- none: false
75
72
  requirements:
76
73
  - - ">="
77
74
  - !ruby/object:Gem::Version
@@ -80,7 +77,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
77
  - 8
81
78
  version: "1.8"
82
79
  required_rubygems_version: !ruby/object:Gem::Requirement
83
- none: false
84
80
  requirements:
85
81
  - - ">="
86
82
  - !ruby/object:Gem::Version
@@ -91,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
87
  requirements: []
92
88
 
93
89
  rubyforge_project:
94
- rubygems_version: 1.3.7
90
+ rubygems_version: 1.3.6
95
91
  signing_key:
96
92
  specification_version: 3
97
93
  summary: Dead simple Ruby Syslog logger