Skip to content

WaYdotNET/sinatra-logger

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

RTFM

If the above is not clear enough, please check the Specs for a better understanding.

Errors / Bugs

If something is not behaving intuitively, it is a bug, and should be reported. Report it here: github.com/kematzy/sinatra-logger/issues

TODOs

  • Making the logging work with Rack::CommonLogger, but still retain it’s independence.

  • Any other improvements you can think of.

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history.

    • (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 kematzy. Released under the MIT License.

See LICENSE for details.

Inspirational Source

  • Monk Glue

About

A Sinatra Extension that makes logging easy

Resources

License

Stars

Watchers

Forks

Packages

No packages published