Class: Padrino::Logger
- Inherits:
-
Object
- Object
- Padrino::Logger
- Includes:
- Extensions
- Defined in:
- 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 collapse
- Levels =
Ruby (standard) logger levels:
- :fatal
-
An not handleable 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 => 4, :error => 3, :warn => 2, :info => 1, :debug => 0, :devel => -1, }
- 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_path
-
Defines log file path or directory if :stream is :to_file
If it's a file, its location is created by mkdir_p. If it's a directory, it must exist. In this case log name is '<env>.log'
- :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
- :colorize_logging
-
Whether or not to colorize log messages. Defaults to: true
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 =
Mutex.new
Constants included from Extensions
Extensions::SOURCE_LOCATION_REGEXP
Instance Attribute Summary collapse
-
#auto_flush ⇒ Object
Returns the value of attribute auto_flush.
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#colorize_logging ⇒ Object
readonly
Returns the value of attribute colorize_logging.
-
#init_args ⇒ Object
readonly
Returns the value of attribute init_args.
-
#level ⇒ Object
Returns the value of attribute level.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#log_static ⇒ Object
Returns the value of attribute log_static.
Class Method Summary collapse
- .logger ⇒ Object
- .logger=(logger) ⇒ Object
-
.setup! ⇒ Padrino::Logger
Setup a new logger.
Instance Method Summary collapse
-
#<<(message = nil) ⇒ Object
(also: #write)
Directly append message to the log.
-
#add(level, message = nil) ⇒ Object
Adds a message to the log - for compatibility with other loggers.
-
#close ⇒ NilClass
Close and remove the current log object.
-
#flush ⇒ Object
Flush the entire buffer to the log object.
- #format(message, level) ⇒ Object
-
#initialize(options = {}) ⇒ Logger
constructor
To initialize the logger you create a new object, proxies to set_log.
- #source_location? ⇒ Boolean
Methods included from Extensions
#bench, #colorize, #colorize!, #enable_source_location?, #exception, #name, #push, #resolve_source_location, #stylized_level
Constructor Details
#initialize(options = {}) ⇒ Logger
To initialize the logger you create a new object, proxies to set_log.
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/padrino-core/logger.rb', line 408 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 @format_datetime = [:format_datetime] || "%d/%b/%Y %H:%M:%S" @format_message = [:format_message] || "%s - %s %s" @log_static = .has_key?(:log_static) ? [:log_static] : false @colorize_logging = .has_key?(:colorize_logging) ? [:colorize_logging] : true @source_location = [:source_location] @sanitize_encoding = [:sanitize_encoding] || false @sanitize_encoding = Encoding.default_external if @sanitize_encoding == true colorize! if @colorize_logging end |
Instance Attribute Details
#auto_flush ⇒ Object
Returns the value of attribute auto_flush.
268 269 270 |
# File 'lib/padrino-core/logger.rb', line 268 def auto_flush @auto_flush end |
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
269 270 271 |
# File 'lib/padrino-core/logger.rb', line 269 def buffer @buffer end |
#colorize_logging ⇒ Object (readonly)
Returns the value of attribute colorize_logging.
269 270 271 |
# File 'lib/padrino-core/logger.rb', line 269 def colorize_logging @colorize_logging end |
#init_args ⇒ Object (readonly)
Returns the value of attribute init_args.
269 270 271 |
# File 'lib/padrino-core/logger.rb', line 269 def init_args @init_args end |
#level ⇒ Object
Returns the value of attribute level.
268 269 270 |
# File 'lib/padrino-core/logger.rb', line 268 def level @level end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
269 270 271 |
# File 'lib/padrino-core/logger.rb', line 269 def log @log end |
#log_static ⇒ Object
Returns the value of attribute log_static.
268 269 270 |
# File 'lib/padrino-core/logger.rb', line 268 def log_static @log_static end |
Class Method Details
.logger ⇒ Object
320 321 322 |
# File 'lib/padrino-core/logger.rb', line 320 def self.logger (@_logger ||= nil) || setup! end |
.logger=(logger) ⇒ Object
324 325 326 327 328 329 330 331 332 333 |
# File 'lib/padrino-core/logger.rb', line 324 def self.logger=(logger) unless logger.class.ancestors.include?(Padrino::Logger::Extensions) warn <<-EOT WARNING! `Padrino.logger = new_logger` no longer extends it with #colorize! and other features. To do it with a custom logger you have to manually `new_logger.extend(Padrino::Logger::Extensions)` before passing to `Padrino.logger = new_logger`. EOT end @_logger = logger end |
.setup! ⇒ Padrino::Logger
Setup a new logger.
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/padrino-core/logger.rb', line 341 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 if filename = config[:log_path] filename = Padrino.root(filename) unless Pathname.new(filename).absolute? if File.directory?(filename) filename = File.join(filename, "#{Padrino.env}.log") else FileUtils.mkdir_p(File.dirname(filename)) end File.new(filename, 'a+') else FileUtils.mkdir_p(Padrino.root('log')) unless File.exist?(Padrino.root('log')) File.new(Padrino.root('log', "#{Padrino.env}.log"), 'a+') end when :null then StringIO.new when :stdout then $stdout when :stderr then $stderr else config[:stream] # return itself, probabilly is a custom stream. end new_logger = Padrino::Logger.new(config.merge(:stream => stream)) new_logger.extend(Padrino::Logger::Extensions) self.logger = new_logger end |
Instance Method Details
#<<(message = nil) ⇒ Object Also known as: write
Directly append message to the log.
466 467 468 469 470 471 472 473 |
# File 'lib/padrino-core/logger.rb', line 466 def <<( = nil) << "\n" unless [-1] == ?\n @@mutex.synchronize { @buffer << } flush if @auto_flush end |
#add(level, message = nil) ⇒ Object
Adds a message to the log - for compatibility with other loggers.
456 457 458 |
# File 'lib/padrino-core/logger.rb', line 456 def add(level, = nil) write() end |
#close ⇒ NilClass
Close and remove the current log object.
447 448 449 450 451 |
# File 'lib/padrino-core/logger.rb', line 447 def close flush @log.close if @log.respond_to?(:close) && !@log.tty? @log = nil end |
#flush ⇒ Object
Flush the entire buffer to the log object.
431 432 433 434 435 436 437 438 439 440 |
# File 'lib/padrino-core/logger.rb', line 431 def flush return unless @buffer.size > 0 @@mutex.synchronize do @buffer.each do |line| line.encode!(@sanitize_encoding, :invalid => :replace, :undef => :replace) if @sanitize_encoding @log.write(line) end @buffer.clear end end |
#format(message, level) ⇒ Object
476 477 478 |
# File 'lib/padrino-core/logger.rb', line 476 def format(, level) @format_message % [stylized_level(level), colorize(Time.now.strftime(@format_datetime), :yellow), .to_s.strip] end |
#source_location? ⇒ Boolean
424 425 426 |
# File 'lib/padrino-core/logger.rb', line 424 def source_location? !!@source_location end |