A Sinatra extension that makes logging within Your apps easy.
Please use Minnodes sinatra-logger instead of this version.
$ (sudo)? gem install sinatra-logger
This Gem depends upon the following:
-
sinatra ( >= 1.0 )
-
logger
-
rspec (>= 1.3.0 )
-
rack-test (>= 0.5.3)
-
rspec_hpricot_matchers (>= 0.1.0)
-
sinatra-tests (>= 0.1.6)
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?
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 ?”.
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.
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.
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.
If the above is not clear enough, please check the Specs for a better understanding.
If something is not behaving intuitively, it is a bug, and should be reported. Report it here: github.com/kematzy/sinatra-logger/issues
-
Making the logging work with Rack::CommonLogger, but still retain it’s independence.
-
Any other improvements you can think of.
-
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.
-
Monk Glue