Module: Sinatra::Logger

Defined in:
lib/sinatra/logger.rb

Overview

Sinatra::Logger

A Sinatra extension that makes logging within Your apps easy.

Installation

$  (sudo)? gem install sinatra-logger

Dependencies

This Gem depends upon the following:

Runtime:

  • sinatra ( >= 1.0 )

  • logger

Development & Tests:

  • rspec (>= 1.3.0 )

  • rack-test (>= 0.5.3)

  • rspec_hpricot_matchers (>= 0.1.0)

  • sinatra-tests (>= 0.1.6)

Getting Started

To get logging in your app, just register the extension in your sub-classed Sinatra app:

class YourApp < Sinatra::Base

  # NB! you need to set the root of the app first
  # set :root, '/path/2/the/root/of/your/app'

  register(Sinatra::Logger)

  <snip...>

end

In your “classic” Sinatra app, you just require the extension like this:

require 'rubygems'
require 'sinatra'
require 'sinatra/logger'

# NB! you need to set the root of the app first
# set :root, '/path/2/the/root/of/your/app'

<snip...>

Then in your App’s route or helper method declarations, just use the #logger

get '/some/route' do
  logger.debug("some informative message goes here")
  <snip...>
end

helpers do
  def some_helper_method
    logger.info("some equally informative message goes here")
    <snip...>
  end
end

That’s pretty painless, no?

Logging Levels

The default Log level is :warn.

All the available logging levels are those of Logger, which are:

  • logger.fatal(msg) - - (FATAL) - an unhandleable error that results in a program crash

  • logger.error(msg) - - (ERROR) - a handleable error condition

  • logger.warn(msg) - - (WARN) - a warning

  • logger.info(msg) - - (INFO) - generic (useful) information about system operation

  • logger.debug(msg) - - (DEBUG) - low-level information for developers

OK, by now you might be asking yourself,

“So where does the log messages go then ?”.

Logging Locations

By default the logger will log it’s message to the following path:

< the root of your app >/log/< environment >.log

In other words if your app’s root is [ /home/www/your-great-app/ ] and it’s running in :production mode, then the log location would become:

/home/www/your-great-app/log/production.log

NB! this extension takes for granted that you have a ../log/ directory with write access at the root of your app.

Custom Logging Location

If the defaults are NOT for you, then just do…

class YourApp < Sinatra::Base

  register(Sinatra::Logger)

  set: :logger_log_file, lambda { "/path/2/your/log/file.ext" }

  <snip...>

end

# the lambda { } is required, especially if you have variables in the path

…, now your log messages will be written to that log file.

Setting Log Level

Finally, to use a different Log level for your app, other than the default :warn just…

class YourApp < Sinatra::Base

  register(Sinatra::Logger)

  set: :logger_level, :fatal # or :error, :warn, :info, :debug
  <snip...>
end

That’s it. I hope that’s easy enough.

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



187
188
189
190
191
192
193
194
195
# File 'lib/sinatra/logger.rb', line 187

def self.registered(app)
  app.helpers Sinatra::Logger::Helpers
  
  # set the output log level
  app.set :logger_level, :warn
  # set the full path to the log file
  app.set :logger_log_file, lambda { File.join(root, 'log', "#{environment}.log") }
  
end

.versionObject

Returns the version string for this extension

Examples

Sinatra::Logger.version  => 'Sinatra::Logger v0.1.0'


160
# File 'lib/sinatra/logger.rb', line 160

def self.version; "Sinatra::Logger v#{VERSION}"; end