java 如何使用 Log4j 和 Storm Framework 将日志写入文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25679963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write logs to a file using Log4j and Storm Framework?
提问by holmes840
I am having bit of an issue in logging to a file using log4j in storm .
我在 Storm 中使用 log4j 登录到文件时遇到了一些问题。
Before submitting my topology , i.e in my main method I wrote some log statements and configured the logger using :
在提交我的拓扑之前,即在我的主要方法中,我写了一些日志语句并使用以下配置记录器:
PropertyConfigurator.configure(myLog4jProperties)
Now when I run my topology using my executable jarin 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)BUTwhen 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.
现在,当我在 eclipse 中使用我的可执行 jar运行我的拓扑时- 它的工作正常并且日志文件正在按预期创建。
或者,
当我使用“java -jar MyJarFile someOtherOptions”运行我的可执行 jar 时,我可以看到 log4j 正在配置并且文件形成正确,并且在文件和控制台上都完成了日志记录(如我的 log4j.properties 中所定义)但是,当我使用“storm jar MyJarFile MyMainClass someOtherOptions”运行同一个 jar 时,它无法创建和登录除控制台之外的任何文件。
I am talking about the logs I am printing BEFOREsubmitting my topology.
我说的是在提交拓扑之前我正在打印的日志。
Is there any way to log my statements in a file while using storm ? I am notbound to use org.apache.log4j.
有没有办法在使用 Storm 时将我的语句记录到文件中?我不一定要使用org.apache.log4j。
回答by Naresh
The storm framework uses its own logging. Your logs most likely will end up in the logs dir where storm is installed({Storm DIR}/logs). You can find the storm log properties in {Storm DIR}/logback/cluster.xml. It uses logback not log4j
Storm 框架使用自己的日志记录。您的日志很可能会出现在安装storm的日志目录({Storm DIR}/logs)中。您可以在 {Storm DIR}/logback/cluster.xml 中找到风暴日志属性。它使用 logback 而不是 log4j
回答by Kit Menke
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.
我建议使用 SLF4J 来记录风暴。您可能可以让 LOG4J 工作,但您将在集群中的每个节点上进行额外的设置。由于storm已经为你做了这件事,我真的不明白这一点。
In your project, include these maven dependencies (slf4j-simple for your unit tests!):
在您的项目中,包括这些 maven 依赖项(slf4j-simple 用于您的单元测试!):
<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>
Then, inside your topologies/spouts/bolts you just get the Logger:
然后,在您的拓扑/spouts/bolts 中,您将获得 Logger:
private static final Logger LOG = LoggerFactory.getLogger(MySpout.class);
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.
在我的环境中,任何 INFO 及以上内容都记录到文件中,更重要的是,在 Storm UI 中可见。您只需要单击您的 spout/bolt,然后单击端口号即可转到日志查看器页面。
If you want to get to the actual files then you can gather them off of each node (mine are in /var/log/storm/
).
如果您想获取实际文件,则可以从每个节点收集它们(我的在 中/var/log/storm/
)。