Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rocLv
Forked from kinopyo/custom_logger.rb
Created April 7, 2017 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rocLv/96bb03da08623c7b5f7653f4a3b92a24 to your computer and use it in GitHub Desktop.
Save rocLv/96bb03da08623c7b5f7653f4a3b92a24 to your computer and use it in GitHub Desktop.
Custom logger file in Rails
# 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
# in development.rb
require "custom_logger"
# in controller files
CUSTOM_LOGGER.info("info from custom logger")
CUSTOM_LOGGER.debug("debug from custom logger")
CUSTOM_LOGGER.error("error from custom logger")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment