将日志滚动到特定目录 Log4j2

发布于 2025-01-11 07:36:53 字数 1956 浏览 1 评论 0原文

目标:拥有 Log4j2:

  1. 创建根“logs”目录(如果不存在)。
  2. 如果“存档”目录不存在,则创建一个目录,以便 RollingFile 附加程序可以保存滚动日志。

预期结果:

..\logs\MyLog.log
..\logs\archive\MyLog_2022-03-03_1.log
..\logs\archive\MyLog_2022-03-03_2.log
..\logs\archive\MyLog_2022-03-03_3.log
.
.
..\logs\archive\MyLog_2022-03-03_20.log

我期待创建“日志”和“日志”。 “存档”目录,以防它们不在启动/第一卷中。

实际结果:

关于“logs”目录:

  • 如果启动前“logs”目录不存在,则系统将继续运行记录任何内容或创建目录。
  • 如果“logs”目录在启动之前存在,那么它会将主日志文件保存到其中。

关于滚动日志的“归档”目录:

  • 如果“归档”目录在启动前不存在则创建,系统崩溃并抛出异常被抛出。
  • 如果“logs”目录在启动之前存在,则系统崩溃并引发异常。

我尝试过的操作:

  1. 用实际的硬编码值替换属性。
  2. 将路径开头替换为“./”。
  3. 将路径开头替换为“../”。
  4. 使用 Log4j2 中的各种可能的 查找 替换路径的开头文档。
  5. 对完整路径进行硬编码,该方法有效,但不是一个可以使用的选项,因为它会根据客户的安装位置而变化。

配置:

<Properties>
    <Property name="log.dir">logs</Property>
    <Property name="log.file">MyLog</Property>
    <Property name="pattern">%d %-5p [%t] %c{2}:%L - %m%n</Property>
</Properties>

<RollingFile name="MainLog" fileName="${log.dir}/${log.file}.log" filePattern="${log.dir}/archive/${log.file}_%d{MM-dd-yyyy}_%i.log.gz">
    <PatternLayout pattern="${pattern}" />
    <Policies>
        <SizeBasedTriggeringPolicy size="20 MB" />
    </Policies>
    <DefaultRolloverStrategy max="20" />
</RollingFile>

研究:

我尝试了各种 Google 搜索并研究了许多 Stackoverflow 问题,我不会在此处列出它们,因为它们不包含任何实际的相关数据。
奇怪的是,我找不到任何类似的问题。

我错过了什么?

Goal: Have Log4j2:

  1. Create the root "logs" directory if it doesn't exist.
  2. Create an "archive" directory if it doesn't exist so RollingFile appender can save the rolled logs.

Expected Result:

..\logs\MyLog.log
..\logs\archive\MyLog_2022-03-03_1.log
..\logs\archive\MyLog_2022-03-03_2.log
..\logs\archive\MyLog_2022-03-03_3.log
.
.
..\logs\archive\MyLog_2022-03-03_20.log

I'm expecting the creation of "logs" & "archive" directories in case they aren't there on start-up/1st roll.

Actual Results:

Regarding the "logs" directory:

  • if the "logs" directory doesn't exist prior to start-up then system continues running without logging anything or creating the directory.
  • if the "logs" directory exists prior to start-up then it saves the main log file to it.

Regarding the "archive" directory for rolling logs:

  • if the "archive" directory doesn't exist prior to start-up then it is created, the system crashes and an exception is thrown.
  • if the "logs" directory exists prior to start-up then the system crashes and an exception is thrown.

What I have tried:

  1. Replacing the properties with their actual hard-coded values.
  2. Replacing the path with "./" at the beginning.
  3. Replacing the path with "../" at the beginning.
  4. Replacing the beginning of the path with various possible Lookups from Log4j2's documentation.
  5. Hard-coding the full-path, which worked but is not an option to use as it will change according to the customer's installation location.

Configuration:

<Properties>
    <Property name="log.dir">logs</Property>
    <Property name="log.file">MyLog</Property>
    <Property name="pattern">%d %-5p [%t] %c{2}:%L - %m%n</Property>
</Properties>

<RollingFile name="MainLog" fileName="${log.dir}/${log.file}.log" filePattern="${log.dir}/archive/${log.file}_%d{MM-dd-yyyy}_%i.log.gz">
    <PatternLayout pattern="${pattern}" />
    <Policies>
        <SizeBasedTriggeringPolicy size="20 MB" />
    </Policies>
    <DefaultRolloverStrategy max="20" />
</RollingFile>

Research:

I've tried various Google searches and looked into many Stackoverflow questions, I will not be listing them here as they do not include any actual relevant data.
Weirdly enough, I wasn't able to find any similar question.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文