Class: Needle::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/needle/logger.rb

Overview

A specialization of the standard Logger class that comes with Ruby. This provides the additional functionality of a fully-customizable message format, whereas the original only provided a customizable date format.

Constant Summary collapse

SPECIFIER_OPTIONS =

The map of specifier options supported by this class.

{
  "c" => { :type => "s", :value => "@name" },
  "C" => { :type => "s", :value => "self.progname" },
  "d" => { :type => "s", :value => "opts[:timestamp]" },
  "F" => { :type => "s", :value => "opts[:caller_file]" },
  "l" => { :type => "s", :value => "opts[:caller_info]" },
  "L" => { :type => "d", :value => "opts[:caller_line]" },
  "m" => { :type => "s", :value => "opts[:msg]" },
  "M" => { :type => "s", :value => "opts[:caller_method]" },
  "n" => { :type => "s", :value => "$/" },
  "p" => { :type => "s", :value => "opts[:severity]" },
  "t" => { :type => "d", :value => "Thread.current.__id__" },
  "%" => { :type => "s", :value => "'%'" },
  "P" => { :type => "s", :value => "opts[:progname]" },
  "$" => { :type => "d", :value => "$$" }
}
SPECIFIER_PATTERN =

The regular expression for matching specifier patterns in the format strings.

/(.*?)%(-?\d*(?:\.\d+)?)?([cCdFlLmMnpt%$P])/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#message_formatObject

The format string for the message (nil if the default should be used)


32
33
34
# File 'lib/needle/logger.rb', line 32

def message_format
  @message_format
end

#nameObject (readonly)

The brief name of this logger (derived from #progname).


29
30
31
# File 'lib/needle/logger.rb', line 29

def name
  @name
end

Instance Method Details

#progname=(progname) ⇒ Object

Extracts the unqualified name from the progname, after setting the progname.


58
59
60
61
# File 'lib/needle/logger.rb', line 58

def progname=( progname )
  super
  @name = progname.split( /\./ ).last
end

#write_to(device, shift_age = 0, shift_size = 1048576) ⇒ Object

Changes the device that the given logger writes to, to be the given device. Does so in a thread-safe manner.


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/needle/logger.rb', line 65

def write_to( device, shift_age = 0, shift_size = 1048576 )
  saved_critical = Thread.critical
  Thread.critical = true

  if device
    if device.respond_to?( :write ) && device.respond_to?( :close )
      @logdev = device
    else
      @logdev = Logger::LogDevice.new( device,
        :shift_age => shift_age, 
        :shift_size => shift_size )
    end
  end

  device
ensure
  Thread.critical = saved_critical
end