Tuesday, 31 March 2015

Custom Logging


For custom logging ruby provides a class called Logger, for custom logging create a method in your model or module which creates the log file by using:

def custom_logger
  @@custom_logger ||= Logger.new("#{Rails.root}/log/custom_logging.log")
end


hold this instance as a static variable so that all the instances share it.

This code will create the custom_logging file, now use the it as you use the usual rails logger.

This logger provides the same levels as of the rails logger.

custom_logger.info "test log data"
custom_logger.warn "test warning"
custom_logger.debug "test debug"
custom_logger.error "test debug error"
custom_logger.unknown "test unknown error"
custom_logger.fatal "test fatal error"

Check you log file all these statements would be written in it.

No comments:

Post a Comment