How to log something in Rails in an independent log file?

前端 未结 9 1245
栀梦
栀梦 2020-11-30 17:12

In rails I want to log some information in a different log file and not the standard development.log or production.log. I want to do this logging from a model class.

相关标签:
9条回答
  • 2020-11-30 17:24

    Here is my custom logger:

    class DebugLog
      def self.debug(message=nil)
        return unless Rails.env.development? and message.present?
        @logger ||= Logger.new(File.join(Rails.root, 'log', 'debug.log'))
        @logger.debug(message) 
      end
    end
    
    0 讨论(0)
  • 2020-11-30 17:25
    class Article < ActiveRecord::Base  
    
          LOGFILE = File.join(RAILS_ROOT, '/log/', "article_#{RAILS_ENV}.log")  
    
          def validate  
            log "was validated!"  
          end   
    
          def log(*args)  
           args.size == 1 ? (message = args; severity = :info) : (severity, message = args)  
           Article.logger severity, "Article##{self.id}: #{message}"  
         end  
    
         def self.logger(severity = nil, message = nil)  
           @article_logger ||= Article.open_log  
           if !severity.nil? && !message.nil? && @article_logger.respond_to?(severity)  
             @article_logger.send severity, "[#{Time.now.to_s(:db)}] [#{severity.to_s.capitalize}] #{message}\n"  
           end  
           message or @article_logger  
         end  
    
         def self.open_log  
           ActiveSupport::BufferedLogger.new(LOGFILE)  
         end  
    
       end  
    
    0 讨论(0)
  • 2020-11-30 17:25

    The Logging framework, with its deceptively simple name, has the sophistication you crave!

    Follow the very short instructions of logging-rails to get started filtering out noise, getting alerts, and choosing output in a fine-grained and high-level way.

    Pat yourself on the back when you are done. Log-rolling, daily. Worth it for that alone.

    0 讨论(0)
  • 2020-11-30 17:28
    class Post < ActiveRecord::Base
        def initialize(attributes)
            super(attributes)
            @logger = Logger.new("#{Rails.root}/log/post.log")
        end
    
        def logger
            @logger
        end
    
        def some_method
            logger.info('Test 1')
        end
    end
    
    ps = Post.new
    ps.some_method
    ps.logger.info('Test 2')
    Post.new.logger.info('Test 3')
    
    0 讨论(0)
  • 2020-11-30 17:32

    A decent option that works for me is to just add a fairly plain class to your app/models folder such as app/models/my_log.rb

    class MyLog
      def self.debug(message=nil)
        @my_log ||= Logger.new("#{Rails.root}/log/my.log")
        @my_log.debug(message) unless message.nil?
      end
    end
    

    then in your controller, or really almost anywhere that you could reference a model's class from within your rails app, i.e. anywhere you could do Post.create(:title => "Hello world", :contents => "Lorum ipsum"); or something similar you can log to your custom file like this

    MyLog.debug "Hello world"
    
    0 讨论(0)
  • 2020-11-30 17:38

    Define a logger class in (say) app/models/special_log.rb:

    class SpecialLog
      LogFile = Rails.root.join('log', 'special.log')
      class << self
        cattr_accessor :logger
        delegate :debug, :info, :warn, :error, :fatal, :to => :logger
      end
    end
    

    initialize the logger in (say) config/initializers/special_log.rb:

    SpecialLog.logger = Logger.new(SpecialLog::LogFile)
    SpecialLog.logger.level = 'debug' # could be debug, info, warn, error or fatal
    

    Anywhere in your app, you can log with:

    SpecialLog.debug("something went wrong")
    # or
    SpecialLog.info("life is good")
    
    0 讨论(0)
提交回复
热议问题