JRuby on Rails 应用程序中的 log4j - 将日志重定向到单独的文件

发布于 2024-12-28 23:36:06 字数 1204 浏览 2 评论 0原文

我希望在 JRuby on Rails 应用程序中获得更精细的日志记录。我介绍了log4j。我目前有一个如下所示的属性文件:

log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

#
log4j.logger.Rails=INFO, A

log4j.appender.A=org.apache.log4j.RollingFileAppender
log4j.appender.A.File=/usr/share/tomcat6/logs/production.log
log4j.appender.A.MaxFileSize=10000KB
log4j.appender.A.MaxBackupIndex=5
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

为了让 Rails.logger 使用它,我弹出以下初始化程序:

org.apache.log4j.PropertyConfigurator.configure("#{::Rails.root.to_s}/config/log4j.properties")

现在,我还希望能够执行类似以下操作:

Rails.logger.feature_1( “记录到feature_1.log”)
Rails.logger.feature_N("Log to feature_N.log")

实现这一目标的最佳方法是什么,以免所有内容都写入一个日志文件?

另外要注意的是,我正在使用此处发布的适配器: http://squaremasher.blogspot.com/2009/08/jruby-rails-and-log4j.html

I want to get more granular logging in my JRuby on Rails app. I introduced log4j. I currently have a properties file that looks like the following:

log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

#
log4j.logger.Rails=INFO, A

log4j.appender.A=org.apache.log4j.RollingFileAppender
log4j.appender.A.File=/usr/share/tomcat6/logs/production.log
log4j.appender.A.MaxFileSize=10000KB
log4j.appender.A.MaxBackupIndex=5
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

To get Rails.logger to use this, I pop in the following initializer:

org.apache.log4j.PropertyConfigurator.configure("#{::Rails.root.to_s}/config/log4j.properties")

Now, I want to be able to also do something like:

Rails.logger.feature_1("Log to feature_1.log")
Rails.logger.feature_N("Log to feature_N.log")

What's the best way of accomplishing this so not everything goes to one log file?

Also just to note, I am using the adapter that was blogged about here: http://squaremasher.blogspot.com/2009/08/jruby-rails-and-log4j.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2025-01-04 23:36:06

首先,您应该在 log4j.properties 文件中为您需要的每个“功能”定义一个附加程序/记录器配置:

...
log4j.logger.Rails=INFO, A

log4j.appender.A=org.apache.log4j.RollingFileAppender
log4j.appender.A.File=/usr/share/tomcat6/logs/production.log

...
log4j.logger.feature_1=INFO, feature_1

log4j.appender.feature_1=org.apache.log4j.RollingFileAppender
log4j.appender.feature_1.File=/path/to/your/feature_1.log
...
log4j.logger.feature_N=INFO, feature_N

log4j.appender.feature_N=org.apache.log4j.RollingFileAppender
log4j.appender.feature_N.File=/path/to/your/feature_N.log

这样每个记录器都将拥有自己的严重性过滤器和目标文件。

然后,您可以根据您在月球的 Ruby 一侧调用的方法(来自您提供的示例适配器)来选择它,而不是总是使用相同的记录器:

class Log4jAdapter

  def method_missing(meth, *args)
    # Checking the method name matches feature_N
    if /\Afeature_(\d+)\z/ =~ method.to_s
      # Retrieve the appropriate logger => getLogger('feature_N')
      logger = org.apache.log4j.Logger.getLogger(method.to_s)
      # Log !
      logger.log *args
    else
        puts "UNSUPPORTED METHOD CALLED: #{meth}"
    end
  end

end

未经测试,但我认为您会明白的。

First, you should define an appender/logger configuration for each "feature" you need in your log4j.properties file :

...
log4j.logger.Rails=INFO, A

log4j.appender.A=org.apache.log4j.RollingFileAppender
log4j.appender.A.File=/usr/share/tomcat6/logs/production.log

...
log4j.logger.feature_1=INFO, feature_1

log4j.appender.feature_1=org.apache.log4j.RollingFileAppender
log4j.appender.feature_1.File=/path/to/your/feature_1.log
...
log4j.logger.feature_N=INFO, feature_N

log4j.appender.feature_N=org.apache.log4j.RollingFileAppender
log4j.appender.feature_N.File=/path/to/your/feature_N.log

This way each logger will have it own severity filter and destination file.

Then instead of always using the same logger you may select it depending on the method you called on the Ruby side of the moon (from the example adapter you provided) :

class Log4jAdapter

  def method_missing(meth, *args)
    # Checking the method name matches feature_N
    if /\Afeature_(\d+)\z/ =~ method.to_s
      # Retrieve the appropriate logger => getLogger('feature_N')
      logger = org.apache.log4j.Logger.getLogger(method.to_s)
      # Log !
      logger.log *args
    else
        puts "UNSUPPORTED METHOD CALLED: #{meth}"
    end
  end

end

Untested but I think you'll get the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文