Class: RemoteLogger::Logger
- Inherits:
-
Log4r::Logger
- Object
- Log4r::Logger
- RemoteLogger::Logger
- Includes:
- Log4r
- Defined in:
- lib/remote_logger/logger.rb
Overview
A subclass of Log4r::Logger pre-configured with specific logging settings
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Logger
constructor
Creates new remote logger.
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 = {} # define outputters: http://log4r.sourceforge.net/rdoc/files/log4r/outputter/outputter_rb.html if [:outputters] outputters = [:outputters] else # specify log message format: http://log4r.sourceforge.net/rdoc/files/log4r/formatter/patternformatter_rb.html format = PatternFormatter.new :pattern => [:pattern]||PATTERN, :date_pattern => [:date_pattern]||DATE_PATTERN # Set up IO streams with correct transcoding and conversion options: log file and (Windows) console conversion = {:undef=>:replace, :replace=>[:replace]||'?'} file = File.new( [:file_name]||FILE_NAME, ([:trunc] ? 'w:' : 'a:') + ([:file_encoding]||FILE_ENCODING), conversion) $stdout.set_encoding([: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([:name]||LOGGER_NAME).tap do |logger| logger.outputters = outputters logger.info "#{name}: Logger created" if [:verbose] end end |