Skip to content

Instantly share code, notes, and snippets.

@kinopyo
Created October 11, 2011 15:40
Show Gist options
  • Star 65 You must be signed in to star a gist
  • Fork 19 You must be signed in to fork a gist
  • Save kinopyo/1278448 to your computer and use it in GitHub Desktop.
Save kinopyo/1278448 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")
@buoyant
Copy link

buoyant commented Dec 31, 2012

thanks helped me a lot.

@johnsogg
Copy link

Very useful!

@KarineValenca
Copy link

Thanks <3

@McGeekiest
Copy link

Brilliant. Thank you.

@vishwas-webonise
Copy link

Sweet & Simple, Very helpful, thank u :)

@loray513
Copy link

Thanks

@lnaia
Copy link

lnaia commented Oct 5, 2015

cheers!

@tommydunn
Copy link

awesome. thanks!

@glasses618
Copy link

Thanks a lot! It's helpful!

@chabgood
Copy link

👍

@RanjitReddy
Copy link

Awesome .... Thnx a lot !!!

@atiftechverx
Copy link

atiftechverx commented Jul 1, 2019

Yeah it is helpful.

@mawaldne
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment