Sha256: bfda9f866faefbb1462aec8278fdbb6599e7577ee835aaf8a508653b1e80bc3b

Contents?: true

Size: 1.44 KB

Versions: 16

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module Sinatra
  # = Sinatra::CustomLogger
  #
  # CustomLogger extension allows you to define your own logger instance
  # using +logger+ setting. That logger then will be available
  # as #logger helper method in your routes and views.
  #
  # == Usage
  #
  # === Classic Application
  #
  # To define your custom logger instance in a classic application:
  #
  #     require 'sinatra'
  #     require 'sinatra/custom_logger'
  #     require 'logger'
  #
  #     set :logger, Logger.new(STDOUT)
  #
  #     get '/' do
  #       logger.info 'Some message' # STDOUT logger is used
  #       # Other code...
  #     end
  #
  # === Modular Application
  #
  # The same for modular application:
  #
  #     require 'sinatra/base'
  #     require 'sinatra/custom_logger'
  #     require 'logger'
  #
  #     class MyApp < Sinatra::Base
  #       helpers Sinatra::CustomLogger
  #
  #       configure :development, :production do
  #         logger = Logger.new(File.open("#{root}/log/#{environment}.log", 'a'))
  #         logger.level = Logger::DEBUG if development?
  #         set :logger, logger
  #       end
  #
  #       get '/' do
  #         logger.info 'Some message' # File-based logger is used
  #         # Other code...
  #       end
  #     end
  #
  module CustomLogger
    def logger
      if settings.respond_to?(:logger)
        settings.logger
      else
        request.logger
      end
    end
  end

  helpers CustomLogger
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
sinatra-contrib-4.1.1 lib/sinatra/custom_logger.rb
sinatra-contrib-4.1.0 lib/sinatra/custom_logger.rb
sinatra-contrib-4.0.0 lib/sinatra/custom_logger.rb
sinatra-contrib-3.2.0 lib/sinatra/custom_logger.rb
sinatra-contrib-3.1.0 lib/sinatra/custom_logger.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/custom_logger.rb
sinatra-contrib-3.0.6 lib/sinatra/custom_logger.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/custom_logger.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/custom_logger.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/custom_logger.rb
sinatra-contrib-3.0.5 lib/sinatra/custom_logger.rb
sinatra-contrib-3.0.4 lib/sinatra/custom_logger.rb
sinatra-contrib-3.0.3 lib/sinatra/custom_logger.rb
sinatra-contrib-3.0.2 lib/sinatra/custom_logger.rb
sinatra-contrib-3.0.1 lib/sinatra/custom_logger.rb
sinatra-contrib-3.0.0 lib/sinatra/custom_logger.rb