class Apple::System::Logger

Constants

VERSION

The version of this library.

Attributes

facility[R]

A syslogd facility. The system default is 'user'.

level[R]

The logging severity threshold. The default is debug.

logdev[R]

If provided, a file or filehandle that the logger will multicast to. The default is nil.

progname[R]

The program name, or ident, that becomes the key sender. The default is nil.

Public Class Methods

new(**kwargs) { |self| ... } click to toggle source

Create and return an Apple::System::Logger instance. The constructor takes a series of optional arguments:

  • facility

  • level

  • progname

  • logdev

  • stderr

Note that the logdev only seems to work with $stdout or $stderr, if provided. You can also specify ':stderr => true' to automatically multicast to stderr.

For the severity level, the possible values are:

  • ASL_LEVEL_EMERG

  • ASL_LEVEL_ALERT

  • ASL_LEVEL_CRIT

  • ASL_LEVEL_ERR

  • ASL_LEVEL_WARNING

  • ASL_LEVEL_NOTICE

  • ASL_LEVEL_INFO

  • ASL_LEVEL_DEBUG

You may optionally use a block, which will yield the logger instance and automatically close itself.

Example:

# Typical
log = Apple::System::Logger.new(facility: 'com.apple.console', progname: 'my-program')
log.warn("Some warning message")
log.close

# Block form
Apple::System::Logger.new(facility: 'com.apple.console', progname: 'my-program') do |log|
  log.warn("Some warning message")
end
# File lib/apple/system/logger.rb, line 65
def initialize(**kwargs)
  @facility = kwargs[:facility]
  @level    = kwargs[:level] || ASL_LEVEL_DEBUG
  @progname = kwargs[:progname]
  @logdev   = kwargs[:logdev]
  @stderr   = kwargs[:stderr]

  if @logdev || @facility || @progname
    options = ASL_OPT_NO_DELAY | ASL_OPT_NO_REMOTE
    options |= ASL_OPT_STDERR if @stderr

    @aslclient = asl_open(@progname, @facility, options)

    if @logdev
      asl_add_log_file(@aslclient, @logdev.fileno)
    end
  else
    @aslclient = nil
  end

  @aslmsg = asl_new(ASL_TYPE_MSG)
  asl_set(@aslmsg, ASL_KEY_FACILITY, @facility) if @facility
  asl_set_filter(@aslclient, @level)

  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
end

Public Instance Methods

<<(message) click to toggle source

Dump a message with no formatting at the current severity level.

# File lib/apple/system/logger.rb, line 100
def <<(message)
  asl_log(@aslclient, @aslmsg, @level, message)
end
add(level, message) click to toggle source

Log a message at the given level.

# File lib/apple/system/logger.rb, line 106
def add(level, message)
  asl_log(@aslclient, @aslmsg, level, message)
end
close() click to toggle source

Close the logger instance. You should always do this.

# File lib/apple/system/logger.rb, line 273
def close
  asl_free(@aslmsg) if @aslmsg
  asl_close(@aslclient) if @aslclient
  @aslmsg = nil
  @aslclient = nil
end
debug(message) click to toggle source

Log a debug message.

# File lib/apple/system/logger.rb, line 112
def debug(message)
  asl_log(@aslclient, @aslmsg, ASL_LEVEL_DEBUG, message)
end
debug?() click to toggle source

Returns true if the current severity level allows for the printing of debug messages.

# File lib/apple/system/logger.rb, line 118
def debug?
  level >= ASL_LEVEL_DEBUG
end
error() click to toggle source

Log an error message.

# File lib/apple/system/logger.rb, line 148
def error
  asl_log(@aslclient, @aslmsg, ASL_LEVEL_ERR, message)
end
error?() click to toggle source

Returns true if the current severity level allows for the printing of erro messages.

# File lib/apple/system/logger.rb, line 154
def error?
  level >= ASL_LEVEL_ERR
end
fatal() click to toggle source

Log a fatal message. For this library that means an ASL_LEVEL_CRIT message.

# File lib/apple/system/logger.rb, line 160
def fatal
  asl_log(@aslclient, @aslmsg, ASL_LEVEL_CRIT, message)
end
fatal?() click to toggle source

Returns true if the current severity level allows for the printing of fatal messages.

# File lib/apple/system/logger.rb, line 166
def fatal?
  level >= ASL_LEVEL_CRIT
end
info(message) click to toggle source

Log an info message.

# File lib/apple/system/logger.rb, line 124
def info(message)
  asl_log(@aslclient, @aslmsg, ASL_LEVEL_INFO, message)
end
info?() click to toggle source

Returns true if the current severity level allows for the printing of info messages.

# File lib/apple/system/logger.rb, line 130
def info?
  level >= ASL_LEVEL_INFO
end
unknown(message) click to toggle source

Log an unknown message. For this library that means an ASL_LEVEL_EMERG message.

# File lib/apple/system/logger.rb, line 172
def unknown(message)
  asl_log(@aslclient, @aslmsg, ASL_LEVEL_EMERG, message)
end
warn(message) click to toggle source

Log a warning message.

# File lib/apple/system/logger.rb, line 136
def warn(message)
  asl_log(@aslclient, @aslmsg, ASL_LEVEL_WARNING, message)
end
warn?() click to toggle source

Returns true if the current severity level allows for the printing of warning messages.

# File lib/apple/system/logger.rb, line 142
def warn?
  level >= ASL_LEVEL_WARNING
end

Private Instance Methods

map_key_to_asl_key(key) click to toggle source
# File lib/apple/system/logger.rb, line 282
def map_key_to_asl_key(key)
  {
    :time     => ASL_KEY_TIME,
    :host     => ASL_KEY_HOST,
    :sender   => ASL_KEY_SENDER,
    :facility => ASL_KEY_FACILITY,
    :pid      => ASL_KEY_PID,
    :uid      => ASL_KEY_UID,
    :gid      => ASL_KEY_GID,
    :level    => ASL_KEY_LEVEL,
    :message  => ASL_KEY_MSG
  }[key]
end