Why use S3 for logging?
I love Heroku and deploy there all the time - its a great way to work with Ruby and web applications. But one of the "features" of Heroku is that its a read-only file system, which can make logging clumsy.
The best way I've found to deal with this situation is to shove all your logging duties off to S3. In fact, I have done this so often that I extracted the code I used to copy/paste from project to project and that's the point of this gem.
Its built using the EasyS3 gem (which itself is built on the Ruby S3 Gem), so you have to be good with that.
Install
Just use the gem
command:
gem install s3_logger
If you don't have easy_s3
or aws-s3
, they will be installed as well.
Setup
S3Logger can't do anything until you setup EasyS3, so go check out that documentation first. Then, you should probably set two more environment variables:
S3_LOGGER_FILE => your default log file
S3_LOGGER_PATH => your default log path
You don't have to setup these defaults, but if you don't you'll have to explicitly set them every time and not having to do that is the whole point of this gem!
Methods
There is really just one method:
log(, =false, file=ENV['S3_LOGGER_FILE'], path=ENV['S3_LOGGER_PATH'])
With four attributes:
data => the actual log message, required
timestamp_flag => prepends Time.now to message when true; optional; defaults to false
file => the file to write our message to; optional; default to the ENV variable
path => the path to our file; optional; defaults to the ENV variable
Usage
S3Logger supports a few different uses:
S3Logger.log('some smart log message')
# => writes this message to the default log file at the default path with no timestamp
S3Logger.log('some smart log message', true)
# => writes this message to the default log file at the default path with a timestamp
S3Logger.log('some smart log message', false, 'debug.log')
# => writes this message to the debug.log file at the default path with no timestamp
S3Logger.log('some smart log message', true, 'debug.log', 'debug/log/path/')
# => writes this message to the debug.log file using debug/log/path/ as the path with a timestamp