将日志滚动到特定目录 Log4j2
目标:拥有 Log4j2:
- 创建根“logs”目录(如果不存在)。
- 如果“存档”目录不存在,则创建一个目录,以便 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”目录在启动之前存在,则系统崩溃并引发异常。
我尝试过的操作:
- 用实际的硬编码值替换属性。
- 将路径开头替换为“
./
”。 - 将路径开头替换为“
../
”。 - 使用 Log4j2 中的各种可能的 查找 替换路径的开头文档。
- 对完整路径进行硬编码,该方法有效,但不是一个可以使用的选项,因为它会根据客户的安装位置而变化。
配置:
<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:
- Create the root "logs" directory if it doesn't exist.
- 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:
- Replacing the properties with their actual hard-coded values.
- Replacing the path with "
./
" at the beginning. - Replacing the path with "
../
" at the beginning. - Replacing the beginning of the path with various possible Lookups from Log4j2's documentation.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论