Class: RemoteLogger::Logger

Inherits:
Log4r::Logger
  • Object
show all
Includes:
Log4r
Defined in:
lib/remote_logger/logger.rb

Overview

A subclass of Log4r::Logger pre-configured with specific logging settings

Direct Known Subclasses

DrbLogger, RingyLogger

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Logger

Creates new remote logger. The following options are available:

:name

Service/logger name - default ‘RemoteLogger’

:outputters

Replace outputters (should be Log4r::Outputter subclasses) - default [log file, stdout]

:file_name

Log file name - default ‘remote.log’

:file_encoding

Log file encoding - default FILE_ENCODING (Windows Cyrillic)

:stdout_encoding

Stdout encoding - default STDOUT_ENCODING (DOS/IBM Cyrillic)

:replace

Replacement for undefined conversion chars - default ‘?’

:pattern

Log message pattern - default PATTERN

:date_pattern

Timestamp pattern - default DATE_PATTERN

:trunc

Truncate (rewrite) log file upon creation - default false (append to file)

:verbose

Log all internal messages of RemoteLogger - default false (do not log logger-specific messages)



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/remote_logger/logger.rb', line 35

def initialize options = {}
  # define outputters: http://log4r.sourceforge.net/rdoc/files/log4r/outputter/outputter_rb.html
  if options[:outputters]
    outputters = options[:outputters]
  else
    # specify log message format: http://log4r.sourceforge.net/rdoc/files/log4r/formatter/patternformatter_rb.html
    format = PatternFormatter.new :pattern => options[:pattern]||PATTERN,
                                  :date_pattern => options[:date_pattern]||DATE_PATTERN

    # Set up IO streams with correct transcoding and conversion options: log file and (Windows) console
    conversion = {:undef=>:replace, :replace=>options[:replace]||'?'}
    file = File.new(                                  options[:file_name]||FILE_NAME, (options[:trunc] ? 'w:' : 'a:') +
            (options[:file_encoding]||FILE_ENCODING), conversion)
    $stdout.set_encoding(options[:stdout_encoding]||STDOUT_ENCODING, conversion)

    outputters = [StdoutOutputter.new('console', :formatter => format),
                  IOOutputter.new('file', file, :formatter => format)]
#   file_o = FileOutputter.new 'file', :filename => 'remote.log', :trunc => false, :formatter => format # wrong encoding
#   err_o = StderrOutputter.new 'error', :formatter => format # just in case
  end

  # create new logger named LOG_NAME
  super(options[:name]||LOGGER_NAME).tap do |logger|
    logger.outputters = outputters
    logger.info "#{name}: Logger created" if options[:verbose]
  end
end