I wrote little telegram bot based on java. Also i connected logger (log4j library) for log bot activity.
After server restart cron execute bot jar-file automatically with this command:
@reboot sleep 5 && java -jar /var/blablabla/praetorian19-1.0-jar-with-dependencies.jar
In this case logger doesn’t create any log files.
But, if i start bot directly under root with the same command, all work well. I see new log files and they updating correctly.
Directory and bot file have a 777 rules recursively.
Is any idea in which side i must dig?
log4j properties:
# Уровень логирования
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p- %m%n
# Апендер для работы с файлами
log4j.appender.file=org.apache.log4j.RollingFileAppender
# Путь где будет создаваться лог файл
log4j.appender.file.File=log_file.log
# Указываем максимальный размер файла с логами
log4j.appender.file.MaxFileSize=200MB
# Конфигурируем шаблон вывода логов в файл
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<!-- Author: Crunchify.com -->
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
</Console>
<RollingFile name="RollingFile" filename="log_file.log"
filepattern="${logPath}/%d{YYYYMMddHHmmss}-.log">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
2
Answers
Just this string in crontab task helped me to store logs in file: @reboot sleep 5 && java -jar /var/blabla/praetorian19-1.0-jar-with-dependencies.jar > /var/blabla/botlog.log
It is probably an issue with file or directory permissions … or something similar. The way to approach this is to follow the advice in the Log4j FAQ on “How do I debug my configuration”:
Among other things, you need to work out which of those logging configs you are actually using. (Log4j cannot use both of them …)