Skip to content

Commit ecae664

Browse files
committed
fix issues/879
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent 85968fa commit ecae664

File tree

6 files changed

+157
-5
lines changed

6 files changed

+157
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2024, QOS.ch. All rights reserved.
4+
*
5+
* This program and the accompanying materials are dual-licensed under
6+
* either the terms of the Eclipse Public License v1.0 as published by
7+
* the Eclipse Foundation
8+
*
9+
* or (per the licensee's choosing)
10+
*
11+
* under the terms of the GNU Lesser General Public License version 2.1
12+
* as published by the Free Software Foundation.
13+
*/
14+
15+
package ch.qos.logback.classic.issue.github879;
16+
17+
18+
import ch.qos.logback.classic.ClassicConstants;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
22+
public class Main {
23+
24+
25+
static {
26+
27+
System.setProperty("outputPath", "logback-classic/target/test-output/issue879");
28+
String configFilePath = "logback-classic/src/test/java/ch/qos/logback/classic/issue/github879/";
29+
System.setProperty("logback.statusListenerClass", "stdout");
30+
System.setProperty(ClassicConstants.CONFIG_FILE_PROPERTY, configFilePath+"logback-879.xml");
31+
}
32+
33+
34+
public static void main(String[] args) {
35+
final Logger LOGGER = LoggerFactory.getLogger(Main.class);
36+
37+
for (int i = 0; i < 20_000; i++) {
38+
LOGGER.info("X".repeat(45));
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
~ Logback: the reliable, generic, fast and flexible logging framework.
4+
~ Copyright (C) 1999-2024, QOS.ch. All rights reserved.
5+
~
6+
~ This program and the accompanying materials are dual-licensed under
7+
~ either the terms of the Eclipse Public License v1.0 as published by
8+
~ the Eclipse Foundation
9+
~
10+
~ or (per the licensee's choosing)
11+
~
12+
~ under the terms of the GNU Lesser General Public License version 2.1
13+
~ as published by the Free Software Foundation.
14+
-->
15+
16+
<!DOCTYPE configuration>
17+
18+
<configuration>
19+
<import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/>
20+
<import class="ch.qos.logback.core.rolling.RollingFileAppender"/>
21+
<import class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"/>
22+
23+
<variable name="outputPath" value=""/>e
24+
25+
<appender name="ROLLING" class="RollingFileAppender">
26+
<rollingPolicy class="SizeAndTimeBasedRollingPolicy">
27+
<fileNamePattern>${outputPath}/mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
28+
<maxFileSize>1MB</maxFileSize>
29+
<maxHistory>90</maxHistory>
30+
<totalSizeCap>20GB</totalSizeCap>
31+
</rollingPolicy>
32+
<encoder class="PatternLayoutEncoder">
33+
<pattern>%msg%n</pattern>
34+
</encoder>
35+
</appender>
36+
37+
<root level="DEBUG">
38+
<appender-ref ref="ROLLING"/>
39+
</root>
40+
</configuration>

logback-core/src/main/java/ch/qos/logback/core/rolling/DefaultTimeBasedFileNamingAndTriggeringPolicy.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void start() {
3838
return;
3939
if (tbrp.fileNamePattern.hasIntegerTokenCOnverter()) {
4040
addError("Filename pattern [" + tbrp.fileNamePattern
41-
+ "] contains an integer token converter, i.e. %i, INCOMPATIBLE with this configuration. Remove it.");
41+
+ "] contains an integer token converter, i.e. %i, INCOMPATIBLE with this configuration. Please remove it.");
4242
return;
4343
}
4444

logback-core/src/main/java/ch/qos/logback/core/rolling/RollingFileAppender.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,14 @@ public void start() {
9898
}
9999
}
100100

101-
currentlyActiveFile = new File(getFile());
102101
addInfo("Active log file name: " + getFile());
102+
currentlyActiveFile = new File(getFile());
103+
initializeLengthCounter();
103104
super.start();
104105
}
105106

107+
108+
106109
private boolean checkForFileAndPatternCollisions() {
107110
if (triggeringPolicy instanceof RollingPolicyBase) {
108111
final RollingPolicyBase base = (RollingPolicyBase) triggeringPolicy;
@@ -147,6 +150,14 @@ private boolean innerCheckForFileNamePatternCollisionInPreviousRFA(FileNamePatte
147150
return collisionsDetected;
148151
}
149152

153+
private void initializeLengthCounter() {
154+
if(getLengthCounter() != null && currentlyActiveFile.exists()) {
155+
long currentFileLength = currentlyActiveFile.length();
156+
addInfo("Setting currentFileLength to "+currentFileLength+ " for "+currentlyActiveFile);
157+
incrementByteCount(currentFileLength);
158+
}
159+
}
160+
150161
@Override
151162
public void stop() {
152163
if (!isStarted()) {
@@ -293,13 +304,18 @@ public void setTriggeringPolicy(TriggeringPolicy<E> policy) {
293304

294305
@Override
295306
protected void updateByteCount(byte[] byteArray) {
307+
if(byteArray == null)
308+
return;
309+
incrementByteCount(byteArray.length);
310+
}
296311

312+
void incrementByteCount(long increment) {
297313
LengthCounter lengthCounter = getLengthCounter();
298314
if (lengthCounter == null)
299315
return;
300316

301-
if (byteArray != null && byteArray.length > 0) {
302-
lengthCounter.add(byteArray.length);
317+
if (increment > 0) {
318+
lengthCounter.add(increment);
303319
}
304320
}
305321

logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP_Test.java

+55
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@
1515

1616
import java.io.File;
1717
import java.io.IOException;
18+
import java.nio.file.Files;
19+
import java.nio.file.Paths;
20+
import java.text.SimpleDateFormat;
1821
import java.util.Date;
1922
import java.util.List;
2023
import java.util.concurrent.ExecutionException;
2124
import java.util.function.UnaryOperator;
2225

26+
import ch.qos.logback.core.CoreConstants;
27+
import ch.qos.logback.core.util.CachingDateFormatter;
2328
import ch.qos.logback.core.util.Duration;
29+
import ch.qos.logback.core.util.StatusPrinter;
2430
import org.junit.jupiter.api.BeforeEach;
2531
import org.junit.jupiter.api.Test;
2632

@@ -31,6 +37,7 @@
3137
import ch.qos.logback.core.status.testUtil.StatusChecker;
3238
import ch.qos.logback.core.util.FileSize;
3339

40+
import static org.junit.jupiter.api.Assertions.assertEquals;
3441
import static org.junit.jupiter.api.Assertions.assertFalse;
3542

3643
public class SizeAndTimeBasedFNATP_Test extends ScaffoldingForRollingTests {
@@ -61,6 +68,7 @@ private void initRollingFileAppender(RollingFileAppender<Object> rfa, String fil
6168
private void initPolicies(RollingFileAppender<Object> rfa, TimeBasedRollingPolicy<Object> tbrp,
6269
String filenamePattern, int sizeThreshold, long givenTime, long lastCheck) {
6370
sizeAndTimeBasedFNATP = new SizeAndTimeBasedFileNamingAndTriggeringPolicy<Object>();
71+
sizeAndTimeBasedFNATP.setContext(context);
6472
sizeAndTimeBasedFNATP.setCheckIncrement(Duration.buildByMilliseconds(10));
6573
tbrp.setContext(context);
6674
sizeAndTimeBasedFNATP.setMaxFileSize(new FileSize(sizeThreshold));
@@ -243,6 +251,53 @@ public void checkDateCollision() {
243251
checker.assertContainsMatch("The date format in FileNamePattern");
244252
}
245253

254+
@Test
255+
public void checkInitialFileSize_withFile() throws IOException {
256+
String stem = "foo.log";
257+
String testId = "checkDateCollision";
258+
String fixedContent = "Hello world";
259+
byte[] fixedContentBytes = fixedContent.getBytes();
260+
261+
String fileProperty = randomOutputDir + stem;
262+
Files.createDirectories(Paths.get(randomOutputDir));
263+
Files.write(Paths.get(fileProperty), fixedContentBytes);
264+
265+
initRollingFileAppender(rfa1, fileProperty);
266+
sizeThreshold = 300;
267+
initPolicies(rfa1, tbrp1, randomOutputDir + testId + "-%d-%i.txt", sizeThreshold,
268+
currentTime, 0);
269+
270+
//StatusPrinter.print(context);
271+
272+
assertEquals(fixedContentBytes.length, tbrp1.getLengthCounter().getLength());
273+
}
274+
275+
276+
@Test
277+
public void checkInitialFileSize_withoutFile() throws IOException {
278+
String testId = "checkInitialFileSize_withoutFile";
279+
String fixedContent = "Hello world";
280+
byte[] fixedContentBytes = fixedContent.getBytes();
281+
282+
283+
CachingDateFormatter cdf = new CachingDateFormatter(CoreConstants.DAILY_DATE_PATTERN);
284+
String nowString = cdf.format(currentTime);
285+
String pathToFirstFile = randomOutputDir + testId + "-"+nowString+"-0.txt";
286+
287+
Files.createDirectories(Paths.get(randomOutputDir));
288+
Files.write(Paths.get(pathToFirstFile), fixedContentBytes);
289+
290+
291+
initRollingFileAppender(rfa1, null);
292+
sizeThreshold = 300;
293+
initPolicies(rfa1, tbrp1, randomOutputDir + testId + "-%d-%i.txt", sizeThreshold,
294+
currentTime, 0);
295+
296+
StatusPrinter.print(context);
297+
298+
assertEquals(fixedContentBytes.length, tbrp1.getLengthCounter().getLength());
299+
}
300+
246301
// @Test
247302
// public void testHistoryAsFileCount() throws IOException {
248303
// String testId = "testHistoryAsFileCount";

logback-core/src/test/java/ch/qos/logback/core/util/CachingDateFotmatterTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
public class CachingDateFotmatterTest {
1515

16-
final static String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm";
16+
private final static String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm";
1717

1818
SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
1919
TimeZone perthTZ = TimeZone.getTimeZone("Australia/Perth");

0 commit comments

Comments
 (0)