Skip to content

Instantly share code, notes, and snippets.

@SingularityMatrix
Forked from kinopyo/custom_logger.rb
Last active August 29, 2015 14:27
Show Gist options
  • Save SingularityMatrix/64076045b42d40ee9d14 to your computer and use it in GitHub Desktop.
Save SingularityMatrix/64076045b42d40ee9d14 to your computer and use it in GitHub Desktop.

Revisions

  1. @kinopyo kinopyo revised this gist Oct 11, 2011. 3 changed files with 3 additions and 1 deletion.
    1 change: 1 addition & 0 deletions custom_logger.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # lib/custom_logger.rb
    class CustomLogger < Logger
    def format_message(severity, timestamp, progname, msg)
    "#{timestamp.to_formatted_s(:db)} #{severity} #{msg}\n"
    1 change: 1 addition & 0 deletions development.rb
    Original file line number Diff line number Diff line change
    @@ -1 +1,2 @@
    # in development.rb
    require "custom_logger"
    2 changes: 1 addition & 1 deletion top_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # usage
    # in controller files
    CUSTOM_LOGGER.info("info from custom logger")
    CUSTOM_LOGGER.debug("debug from custom logger")
    CUSTOM_LOGGER.error("error from custom logger")
  2. @kinopyo kinopyo created this gist Oct 11, 2011.
    9 changes: 9 additions & 0 deletions custom_logger.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    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
    1 change: 1 addition & 0 deletions development.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    require "custom_logger"
    4 changes: 4 additions & 0 deletions top_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    # usage
    CUSTOM_LOGGER.info("info from custom logger")
    CUSTOM_LOGGER.debug("debug from custom logger")
    CUSTOM_LOGGER.error("error from custom logger")