Class: Padrino::Logger

### Home Classes Methods

### In Files

- lib/padrino-core/logger.rb

### Parent

Object

### Namespace

- MODULE Padrino::Logger::Colorize
- MODULE Padrino::Logger::Extensions
- CLASS Padrino::Logger::Rack

### Methods

- ::logger
- ::logger=
- ::new
- ::setup!
- #<<
- #add
- #close
- #flush
- #format
- #write

### Included Modules

- Padrino::Logger::Extensions

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Padrino
- Padrino::Application
- Padrino::ApplicationLoadError
- Padrino::ApplicationSetupError
- Padrino::Cli
- Padrino::Cli::Adapter
- Padrino::Cli::Base
- Padrino::Erubis
- Padrino::Erubis::SafeBufferEnhancer
- Padrino::Erubis::SafeBufferTemplate
- Padrino::Erubis::Template
- Padrino::Filter
- Padrino::Flash
- Padrino::Flash::Helpers
- Padrino::Flash::Storage
- Padrino::Logger
- Padrino::Logger::Colorize
- Padrino::Logger::Extensions
- Padrino::Logger::Rack
- Padrino::Module
- Padrino::Mounter
- Padrino::Mounter::MounterException
- Padrino::Reloader
- Padrino::Reloader::Rack
- Padrino::Rendering
- Padrino::Rendering::ClassMethods
- Padrino::Rendering::InstanceMethods
- Padrino::Rendering::SafeTemplate
- Padrino::Rendering::TemplateNotFound
- Padrino::Router
- Padrino::Routing
- Padrino::Routing::ClassMethods
- Padrino::Routing::InstanceMethods
- Padrino::Routing::Parent
- Padrino::Routing::UnrecognizedException
- Padrino::Server
- Padrino::ShowExceptions
- Padrino::Tasks
- Padrino::Utils
- HttpRouter
- HttpRouter::Node
- HttpRouter::Node::Path
- HttpRouter::Request
- HttpRouter::Route
- Haml
- Haml::Helpers
- Haml::Util
- Sinatra
- Sinatra::Request
- Slim
- Slim::Template
- Tilt
- Tilt::HamlTemplate
- FileSet
- Kernel
- Module
- Object
- ObjectSpace
- PadrinoTasks
- String
- SupportLite

No matching classes.

# Padrino::Logger

Padrinos internal logger, using all of Padrino log extensions.

### Constants

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
  :colorize_logging | Whether or not to colorize log messages. Defaults to: true

  @example

      Padrino::Logger::Config[:development] = { :log_level => :debug, :stream => :to_file }
      # or you can edit our defaults
      Padrino::Logger::Config[:development][:log_level] = :error
      # or you can use your stream
      Padrino::Logger::Config[:development][:stream] = StringIO.new

  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 }}

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

### Attributes

auto_flush[RW]
buffer[R]
colorize_logging[R]
init_args[R]
level[RW]
log[R]
log_static[RW]

### Public Class Methods

logger() click to toggle source

    # File lib/padrino-core/logger.rb, line 255
    def self.logger
      @_logger || setup!
    end

logger=(logger) click to toggle source

    # File lib/padrino-core/logger.rb, line 259
    def self.logger=(logger)
      logger.extend(Padrino::Logger::Extensions)

      @_logger = logger
    end

new(options={}) click to toggle source

To initialize the logger you create a new object, proxies to set_log.

@param [Hash] options

@option options [Symbol] :stream ($stdout)

    Either an IO object or a name of a logfile. Defaults to $stdout

@option options [Symbol] :log_level (:production in the production environment and :debug otherwise)

    The log level from, e.g. :fatal or :info.

@option options [Symbol] :auto_flush (true)

    Whether the log should automatically flush after new messages are
    added. Defaults to true.

@option options [Symbol] :format_datetime (" [%d/%b/%Y %H:%M:%S] ")

    Format of datetime

@option options [Symbol] :format_message ("%s -%s%s")

    Format of message

@option options [Symbol] :log_static (false)

    Whether or not to show log messages for static files.

@option options [Symbol] :colorize_logging (true)

    Whether or not to colorize log messages. Defaults to: true

    # File lib/padrino-core/logger.rb, line 322
    def initialize(options={})
      @buffer          = []
      @auto_flush      = options.has_key?(:auto_flush) ? options[:auto_flush] : true
      @level           = options[:log_level] ? Padrino::Logger::Levels[options[:log_level]] : Padrino::Logger::Levels[:debug]
      @log             = options[:stream]  || $stdout
      @log.sync        = true
      @format_datetime = options[:format_datetime] || "%d/%b/%Y %H:%M:%S"
      @format_message  = options[:format_message]  || "%s - %s %s"
      @log_static      = options.has_key?(:log_static) ? options[:log_static] : false
      @colorize_logging = options.has_key?(:colorize_logging) ? options[:colorize_logging] : true
      colorize! if @colorize_logging
    end

setup!() click to toggle source

Setup a new logger

@return [Padrino::Logger]

    A {Padrino::Logger} instance

    # File lib/padrino-core/logger.rb, line 271
    def self.setup!
      self.logger = begin
        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

        Padrino::Logger.new(config.merge(:stream => stream))
      end
    end

### Public Instance Methods

<<(message = nil) click to toggle source

Directly append message to the log.

@param [String] message

    The message

    # File lib/padrino-core/logger.rb, line 371
    def <<(message = nil)
      message << "\n" unless message[-1] == \n\
      @@mutex.synchronize {
        @buffer << message
      }
      flush if @auto_flush
      message
    end

Also aliased as: write
add(level, message = nil) click to toggle source

Adds a message to the log - for compatibility with other loggers.

@private

    # File lib/padrino-core/logger.rb, line 361
    def add(level, message = nil)
      write(message)
    end

close() click to toggle source

Close and remove the current log object.

@return [NilClass]

    # File lib/padrino-core/logger.rb, line 351
    def close
      flush
      @log.close if @log.respond_to?(:close) && !@log.tty?
      @log = nil
    end

flush() click to toggle source

Flush the entire buffer to the log object.

    # File lib/padrino-core/logger.rb, line 338
    def flush
      return unless @buffer.size > 0
      @@mutex.synchronize do
        @log.write(@buffer.join(''))
        @buffer.clear
      end
    end

format(message, level) click to toggle source

    # File lib/padrino-core/logger.rb, line 381
    def format(message, level)
      @format_message % [stylized_level(level), colorize(Time.now.strftime(@format_datetime), :yellow), message.to_s.strip]
    end

write(message = nil) click to toggle source
Alias for: <<

[Validate]

Generated with the Darkfish Rdoc Generator 2.