Class: Syslogger

Inherits:
Object
  • Object
show all
Defined in:
lib/syslogger.rb

Constant Summary collapse

VERSION =
"1.6.2"
MAPPING =
{
  Logger::DEBUG => Syslog::LOG_DEBUG,
  Logger::INFO => Syslog::LOG_INFO,
  Logger::WARN => Syslog::LOG_WARNING,
  Logger::ERROR => Syslog::LOG_ERR,
  Logger::FATAL => Syslog::LOG_CRIT,
  Logger::UNKNOWN => Syslog::LOG_ALERT
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil) ⇒ Syslogger

Initializes default options for the logger

ident

the name of your program [default=$0].

options

syslog options [default=Syslog::LOG_PID | Syslog::LOG_CONS]. Correct values are:

LOG_CONS    : writes the message on the console if an error occurs when sending the message;
LOG_NDELAY  : no delay before sending the message;
LOG_PERROR  : messages will also be written on STDERR;
LOG_PID     : adds the process number to the message (just after the program name)
facility

the syslog facility [default=nil] Correct values include:

Syslog::LOG_DAEMON
Syslog::LOG_USER
Syslog::LOG_SYSLOG
Syslog::LOG_LOCAL2
Syslog::LOG_NEWS
etc.

Usage:

logger = Syslogger.new("my_app", Syslog::LOG_PID | Syslog::LOG_CONS, Syslog::LOG_LOCAL0)
logger.level = Logger::INFO # use Logger levels
logger.warn "warning message"
logger.debug "debug message"
logger.info "my_subapp" { "Some lazily computed message" }


45
46
47
48
49
50
51
52
53
54
# File 'lib/syslogger.rb', line 45

def initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil)
  @ident = ident
  @options = options || (Syslog::LOG_PID | Syslog::LOG_CONS)
  @facility = facility
  @level = Logger::INFO
  @mutex = Mutex.new
  @formatter = proc do |severity, datetime, progname, msg|
    msg
  end
end

Instance Attribute Details

#facilityObject (readonly)

Returns the value of attribute facility.



9
10
11
# File 'lib/syslogger.rb', line 9

def facility
  @facility
end

#formatterObject

Returns the value of attribute formatter.



10
11
12
# File 'lib/syslogger.rb', line 10

def formatter
  @formatter
end

#identObject

Returns the value of attribute ident.



9
10
11
# File 'lib/syslogger.rb', line 9

def ident
  @ident
end

#levelObject

Returns the value of attribute level.



9
10
11
# File 'lib/syslogger.rb', line 9

def level
  @level
end

#max_octetsObject

Returns the value of attribute max_octets.



9
10
11
# File 'lib/syslogger.rb', line 9

def max_octets
  @max_octets
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/syslogger.rb', line 9

def options
  @options
end

Instance Method Details

#<<(msg) ⇒ Object

Logs a message at the Logger::INFO level.



78
79
80
# File 'lib/syslogger.rb', line 78

def <<(msg)
  add(Logger::INFO, msg)
end

#add(severity, message = nil, progname = nil, &block) ⇒ Object

Low level method to add a message.

severity

the level of the message. One of Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR, Logger::FATAL, Logger::UNKNOWN

message

the message string. If nil, the method will call the block and use the result as the message string. If both are nil or no block is given, it will use the progname as per the behaviour of both the standard Ruby logger, and the Rails BufferedLogger.

progname

optionally, overwrite the program name that appears in the log message.



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
# File 'lib/syslogger.rb', line 88

def add(severity, message = nil, progname = nil, &block)
  if message.nil? && block.nil? && !progname.nil?
    message, progname = progname, nil
  end
  progname ||= @ident

  @mutex.synchronize do
    Syslog.open(progname, @options, @facility) do |s|
      s.mask = Syslog::LOG_UPTO(MAPPING[@level])
      communication = clean(message || block && block.call)
      formatted_communication = formatter.call([severity], Time.now, progname, communication)
      if self.max_octets
        buffer = "#{tags_text}"
        formatted_communication.bytes do |byte|
          buffer.concat(byte)
          # if the last byte we added is potentially part of an escape, we'll go ahead and add another byte
          if buffer.bytesize >= self.max_octets && !['%'.ord,'\\'.ord].include?(byte)
            s.log(MAPPING[severity],buffer)
            buffer = ""
          end
        end
        s.log(MAPPING[severity],buffer) unless buffer.empty?
      else
        s.log(MAPPING[severity],"#{tags_text}#{formatted_communication}")
      end
    end
  end
end

#clear_tags!Object



157
158
159
# File 'lib/syslogger.rb', line 157

def clear_tags!
  current_tags.clear
end

#pop_tags(size = 1) ⇒ Object



153
154
155
# File 'lib/syslogger.rb', line 153

def pop_tags(size = 1)
  current_tags.pop size
end

#push_tags(*tags) ⇒ Object



147
148
149
150
151
# File 'lib/syslogger.rb', line 147

def push_tags(*tags)
  tags.flatten.reject{ |i| i.respond_to?(:empty?) ? i.empty? : !i }.tap do |new_tags|
    current_tags.concat new_tags
  end
end

#tagged(*tags) ⇒ Object

Tagging code borrowed from ActiveSupport gem



140
141
142
143
144
145
# File 'lib/syslogger.rb', line 140

def tagged(*tags)
  new_tags = push_tags(*tags)
  yield self
ensure
  pop_tags(new_tags.size)
end

#write(msg) ⇒ Object

Log a message at the Logger::INFO level. Useful for use with Rack::CommonLogger



73
74
75
# File 'lib/syslogger.rb', line 73

def write(msg)
  add(Logger::INFO, msg)
end