如何使用 Log4j 和 Storm Framework 将日志写入文件? [英] How to write logs to a file using Log4j and Storm Framework?

查看:30
本文介绍了如何使用 Log4j 和 Storm Framework 将日志写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Storm 中使用 log4j 登录到文件时遇到了一些问题.

I am having bit of an issue in logging to a file using log4j in storm .

在提交我的拓扑之前,即在我的主要方法中,我写了一些日志语句并使用:

Before submitting my topology , i.e in my main method I wrote some log statements and configured the logger using :

PropertyConfigurator.configure(myLog4jProperties)

  • 现在,当我在 Eclipse 中使用 可执行 jar 运行拓扑时 -它的工作正常,并且正在按预期创建日志文件.

    当我使用java -jar MyJarFile"运行可执行文件时someOtherOptions",我可以看到正在配置 log4j 并且文件是正确形成并且日志记录在文件和控制台上完成(如在我的 log4j.properties 中定义)

    • Now when I run my topology using my executable jar in eclipse - its working fine and log files are being created as supposed.
      OR
      When i run my executable jar using "java -jar MyJarFile someOtherOptions", i can see log4j being configured and the files are formed correctly and logging is done on both files and console (as defined in my log4j.properties)

      但是当我使用storm jar MyJarFile MyMainClass someOtherOptions"运行同一个jar时,它无法创建和记录进入除控制台之外的任何文件.

      BUT when i run the same jar using "storm jar MyJarFile MyMainClass someOtherOptions" it is not being able to create and log into any of the files except on console.

      我指的是我正在提交拓扑之前打印的日志.

      I am talking about the logs I am printing BEFORE submitting my topology.

      有没有办法在使用storm时将我的语句记录到文件中?我一定要使用org.apache.log4j.

      Is there any way to log my statements in a file while using storm ? I am not bound to use org.apache.log4j.

      推荐答案

      我建议您使用 SLF4J 在 Storm 中进行日志记录.您可能可以让 LOG4J 工作,但您将在集群中的每个节点上进行额外的设置.既然 Storm 已经为你做了这件事,我真的不明白这一点.

      I would recommend using SLF4J for your logging within storm. You probably could get LOG4J working but you will have additional setup to do on each node in the cluster. Since storm does this for you already, I don't really see the point.

      在您的项目中,包含这些 maven 依赖项(slf4j-simple 用于您的单元测试!):

      In your project, include these maven dependencies (slf4j-simple for your unit tests!):

      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.5</version>
        <scope>test</scope>
      </dependency>
      

      然后,在您的拓扑/spouts/bolts 中,您将获得 Logger:

      Then, inside your topologies/spouts/bolts you just get the Logger:

      private static final Logger LOG = LoggerFactory.getLogger(MySpout.class);
      

      在我的环境中,INFO 及以上的任何内容都会记录到文件中,更重要的是,在 Storm UI 中可见.您只需要单击您的 spout/bolt,然后单击端口号即可转到日志查看器页面.

      In my environment, anything INFO and above is logged to file and, more importantly, visible in the Storm UI. You just need to click on your spout/bolt and then click on the port number to go to the log viewer page.

      如果你想获得实际文件,那么你可以从每个节点收集它们(我的在 /var/log/storm/ 中).

      If you want to get to the actual files then you can gather them off of each node (mine are in /var/log/storm/).

      这篇关于如何使用 Log4j 和 Storm Framework 将日志写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆