Class: Padrino::Logger
- Inherits:
-
Object
- Object
- Padrino::Logger
- Includes:
- Colorize, Extensions
- Defined in:
- padrino-core/lib/padrino-core/logger.rb
Overview
Padrinos internal logger, using all of Padrino log extensions.
Defined Under Namespace
Modules: Colorize, Extensions Classes: Rack
Constant Summary
- Levels =
Ruby (standard) logger levels:
- :fatal
-
An unhandleable error that results in a program crash
- :error
-
A handleable error condition
- :warn
-
A warning
- :info
-
generic (useful) information about system operation
- :debug
-
low-level information for developers
- :devel
-
Development-related information that is unnecessary in debug mode
{ :fatal => 7, :error => 6, :warn => 4, :info => 3, :debug => 0, :devel => -1, } unless const_defined?(:Levels)
- Config =
Configuration for a given environment, possible options are:
- :log_level
-
Once of [:fatal, :error, :warn, :info, :debug]
- :stream
-
Once of [:to_file, :null, :stdout, :stderr] our your custom stream
- :log_level
-
The log level from, e.g. :fatal or :info. Defaults to :warn in the production environment and :debug otherwise.
- :auto_flush
-
Whether the log should automatically flush after new messages are added. Defaults to true.
- :format_datetime
-
Format of datetime. Defaults to: “%d/%b/%Y %H:%M:%S”
- :format_message
-
Format of message. Defaults to: “”%s - - [%s] "%s"“”
- :log_static
-
Whether or not to show log messages for static files. Defaults to: false
Defaults are:
:production => { :log_level => :warn, :stream => :to_file } :development => { :log_level => :debug, :stream => :stdout } :test => { :log_level => :fatal, :stream => :null }
In some cases, configuring the loggers before loading the framework is necessary. You can do so by setting PADRINO_LOGGER:
PADRINO_LOGGER = { :staging => { :log_level => :debug, :stream => :to_file }}
{ :production => { :log_level => :warn, :stream => :to_file }, :development => { :log_level => :debug, :stream => :stdout, :format_datetime => ' ' }, :test => { :log_level => :debug, :stream => :null } }
- @@mutex =
{}
Constants included from Colorize
Instance Attribute Summary (collapse)
-
- (Object) auto_flush
Returns the value of attribute auto_flush.
-
- (Object) buffer
readonly
Returns the value of attribute buffer.
-
- (Object) init_args
readonly
Returns the value of attribute init_args.
-
- (Object) level
Returns the value of attribute level.
-
- (Object) log
readonly
Returns the value of attribute log.
-
- (Object) log_static
Returns the value of attribute log_static.
Class Method Summary (collapse)
-
+ (Padrino::Logger) setup!
Setup a new logger.
Instance Method Summary (collapse)
-
- (Object) <<(message = nil)
(also: #write)
Directly append message to the log.
-
- (NilClass) close
Close and remove the current log object.
-
- (Object) flush
Flush the entire buffer to the log object.
- - (Object) format(message, level)
-
- (Logger) initialize(options = {})
constructor
To initialize the logger you create a new object, proxies to set_log.
Methods included from Colorize
Methods included from Extensions
#bench, #colorize, #colorize!, #push, #stylized_level
Constructor Details
- (Logger) initialize(options = {})
To initialize the logger you create a new object, proxies to set_log.
314 315 316 317 318 319 320 321 322 323 324 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 314 def initialize(={}) @buffer = [] @auto_flush = .has_key?(:auto_flush) ? [:auto_flush] : true @level = [:log_level] ? Padrino::Logger::Levels[[:log_level]] : Padrino::Logger::Levels[:debug] @log = [:stream] || $stdout @log.sync = true @mutex = @@mutex[@log] ||= Mutex.new @format_datetime = [:format_datetime] || "%d/%b/%Y %H:%M:%S" = [:format_message] || "%s -%s%s" @log_static = .has_key?(:log_static) ? [:log_static] : false end |
Instance Attribute Details
- (Object) auto_flush
Returns the value of attribute auto_flush
214 215 216 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 214 def auto_flush @auto_flush end |
- (Object) buffer (readonly)
Returns the value of attribute buffer
215 216 217 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 215 def buffer @buffer end |
- (Object) init_args (readonly)
Returns the value of attribute init_args
217 218 219 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 217 def init_args @init_args end |
- (Object) level
Returns the value of attribute level
213 214 215 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 213 def level @level end |
- (Object) log (readonly)
Returns the value of attribute log
216 217 218 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 216 def log @log end |
- (Object) log_static
Returns the value of attribute log_static
218 219 220 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 218 def log_static @log_static end |
Class Method Details
+ (Padrino::Logger) setup!
Setup a new logger
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 268 def self.setup! config_level = (PADRINO_LOG_LEVEL || Padrino.env || :test).to_sym # need this for PADRINO_LOG_LEVEL config = Config[config_level] unless config warn("No logging configuration for :#{config_level} found, falling back to :production") config = Config[:production] end stream = case config[:stream] when :to_file FileUtils.mkdir_p(Padrino.root('log')) unless File.exists?(Padrino.root('log')) File.new(Padrino.root('log', "#{Padrino.env}.log"), 'a+') when :null then StringIO.new when :stdout then $stdout when :stderr then $stderr else config[:stream] # return itself, probabilly is a custom stream. end Thread.current[:padrino_logger] = Padrino::Logger.new(config.merge(:stream => stream)) end |
Instance Method Details
- (Object) <<(message = nil) Also known as: write
Directly append message to the log.
361 362 363 364 365 366 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 361 def <<( = nil) << "\n" unless [-1] == ?\n @buffer << flush if @auto_flush end |
- (NilClass) close
Close and remove the current log object.
341 342 343 344 345 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 341 def close flush @log.close if @log.respond_to?(:close) && !@log.tty? @log = nil end |
- (Object) flush
Flush the entire buffer to the log object.
329 330 331 332 333 334 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 329 def flush return unless @buffer.size > 0 @mutex.synchronize do @log.write(@buffer.slice!(0..-1).join('')) end end |
- (Object) format(message, level)
369 370 371 |
# File 'padrino-core/lib/padrino-core/logger.rb', line 369 def format(, level) % [stylized_level(level), colorize(Time.now.strftime(@format_datetime), :yellow), .to_s.strip] end |