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

前端 未结 9 1317
栀梦
栀梦 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: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')
    

提交回复
热议问题