Created
October 11, 2011 15:40
-
-
Save kinopyo/1278448 to your computer and use it in GitHub Desktop.
Custom logger file in Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/custom_logger.rb | |
class CustomLogger < Logger | |
def format_message(severity, timestamp, progname, msg) | |
"#{timestamp.to_formatted_s(:db)} #{severity} #{msg}\n" | |
end | |
end | |
logfile = File.open("#{Rails.root}/log/custom.log", 'a') # create log file | |
logfile.sync = true # automatically flushes data to file | |
CUSTOM_LOGGER = CustomLogger.new(logfile) # constant accessible anywhere |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in development.rb | |
require "custom_logger" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in controller files | |
CUSTOM_LOGGER.info("info from custom logger") | |
CUSTOM_LOGGER.debug("debug from custom logger") | |
CUSTOM_LOGGER.error("error from custom logger") |
Awesome .... Thnx a lot !!!
Yeah it is helpful.
Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍