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"
- DEFAULT_MESSAGE_FORMAT =
The default format of the log messages (see Logger for more info)
"[%-5p] %d -- %C: %m"
- 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_date_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.
-
#default_message_format ⇒ Object
readonly
The default message format string to use when logging.
-
#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_date_format
: the default date format string for the log. -
:default_message_format
: the default message 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’ ‘date_format’, and ‘message_format’ keys, specifying the log level, date format, and message format for any log whose name matches the key.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/copland/log-factory.rb', line 99 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_date_format = opts[ :default_date_format ] @default_message_format = opts[ :default_message_format ] || DEFAULT_MESSAGE_FORMAT @default_level = opts[ :default_level ] @levels = Hash.new "level" => nil, "date-format" => nil, "message-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_date_format ⇒ Object (readonly)
The default date format string to use when logging.
68 69 70 |
# File 'lib/copland/log-factory.rb', line 68 def default_date_format @default_date_format end |
#default_level ⇒ Object (readonly)
The default log level to use for logs that are created.
74 75 76 |
# File 'lib/copland/log-factory.rb', line 74 def default_level @default_level end |
#default_message_format ⇒ Object (readonly)
The default message format string to use when logging.
71 72 73 |
# File 'lib/copland/log-factory.rb', line 71 def @default_message_format end |
#device ⇒ Object (readonly)
The device that logs will write to.
77 78 79 |
# File 'lib/copland/log-factory.rb', line 77 def device @device end |
Instance Method Details
#close ⇒ Object
Closes the logging device and makes this factory invalid for future accesses.
204 205 206 207 208 209 210 211 212 213 |
# File 'lib/copland/log-factory.rb', line 204 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.
216 217 218 |
# File 'lib/copland/log-factory.rb', line 216 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.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/copland/log-factory.rb', line 174 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 date_format = definition[ "date-format" ] || @default_date_format = definition[ "message-format" ] || @default_message_format level = LEVEL_TRANSLATOR[ level ] || level logger = Copland::Logger.new( @device ) logger.level = level if level logger.progname = name logger.datetime_format = date_format if date_format logger. = if @loggers[ name ] = logger return logger end end |