为什么Log4j2不将日志写入文件?

huangapple go评论69阅读模式
英文:

Why Log4j2 doesn't write logs to a file?

问题

以下是翻译好的部分:

这是我的配置 log4j2.xml,带有文件路径 src/com/tarasiuk/task_01/log/dataLogger.log

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">

    <Properties>
        <Property name="FORMAT_MESSAGE">
            %d{YYYY-MM-dd HH:mm:ss} [%t] Level:%-7p Class:%c{1}:%-5L - %msg%n
        </Property>

        <Property name="LOG_FILE">
            src/com/tarasiuk/task_01/log/dataLogger.log
        </Property>
    </Properties>

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${FORMAT_MESSAGE}" />
        </Console>

        <File name="File" fileName="${LOG_FILE}" append="false">
            <PatternLayout pattern="${FORMAT_MESSAGE}" />
        </File>
    </Appenders>

    <Loggers>
        <Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
            <AppenderRef ref="File" />
        </Logger>

        <Root level="info">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

我的项目结构:
为什么Log4j2不将日志写入文件?

我的操作:

  1. 将日志文件的 pathsrc/com/tarasiuk/task_01/log/dataLogger.log 更改为 com/tarasiuk/task_01/log/dataLogger.log - 没有结果。
  2. <Logger> 中的 leveldebug 更改为 info - 没有结果。

日志输出到控制台 - 没问题。但是为什么 Log4j2 不会将日志写入 文件 呢?

英文:

This is my configuration log4j2.xml with path to file - src/com/tarasiuk/task_01/log/dataLogger.log:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">

    <Properties>
        <Property name="FORMAT_MESSAGE">
            %d{YYYY-MM-dd HH:mm:ss} [%t] Level:%-7p Class:%c{1}:%-5L - %msg%n
        </Property>

        <Property name="LOG_FILE">
            src/com/tarasiuk/task_01/log/dataLogger.log
        </Property>
    </Properties>

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${FORMAT_MESSAGE}" />
        </Console>

        <File name="File" fileName="${LOG_FILE}" append="false">
            <PatternLayout pattern="${FORMAT_MESSAGE}" />
        </File>
    </Appenders>

    <Loggers>
        <Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
            <AppenderRef ref="File" />
        </Logger>

        <Root level="info">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

Structure of my project:
为什么Log4j2不将日志写入文件?

What I do:

  1. change path to log file from src/com/tarasiuk/task_01/log/dataLogger.log to com/tarasiuk/task_01/log/dataLogger.log - no result.
  2. change level in <Logger> from debug to info - no result.

Logs are output to the console - that ok. But why Log4j2 doesn't write logs to a file?

答案1

得分: 2

尝试使用以下 appender。

也许在您的情况下,它无法从属性中获取路径,所以我只提供了名称。
因此,它会自动在与您的应用程序相同的路径上创建文件。

<Appenders>
    <File name="dataLogger" fileName="dataLogger.log" append="false">
       <PatternLayout pattern="%level - %m%n"/>
    </File>
    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%level - %m%n"/>
    </Console>
  </Appenders>

这将有助于您。

英文:

Try with below appender.

May be in your case it is not able to get path from property, so i had provided only name.
So automatically it will create file on same path as your application is.

&lt;Appenders&gt;
    &lt;File name=&quot;dataLogger&quot; fileName=&quot;dataLogger.log&quot; append=&quot;false&quot;&gt;
       &lt;PatternLayout pattern=&quot;%level - %m%n&quot;/&gt;
    &lt;/File&gt;
    &lt;Console name=&quot;STDOUT&quot; target=&quot;SYSTEM_OUT&quot;&gt;
      &lt;PatternLayout pattern=&quot;%level - %m%n&quot;/&gt;
    &lt;/Console&gt;
  &lt;/Appenders&gt;

This will help you.

答案2

得分: 0

在使用Linux服务器上的相对路径时,我遇到了这个错误。我是通过一个 sh 脚本来启动我的程序的。

与Windows不同,Windows会根据启动脚本的位置设置当前目录,而Linux会将当前目录设置为用户主目录(除非另有指定)。

您可以通过使用 cd 命令并移动到启动 sh 脚本的目录来另行指定。

所以在我的情况下,日志文件被创建在错误的位置(即用户主目录内部)。
如果您遇到相同的问题,只需在您的 sh 脚本前面加上 cd 命令。

英文:

I had this error when using relative path on a linux server. I was starting my program using a sh script.

Unlike Windows, that sets the current directory based on where the launching script is placed, Linux sets the current directory as the user home directory (unless specified otherwise).

You can specify otherwise by using the cd command and move to the directory where the sh script is launched

So in my case, log file was created, but in the wrong location (i.e inside the home of the user).
If you are having the same problem just prepend a cd command to your sh script.

huangapple
  • 本文由 发表于 2020年8月27日 03:52:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63604843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定