Class: Copland::LogFactory
- Inherits:
-
Object
- Object
- Copland::LogFactory
- Defined in:
- lib/copland/log-factory.rb
Overview
A factory class that returns Logger instances. The LogFactory is not a singleton for the same reason that Copland::Registry is not a singleton–the allow multiple registries to exist simultaneously. Since each registry has its own logger factory, the logger factory must be separately instantiable.
Constant Summary collapse
- DEFAULT_CONFIG_FILE =
The default name of the logger configuration file. This file will be used to read default properties for initializing logger objects.
"./logger.yml"
- DEFAULT_LOG_FILENAME =
The default name of the log file to write to.
"./copland.log"
- LEVEL_TRANSLATOR =
Translate names of levels to their actual values.
{ "DEBUG" => Logger::DEBUG, "INFO" => Logger::INFO, "WARN" => Logger::WARN, "ERROR" => Logger::ERROR, "FATAL" => Logger::FATAL, "UNKNOWN" => Logger::UNKNOWN }
Instance Attribute Summary collapse
-
#default_format ⇒ Object
readonly
The default (date) format string to use when logging.
-
#default_level ⇒ Object
readonly
The default log level to use for logs that are created.
-
#device ⇒ Object
readonly
The device that logs will write to.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the logging device and makes this factory invalid for future accesses.
-
#closed? ⇒ Boolean
Returns true if the factory has been closed.
-
#get(name) ⇒ Object
Retrieves the logger with the given name.
-
#initialize(opts = {}) ⇒ LogFactory
constructor
Create a new LogFactory using the given initialization parameters.
Constructor Details
#initialize(opts = {}) ⇒ LogFactory
Create a new LogFactory using the given initialization parameters. The valid options are:
-
:config_file
: the configuration file from which to read logger configuration options. -
:device
: the device (pseudo-IO object) to write log messages to. Either this, or:filename
must be specified. -
:filename
: the filename of the log to write to. -
:roll_age
: the number of days before the log should be rolled. -
:roll_frequency
: either ‘daily’, ‘weekly’, or ‘monthly’. -
:roll_size
: the maximum size of a log file. -
:default_format
: the default date format string for the log. -
:default_level
: the default log level for the log. -
:levels
: a hash of patterns that map to a hash of ‘level’ and ‘format’ keys, specifying the log level and date format for any log whose name matches the key.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/copland/log-factory.rb', line 90 def initialize( opts={} ) opts[ :config_file ] ||= DEFAULT_CONFIG_FILE read_config opts if opts[ :device ] @device = opts[ :device ] else filename = opts[ :filename ] || DEFAULT_LOG_FILENAME roll_age = opts[ :roll_age ] || opts[ :roll_frequency ] || 0 roll_size = opts[ :roll_size ] @device = Logger::LogDevice.new( filename, :shift_age => roll_age, :shift_size => roll_size ) end @default_format = opts[ :default_format ] @default_level = opts[ :default_level ] @levels = Hash.new "level" => nil, "format" => nil ( opts[ :levels ] || Hash.new ).each_pair do |key, value| key = Regexp.new( "^" + key.gsub( /\./, "\\." ).gsub( /\*/, ".*" ) ) value = { "level" => value } if value.is_a? String @levels[ key ] = value end @loggers = Hash.new @mutex = Mutex.new end |
Instance Attribute Details
#default_format ⇒ Object (readonly)
The default (date) format string to use when logging.
65 66 67 |
# File 'lib/copland/log-factory.rb', line 65 def default_format @default_format end |
#default_level ⇒ Object (readonly)
The default log level to use for logs that are created.
68 69 70 |
# File 'lib/copland/log-factory.rb', line 68 def default_level @default_level end |
#device ⇒ Object (readonly)
The device that logs will write to.
71 72 73 |
# File 'lib/copland/log-factory.rb', line 71 def device @device end |
Instance Method Details
#close ⇒ Object
Closes the logging device and makes this factory invalid for future accesses.
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/copland/log-factory.rb', line 188 def close @mutex.synchronize do return if @closed @device.close unless [ $stdout, $stderr ].include?( @device ) @loggers = nil @closed = true end end |
#closed? ⇒ Boolean
Returns true if the factory has been closed.
200 201 202 |
# File 'lib/copland/log-factory.rb', line 200 def closed? @closed end |
#get(name) ⇒ Object
Retrieves the logger with the given name. If no such log has been created, the log will be created and initialized. Otherwise, the log with the given name is returned.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/copland/log-factory.rb', line 161 def get( name ) # the common case first, outside the synchronize, for speed return @loggers[ name ] if @loggers[ name ] @mutex.synchronize do # check again, inside the synchronize, to avoid race conditions return @loggers[ name ] if @loggers[ name ] definition = find_definition( name ) level = definition[ "level" ] || @default_level format = definition[ "format" ] || @default_format level = LEVEL_TRANSLATOR[ level ] || level logger = Logger.new( @device ) logger.level = level if level logger.datetime_format = format if format logger.progname = name @loggers[ name ] = logger return logger end end |