Module: Syslog

Includes:
Constants
Defined in:
lib/syslog/jruby.rb,
ext/syslog/syslog.c

Defined Under Namespace

Modules: Constants, Facility, Foreign, Level, Macros, Option Classes: Logger

Constant Summary collapse

FORMAT_STRING =
'%s'
VERSION =

Syslog macros

rb_str_new_cstr(SYSLOG_VERSION)

Constants included from Facility

Facility::LOG_AUTH, Facility::LOG_AUTHPRIV, Facility::LOG_CONSOLE, Facility::LOG_CRON, Facility::LOG_DAEMON, Facility::LOG_FTP, Facility::LOG_KERN, Facility::LOG_LOCAL0, Facility::LOG_LOCAL1, Facility::LOG_LOCAL2, Facility::LOG_LOCAL3, Facility::LOG_LOCAL4, Facility::LOG_LOCAL5, Facility::LOG_LOCAL6, Facility::LOG_LOCAL7, Facility::LOG_LPR, Facility::LOG_MAIL, Facility::LOG_NEWS, Facility::LOG_NTP, Facility::LOG_SECURITY, Facility::LOG_SYSLOG, Facility::LOG_USER, Facility::LOG_UUCP

Constants included from Level

Level::LOG_ALERT, Level::LOG_CRIT, Level::LOG_DEBUG, Level::LOG_EMERG, Level::LOG_ERR, Level::LOG_INFO, Level::LOG_NOTICE, Level::LOG_WARNING

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Constants

included

Methods included from Level

#alert, #crit, #debug, #emerg, #err, #info, #notice, #warning

Class Attribute Details

.maskObject

Returns the log priority mask in effect. The mask is not reset by opening or closing syslog.



202
203
204
205
# File 'lib/syslog/jruby.rb', line 202

def mask
  @mask ||= -1
  @opened ? @mask : nil
end

Class Method Details

.alert(*args) ⇒ Object

handy little shortcut for LOG_ALERT as the priority



296
# File 'lib/syslog/jruby.rb', line 296

def alert(*args);  write(Syslog::LOG_ALERT,   *args); end

.closeObject

Closes the syslog facility. Raises a runtime exception if it is not open.



271
272
273
274
275
276
277
278
# File 'lib/syslog/jruby.rb', line 271

def close
  raise "Syslog not opened" unless @opened

  Foreign.close
  @ident = nil
  @options = @facility = @mask = -1;
  @opened = false
end

.crit(*args) ⇒ Object

handy little shortcut for LOG_CRIT as the priority



304
# File 'lib/syslog/jruby.rb', line 304

def crit(*args);   write(Syslog::LOG_CRIT,    *args); end

.debug(*args) ⇒ Object

handy little shortcut for LOG_DEBUG as the priority



320
# File 'lib/syslog/jruby.rb', line 320

def debug(*args);  write(Syslog::LOG_DEBUG,   *args); end

.emerg(*args) ⇒ Object

handy little shortcut for LOG_EMERG as the priority



292
# File 'lib/syslog/jruby.rb', line 292

def emerg(*args);  write(Syslog::LOG_EMERG,   *args); end

.err(*args) ⇒ Object

handy little shortcut for LOG_ERR as the priority



300
# File 'lib/syslog/jruby.rb', line 300

def err(*args);    write(Syslog::LOG_ERR,     *args); end

.facilityObject

Returns the facility number used in the last call to open()



188
189
190
# File 'lib/syslog/jruby.rb', line 188

def facility
  @opened ? @facility : nil
end

.identObject

Returns the identity string used in the last call to open()



176
177
178
# File 'lib/syslog/jruby.rb', line 176

def ident
  @opened ? @ident : nil
end

.info(*args) ⇒ Object

handy little shortcut for LOG_INFO as the priority



316
# File 'lib/syslog/jruby.rb', line 316

def info(*args);   write(Syslog::LOG_INFO,    *args); end

.inspectObject

Returns an inspect() string summarizing the object state.



322
323
324
325
326
327
328
329
# File 'ext/syslog/syslog.c', line 322

def inspect
  if @opened
    "<#%s: opened=true, ident=\"%s\", options=%d, facility=%d, mask=%d>" %
      [self.name, @ident, @options, @facility, @mask]
  else
    "<##{self.name}: opened=false>"
  end
end

.instanceObject

Returns self, for backward compatibility.



351
352
353
# File 'lib/syslog/jruby.rb', line 351

def instance
  self
end

.log(priority, format_string, *format_args) ⇒ Object

Log a message with the specified priority. Example:

Syslog.log(Syslog::LOG_CRIT, "Out of disk space")
Syslog.log(Syslog::LOG_CRIT, "User %s logged in", ENV['USER'])

The priority levels, in descending order, are:

LOG_EMERG

System is unusable

LOG_ALERT

Action needs to be taken immediately

LOG_CRIT

A critical condition has occurred

LOG_ERR

An error occurred

LOG_WARNING

Warning of a possible problem

LOG_NOTICE

A normal but significant condition occurred

LOG_INFO

Informational message

LOG_DEBUG

Debugging information

Each priority level also has a shortcut method that logs with it’s named priority. As an example, the two following statements would produce the same result:

Syslog.log(Syslog::LOG_ALERT, "Out of memory")
Syslog.alert("Out of memory")


286
287
288
# File 'lib/syslog/jruby.rb', line 286

def log(pri, *args)
  write(pri, *args)
end

.LOG_MASK(pri) ⇒ Object

LOG_MASK(pri)

HACK copied from macro Creates a mask for one priority.



327
328
329
# File 'lib/syslog/jruby.rb', line 327

def LOG_MASK(pri)
  1 << pri
end

.LOG_UPTO(pri) ⇒ Object

LOG_UPTO(pri) HACK copied from macro Creates a mask for all priorities up to pri.



335
336
337
# File 'lib/syslog/jruby.rb', line 335

def LOG_UPTO(pri)
  (1 << ((pri)+1)) - 1
end

.notice(*args) ⇒ Object

handy little shortcut for LOG_NOTICE as the priority



312
# File 'lib/syslog/jruby.rb', line 312

def notice(*args); write(Syslog::LOG_NOTICE,  *args); end

.open(ident, options, facility) ⇒ Object

:yields: syslog

Open the syslog facility. Raises a runtime exception if it is already open.

Can be called with or without a code block. If called with a block, the Syslog object created is passed to the block.

If the syslog is already open, raises a RuntimeError.

ident is a String which identifies the calling program.

options is the logical OR of any of the following:

LOG_CONS

If there is an error while sending to the system logger, write directly to the console instead.

LOG_NDELAY

Open the connection now, rather than waiting for the first message to be written.

LOG_NOWAIT

Don’t wait for any child processes created while logging messages. (Has no effect on Linux.)

LOG_ODELAY

Opposite of LOG_NDELAY; wait until a message is sent before opening the connection. (This is the default.)

LOG_PERROR

Print the message to stderr as well as sending it to syslog. (Not in POSIX.1-2001.)

LOG_PID

Include the current process ID with each message.

facility describes the type of program opening the syslog, and is the logical OR of any of the following which are defined for the host OS:

LOG_AUTH

Security or authorization. Deprecated, use LOG_AUTHPRIV instead.

LOG_AUTHPRIV

Security or authorization messages which should be kept private.

LOG_CONSOLE

System console message.

LOG_CRON

System task scheduler (cron or at).

LOG_DAEMON

A system daemon which has no facility value of its own.

LOG_FTP

An FTP server.

LOG_KERN

A kernel message (not sendable by user processes, so not of much use to Ruby, but listed here for completeness).

LOG_LPR

Line printer subsystem.

LOG_MAIL

Mail delivery or transport subsystem.

LOG_NEWS

Usenet news system.

LOG_NTP

Network Time Protocol server.

LOG_SECURITY

General security message.

LOG_SYSLOG

Messages generated internally by syslog.

LOG_USER

Generic user-level message.

LOG_UUCP

UUCP subsystem.

LOG_LOCAL0 to LOG_LOCAL7

Locally-defined facilities.

Example:

Syslog.open("webrick", Syslog::LOG_PID,
            Syslog::LOG_DAEMON | Syslog::LOG_LOCAL3)


219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/syslog/jruby.rb', line 219

def open(ident=nil, opt=nil, fac=nil)
  raise "Syslog already open" unless not @opened

  ident ||= $0
  opt ||= Constants::LOG_PID | Constants::LOG_CONS
  fac ||= Constants::LOG_USER

  @ident = ident
  @options = opt
  @facility = fac
  @ident_memory = if ident
                    FFI::MemoryPointer.from_string(ident)
                  else
                    nil
                  end
  Foreign.open(@ident_memory, opt, fac)

  @opened = true

  # Calling set_mask twice is the standard way to set the 'default' mask
  @mask = Foreign.set_mask(0)
  Foreign.set_mask(@mask)

  if block_given?
    begin
      yield self
    ensure
      close
    end
  end

  self
end

.opened?Boolean

Returns true if the syslog is open.

Returns:

  • (Boolean)


264
265
266
# File 'lib/syslog/jruby.rb', line 264

def opened?
  @opened || false
end

.optionsObject

Returns the options bitmask used in the last call to open()



182
183
184
# File 'lib/syslog/jruby.rb', line 182

def options
  @opened ? @options : nil
end

.reopen(ident, options, facility) ⇒ Object Also known as: open!

:yields: syslog

Closes and then reopens the syslog.

Arguments are the same as for open().



255
256
257
258
# File 'lib/syslog/jruby.rb', line 255

def reopen(*args, &block)
  close
  open(*args, &block)
end

.warning(*args) ⇒ Object

handy little shortcut for LOG_WARNING as the priority



308
# File 'lib/syslog/jruby.rb', line 308

def warning(*args);write(Syslog::LOG_WARNING, *args); end

.write(pri, format, *args) ⇒ Object



356
357
358
359
360
361
# File 'lib/syslog/jruby.rb', line 356

def write(pri, format, *args)
  raise "Syslog must be opened before write" unless @opened

  message = format % args
  Foreign.write(pri, FORMAT_STRING, :string, message, :pointer, nil)
end