Module: Syslog

Includes:
Constants
Defined in:
lib/syslog.rb

Overview

A Simple wrapper for the UNIX syslog system calls that might be handy if you’re writing a server in Ruby. For the details of the syslog(8) architecture and constants, see the syslog(3) manual page of your platform.

Defined Under Namespace

Modules: Constants, Foreign Classes: Logger

Constant Summary collapse

FFI =
Rubinius::FFI

Constants included from Constants

Constants::LOG_ALERT, Constants::LOG_AUTH, Constants::LOG_AUTHPRIV, Constants::LOG_CONS, Constants::LOG_CONSOLE, Constants::LOG_CRIT, Constants::LOG_CRON, Constants::LOG_DAEMON, Constants::LOG_DEBUG, Constants::LOG_EMERG, Constants::LOG_ERR, Constants::LOG_FTP, Constants::LOG_INFO, Constants::LOG_KERN, Constants::LOG_LOCAL0, Constants::LOG_LOCAL1, Constants::LOG_LOCAL2, Constants::LOG_LOCAL3, Constants::LOG_LOCAL4, Constants::LOG_LOCAL5, Constants::LOG_LOCAL6, Constants::LOG_LOCAL7, Constants::LOG_LPR, Constants::LOG_MAIL, Constants::LOG_NDELAY, Constants::LOG_NEWS, Constants::LOG_NOTICE, Constants::LOG_NOWAIT, Constants::LOG_NTP, Constants::LOG_ODELAY, Constants::LOG_PERROR, Constants::LOG_PID, Constants::LOG_SECURITY, Constants::LOG_SYSLOG, Constants::LOG_USER, Constants::LOG_UUCP, Constants::LOG_WARNING

Class Method Summary collapse

Class Method Details

.alert(*args) ⇒ Object

handy little shortcut for LOG_ALERT as the priority



235
236
237
# File 'lib/syslog.rb', line 235

def self.alert(*args)
  log(LOG_ALERT, *args)
end

.closeObject

Close the log close will raise an error if it is already closed



203
204
205
206
207
208
209
210
# File 'lib/syslog.rb', line 203

def self.close
  raise "Syslog not opened" unless @open

  Foreign.close
  @ident = nil
  @options = @facility = @mask = nil;
  @open = false
end

.crit(*args) ⇒ Object

handy little shortcut for LOG_CRIT as the priority



247
248
249
# File 'lib/syslog.rb', line 247

def self.crit(*args)
  log(LOG_CRIT, *args)
end

.debug(*args) ⇒ Object

handy little shortcut for LOG_DEBUG as the priority



271
272
273
# File 'lib/syslog.rb', line 271

def self.debug(*args)
  log(LOG_DEBUG, *args)
end

.emerg(*args) ⇒ Object

handy little shortcut for LOG_EMERG as the priority



229
230
231
# File 'lib/syslog.rb', line 229

def self.emerg(*args);
  log(LOG_EMERG, *args)
end

.err(*args) ⇒ Object

handy little shortcut for LOG_ERR as the priority



241
242
243
# File 'lib/syslog.rb', line 241

def self.err(*args)
  log(LOG_ERR, *args)
end

.facilityObject

returns the facility of the last open call



109
110
111
# File 'lib/syslog.rb', line 109

def self.facility
  @open ? @facility : nil
end

.identObject

returns the ident of the last open call



97
98
99
# File 'lib/syslog.rb', line 97

def self.ident
  @open ? @ident : nil
end

.info(*args) ⇒ Object

handy little shortcut for LOG_INFO as the priority



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

def self.info(*args)
  log(LOG_INFO, *args)
end

.inspectObject



287
288
289
290
291
292
293
294
# File 'lib/syslog.rb', line 287

def self.inspect
  if @open
    "#<%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

Syslog.instance # => Syslog Returns the Syslog module



299
300
301
# File 'lib/syslog.rb', line 299

def self.instance
  self
end

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

log(Syslog::LOG_CRIT, “The %s is falling!”, “sky”)

Doesn’t take any platform specific printf statements

logs things to $stderr
log(Syslog::LOG_CRIT, "Welcome, %s, to my %s!", "leethaxxor", "lavratory")


218
219
220
221
222
223
224
225
# File 'lib/syslog.rb', line 218

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

  pri = Rubinius::Type.coerce_to(pri, Fixnum, :to_i)

  message = StringValue(format) % args
  Foreign.write(pri, "%s", message)
end

.LOG_MASK(pri) ⇒ Object



275
276
277
# File 'lib/syslog.rb', line 275

def self.LOG_MASK(pri)
  Constants.LOG_MASK(pri)
end

.LOG_UPTO(pri) ⇒ Object

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



283
284
285
# File 'lib/syslog.rb', line 283

def self.LOG_UPTO(pri)
  Constants.LOG_UPTO(pri)
end

.maskObject



135
136
137
# File 'lib/syslog.rb', line 135

def self.mask
  @open ? @mask : nil
end

.mask=(mask) ⇒ Object

mask

mask=(mask)

Returns or sets the log priority mask. The value of the mask is persistent and will not be reset by Syslog::open or Syslog::close.

Example:

Syslog.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR)


123
124
125
126
127
128
129
130
131
132
133
# File 'lib/syslog.rb', line 123

def self.mask=(mask)
  unless @open
    raise RuntimeError, "must open syslog before setting log mask"
  end

  @mask_before_reopen = nil

  @mask = Rubinius::Type.coerce_to mask, Fixnum, :to_int

  Foreign.set_mask(@mask)
end

.notice(*args) ⇒ Object

handy little shortcut for LOG_NOTICE as the priority



259
260
261
# File 'lib/syslog.rb', line 259

def self.notice(*args)
  log(LOG_NOTICE, *args)
end

.open(ident = nil, opt = nil, fac = nil) ⇒ Object

open(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, facility = Syslog::LOG_USER) [{ |syslog| … }]

Opens syslog with the given options and returns the module itself. If a block is given, calls it with an argument of itself. If syslog is already opened, raises RuntimeError.

Examples:

Syslog.open('ftpd', Syslog::LOG_PID | Syslog::LOG_NDELAY, Syslog::LOG_FTP)
open!(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, facility = Syslog::LOG_USER)
reopen(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, facility = Syslog::LOG_USER)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/syslog.rb', line 150

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

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

  @ident = ident
  @options = opt
  @facility = fac

  # syslog rereads the string everytime syslog() is called, so we have to use
  # an FFI pointer to keep the memory the string is in alive
  @ident_pointer = FFI::MemoryPointer.new(@ident.size + 1)
  @ident_pointer.write_string(@ident)

  Foreign.open(@ident_pointer, opt, fac)

  @open = true

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

  if block_given?
    begin
      yield self
    ensure
      close
    end
  end

  self
end

.opened?Boolean

Is it open?

Returns:

  • (Boolean)


196
197
198
# File 'lib/syslog.rb', line 196

def self.opened?
  @open
end

.optionsObject

returns the options of the last open call



103
104
105
# File 'lib/syslog.rb', line 103

def self.options
  @open ? @options : nil
end

.reopen(*args, &block) ⇒ Object Also known as: open!



184
185
186
187
188
# File 'lib/syslog.rb', line 184

def self.reopen(*args, &block)
  @mask_before_reopen = mask
  close
  open(*args, &block)
end

.warning(*args) ⇒ Object

handy little shortcut for LOG_WARNING as the priority



253
254
255
# File 'lib/syslog.rb', line 253

def self.warning(*args)
  log(LOG_WARNING, *args)
end