我在java中有一个程序,使用在xml配置文件中配置的log4j2来编写日志。 即使日志为空,我是否可以在log4j2中继续滚动文件。我需要使用log4j2生成的存档日志。现在,只有在日志不为空时才会创建存档日志,如果没有日志则不会创建存档日志。但我希望log4j2保持生成日志,即使有空日志。有什么建议吗?
这是我的代码:
var path = @"c:\public\temp";
DirectoryInfo dir = new DirectoryInfo(path);
foreach (var file in dir.GetFiles("*.txt"))
{
foreach (var line in File.ReadAllLines(file.FullName))
{
if (line.StartsWith("0001") || line.StartsWith("0002"))
{
var lineParts = line.Split(new[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
// This is assuming that the word count is in the column (which has index 2)
if (lineParts.Length > 2)
{
var wordCount = lineParts[2];
Console.WriteLine($"Found data in file {file.Name}:");
Console.WriteLine($" - Line starts with {lineParts[0].Substring(0, 4)}");
Console.WriteLine($" - Has word count of {wordCount}");
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
我还在我的java代码中添加了一些代码
<Appenders>
<RollingFile
fileName="logs/app-%d{yyyyMMdd}.log"
filePattern="logs/$${date:yyyy-MM-dd}/app-%d{yyyyMMdd}-%i.log.gz"
name="app_file">
<PatternLayout>
<Pattern>%d %p %m %ex%n
</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy
size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<Console name="stdout" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %highlight{%class{1.}:%L} [%c] [%p] %m%n" />
</Console>
</Appenders>
<Loggers>
<Logger additivity="false" includeLocation="true"
name="app">
<AppenderRef ref="app_file" level="trace"/>
<AppenderRef ref="stdout" level="trace"/>
</Logger>
<Logger level="error" name="com.ulisesbocchio.jasyptspringboot">
</Logger>
<Logger level="error" name="org.springframework">
</Logger>
<Root includeLocation="true">
<AppenderRef ref="stdout" />
</Root>
</Loggers>
答案 0 :(得分:0)
似乎只有在将某些内容写入文件时文件才会翻转。我认为基本上你应该做一些自定义,这里有两个选项:
RollingFile
并翻转文件,无论您想要什么。