rtomayko (owner)

Revisions

gist: 229851 Download_button fork
public
Description:
Automatic log config based on existence of log file
Public Clone URL: git://gist.github.com/229851.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Configure logging based on the existence of the log file. If the
# log file doesn't exist, logging is disable entirely and no log
# file will be created. If the log file does exist, logging is
# enabled and turned up to 11.
#
# Example is from config/environments/test.rb in a Rails app but it
# could easily be adapted to other environments/frameworks.
 
# Disabling logging in test environments can speed things up a bit.
# In your environment:
# unlink log/test.log - disable logging
# touch log/test.log - enable logging
if File.exist?("#{RAILS_ROOT}/log/test.log")
  config.log_level = :debug
else
  config.log_level = :error
  config.logger = Logger.new(File.open("/dev/null", 'wb'))
end
 
# BONUS POINTS: make it so that this works in a hot app. e.g.,
# logging is automatically enabled/disabled based on the presence
# of the log file -- and -- newly created files are reopened while
# app is running. You'll need to check whether the file is present
# and has the same inode at semi-frequent intervals somehow.