skip to Main Content

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


  1. Chosen as BEST ANSWER

    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


  2. Is any idea in which side I must dig?

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

    1. Check that you have the right JAR files on the classpath
    2. Enable Log4j internal logging (as per the instructions) and look at what it is doing.

    Among other things, you need to work out which of those logging configs you are actually using. (Log4j cannot use both of them …)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search