[英]Log4j2 Logging is not working when application is running through crontab in linux
I have a java application in which I implemented logging. 我有一个Java应用程序,在其中实现了日志记录。 Here are the files 这是文件
log4j2.xml log4j2.xml
<configuration xmlns:xi="http://www.w3.org/2001/XInclude" status="WARN">
<xi:include href="log4j-xinclude-appenders.xml" />
<xi:include href="log4j-xinclude-loggers.xml" />
</configuration>
log4j-xinclude-appenders.xml log4j的-XInclude的-appenders.xml
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<Routing name="RoutingAppender">
<Routes pattern="${ctx:logFileName}">
<Route>
<RollingFile name="Rolling-${ctx:logFileName}"
fileName="logs/${ctx:logFileName}.log"
filePattern="logs/${ctx:logFileName}-%d{MM-dd-yyyy}-%i.log">
<PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %c{36} %l: %msg%n"/>
<SizeBasedTriggeringPolicy size="5 MB" />
<DefaultRolloverStrategy min="1" max="4" />
</RollingFile>
</Route>
<Route ref="Console" key="${ctx:logFileName}"/>
</Routes>
</Routing>
</appenders>
log4j-xinclude-loggers.xml: log4j的-XInclude的-loggers.xml:
<loggers>
<root level="warn">
<appender-ref ref="Console" />
</root>
<logger name="abc.def.ghi.regulators" level="info" additivity="false">
<appender-ref ref="RoutingAppender" />
</logger>
<logger name="org.apache" level="info" />
<logger name="org.springframework" level="info" />
</loggers>
Now I set the cron job in Linux like 现在我在Linux中设置cron作业,例如
*/15 9-23 * * * /usr/bin/java -jar /pathToApplication/application.jar arg1
Now the problem is If I run this jar manually then everything works fine. 现在的问题是,如果我手动运行此jar,则一切正常。 Like I go to the application path and execute the command 就像我去应用程序路径并执行命令
java -jar application.jar arg1
But when this command run through cronjob then there is no logging. 但是,当此命令通过cronjob运行时,则不会记录日志。 Although application works fine, I get the emails and run smoothly but no logging. 尽管应用程序运行正常,但我收到了电子邮件,运行平稳,但没有日志记录。
I am wondering why when I run it manually then there is logging but when run through cron job then there is no logging. 我想知道为什么当我手动运行它时会出现日志记录,但是通过cron作业运行时却没有日志记录。
I am creating files in my application, writing to files, reading from files everything is working except logging when running using cron job. 我在应用程序中创建文件,写入文件,从文件读取,除了使用cron作业运行时进行日志记录之外,其他所有东西都在工作。
How can I solve this logging problem ? 如何解决此日志记录问题? Is this permission issue ? 这是权限问题吗? IF yes then how other files related tasks are performing well (creating, reading, writing). 如果是,那么其他文件相关任务的执行情况(创建,读取,写入)如何良好。 ? ?
Thanks 谢谢
The problem can be with working directory . 问题可能出在工作目录上 。 Crontab executes tasks with home directory of the crontab user as working directory . Crontab以crontab用户的主目录作为工作目录执行任务。 You should just fix your crontab smth like this 您应该像这样修复您的crontab smth
*/15 9-23 * * * cd /pathToApplication/ && /usr/bin/java -jar /pathToApplication/application.jar arg1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.