class Daemons::SyslogIO

This is a simple class meant to allow using syslog through an IO-like object. Code borrowed from github.com/phemmer/ruby-syslogio

The usage is simple:

require 'syslogio'
$stdout = SyslogIO.new("myapp", :local0, :info, $stdout)
$stderr = SyslogIO.new("myapp", :local0, :err, $stderr)
$stdout.puts "This is a message"
$stderr.puts "This is an error"
raise StandardError, 'This will get written through the SyslogIO for $stderr'

Attributes

sync[R]

Indicates whether synchonous IO is enabled. @return [Boolean]

Public Class Methods

new(*options) click to toggle source

Creates a new object. You can have as many SyslogIO objects as you like. However because they all share the same syslog connection, some parameters are shared. The identifier shared among all SyslogIO objects, and is set to the value of the last one created. The Syslog options are merged together as a combination of all objects. The facility and level are distinct between each though. If an IO object is provided as an argument, any text written to the SyslogIO object will also be passed through to that IO object.

@param identifier [String] Identifier @param facility [Fixnum<Syslog::Facility>] Syslog facility @param level [Fixnum<Syslog::Level>] Syslog level @param option [Fixnum<Syslog::Options>] Syslog option @param passthrough [IO] IO passthrough

# File lib/daemons/syslogio.rb, line 58
def initialize(*options)
  options.each do |option|
    if option.is_a?(String)
      @ident = option
    elsif value = self.class.syslog_facility(option)
      @facility = value
    elsif value = self.class.syslog_level(option)
      @level = value
    elsif value = self.class.syslog_option(option)
      @options = 0 if @options.nil?
      @options |= value
    elsif option.is_a?(IO)
      @out = option
    else
      raise ArgumentError, "Unknown argument #{option.inspect}"
    end
  end

  @options ||= 0
  @ident ||= $0.sub(/.*\//, '')
  @facility ||= Syslog::LOG_USER
  @level ||= Syslog::LOG_INFO

  if Syslog.opened? then
    options = Syslog.options | @options
    @syslog = Syslog.reopen(@ident, options, @facility)
  else
    @syslog = Syslog.open(@ident, @options, @facility)
  end

  @subs = []
  @sync = false
  @buffer = ''

  at_exit { flush }
end
syslog_constant(option) click to toggle source

@!visibility private

# File lib/daemons/syslogio.rb, line 29
def self.syslog_constant(option)
  return unless option = syslog_constant_sym(option)
  return Syslog.constants.include?(option) ? Syslog.const_get(option) : nil
end
syslog_constant_sym(option) click to toggle source

@!visibility private

# File lib/daemons/syslogio.rb, line 21
def self.syslog_constant_sym(option)
  return unless option.is_a?(Symbol) or option.is_a?(String)
  option = option.to_s.upcase
  option = "LOG_#{option}" unless option[0..4] == 'LOG_'
  option = option.to_sym
  option
end
syslog_facility(option) click to toggle source

@!visibility private

# File lib/daemons/syslogio.rb, line 34
def self.syslog_facility(option)
  return unless option = syslog_constant_sym(option)
  return Syslog::Facility.constants.include?(option) ? Syslog.const_get(option) : nil
end
syslog_level(option) click to toggle source

@!visibility private

# File lib/daemons/syslogio.rb, line 39
def self.syslog_level(option)
  return unless option = syslog_constant_sym(option)
  return Syslog::Level.constants.include?(option) ? Syslog.const_get(option) : nil
end
syslog_option(option) click to toggle source

@!visibility private

# File lib/daemons/syslogio.rb, line 44
def self.syslog_option(option)
  return unless option = syslog_constant_sym(option)
  return Syslog::Option.constants.include?(option) ? Syslog.const_get(option) : nil
end

Public Instance Methods

<<(text)
Alias for: write
crit(text) click to toggle source

Log at the critical level

Shorthand for {#log}(text, Syslog::LOG_CRIT)

# File lib/daemons/syslogio.rb, line 195
def crit(text)
  log(text, Syslog::LOG_CRIT)
end
Also aliased as: fatal
debug(text) click to toggle source

Log at the debug level

Shorthand for {#log}(text, Syslog::LOG_DEBUG)

# File lib/daemons/syslogio.rb, line 159
def debug(text)
  log(text, Syslog::LOG_DEBUG)
end
emerg(text) click to toggle source

Log at the emergency level

Shorthand for {#log}(text, Syslog::LOG_EMERG)

# File lib/daemons/syslogio.rb, line 203
def emerg(text)
  log(text, Syslog::LOG_EMERG)
end
error(text) click to toggle source

Log at the error level

Shorthand for {#log}(text, Syslog::LOG_ERR)

# File lib/daemons/syslogio.rb, line 188
def error(text)
  log(text, Syslog::LOG_ERR)
end
fatal(text)
Alias for: crit
flush() click to toggle source

Immediately flush any buffered data

# File lib/daemons/syslogio.rb, line 151
def flush
  syswrite(@buffer)
  @buffer = ''
end
info(text) click to toggle source

Log at the info level

Shorthand for {#log}(text, Syslog::LOG_INFO)

# File lib/daemons/syslogio.rb, line 166
def info(text)
  log(text, Syslog::LOG_INFO)
end
isatty() click to toggle source

false

# File lib/daemons/syslogio.rb, line 237
def isatty
  false
end
log(text, level = nil) click to toggle source

Write a complete line at the specified log level

Similar to {#puts} but allows changing the log level for just this one message

# File lib/daemons/syslogio.rb, line 219
def log(text, level = nil)
  if priority.nil? then
    write(text.chomp + "\n")
  else
    priority_bkup = @priority
    #TODO fix this to be less ugly. Temporarily setting an instance variable is evil
    @priority = priority
    write(text.chomp + "\n")
    @priority = priority_bkup
  end
end
noop(*args) click to toggle source

@!visibility private

# File lib/daemons/syslogio.rb, line 232
def noop(*args)
end
Also aliased as: reopen
notice(text) click to toggle source

Log at the notice level

Shorthand for {#log}(text, Syslog::LOG_NOTICE)

# File lib/daemons/syslogio.rb, line 173
def notice(text)
  log(text, Syslog::LOG_NOTICE)
end
Also aliased as: notify
notify(text)
Alias for: notice
puts(*texts) click to toggle source

Log a complete line

Similar to {#write} but appends a newline if not present.

# File lib/daemons/syslogio.rb, line 210
def puts(*texts)
  texts.each do |text|
    write(text.chomp + "\n")
  end
end
reopen(*args)
Alias for: noop
sub_add(regex, replacement) click to toggle source

Add a substitution rule

These substitutions will be applied to each line before it is logged. This can be useful if some other gem is generating log content and you want to change the formatting. @param regex [Regex]

# File lib/daemons/syslogio.rb, line 99
def sub_add(regex, replacement)
  @subs << [regex, replacement]
end
sync=(sync) click to toggle source

Enable or disable synchronous IO (buffering).

When false (default), output will be line buffered. For syslog this is optimal so the log entries are complete lines.

# File lib/daemons/syslogio.rb, line 106
def sync=(sync)
  if sync != true and sync != false then
    raise ArgumentError, "sync must be true or false"
  end
  @sync = sync
  if sync == true then
    flush
  end
end
syswrite(text) click to toggle source

Write to syslog directly, bypassing buffering if enabled.

# File lib/daemons/syslogio.rb, line 132
def syswrite(text)
  begin
    @out.syswrite(text) if @out and !@out.closed?
  rescue SystemCallError => e
  end

  text.split(/\n/).each do |line|
    @subs.each do |sub|
      line.sub!(sub[0], sub[1])
    end
    if line == '' or line.match(/^\s*$/) then
      next
    end
    Syslog.log(@facility | @level, line)
  end
  nil
end
warn(text) click to toggle source

Log at the warning level

Shorthand for {#log}(text, Syslog::LOG_WARNING)

# File lib/daemons/syslogio.rb, line 181
def warn(text)
  log(text, Syslog::LOG_WARNING)
end
write(text) click to toggle source

Write to syslog respecting the behavior of the {#sync} setting.

# File lib/daemons/syslogio.rb, line 117
def write(text)
  if @sync then
    syswrite(text)
  else
    text.split(/(\n)/).each do |line|
      @buffer = @buffer + line.to_s
      if line == "\n" then
        flush
      end
    end
  end
end
Also aliased as: <<