Log4j2 Logging is not working when application is running through crontab in linux

好久不见. 提交于 2019-12-12 18:37:51

问题


I have a java application in which I implemented logging. Here are the files

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

<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:

<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

*/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. 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. 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.

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

Thanks


回答1:


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


来源:https://stackoverflow.com/questions/32120259/log4j2-logging-is-not-working-when-application-is-running-through-crontab-in-lin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!