当应用程序通过linux中的crontab运行时,Log4j2 Logging无效 [英] Log4j2 Logging is not working when application is running through crontab in linux

查看:223
本文介绍了当应用程序通过linux中的crontab运行时,Log4j2 Logging无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java应用程序,我在其中实现了日志记录。以下是文件

I have a java application in which I implemented logging. 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>

现在我在Linux中设置cron作业

Now I set the cron job in Linux like

*/15 9-23 * * * /usr/bin/java -jar /pathToApplication/application.jar arg1

现在的问题是如果我手动运行这个jar,那么一切正常。就像我进入应用程序路径并执行命令一样

Now the problem is If I run this jar manually then everything works fine. Like I go to the application path and execute the command

java -jar application.jar arg1

但是当这个命令通过cronjob运行时,就没有记录。虽然应用程序工作正常,但我收到电子邮件并且运行顺利但没有记录。

But when this command run through cronjob then there is no logging. Although application works fine, I get the emails and run smoothly but no logging.

我想知道为什么当我手动运行它然后有记录但是当通过cron作业运行时然后没有记录。

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.

如何解决此日志记录问题?此权限问题是否存在?如果是,则其他文件相关任务如何表现良好(创建,阅读,写作)。 ?

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). ?

谢谢

推荐答案

问题可能出在工作目录。 Crontab使用crontab用户的主目录执行任务作为工作目录。你应该像这样修复你的crontab smth

The problem can be with working directory. Crontab executes tasks with home directory of the crontab user as working directory. You should just fix your crontab smth like this

*/15 9-23 * * * cd /pathToApplication/ && /usr/bin/java -jar /pathToApplication/application.jar arg1

这篇关于当应用程序通过linux中的crontab运行时,Log4j2 Logging无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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