Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RollingFileAppender only when log_limit_in_kbytes greater than 0 #1228

Merged
merged 2 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private AbstractOutputStreamAppender<? extends FileManager> buildRollingFileAppe
return buildDailyRollingAppender();
}

if ((fileCount > 1) || (logLimitBytes > 0)) {
if (logLimitBytes > 0) {
return initializeRollingFileAppender()
.withStrategy(DefaultRolloverStrategy.newBuilder()
.withMin(String.valueOf(MIN_FILE_COUNT))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.newrelic.agent.logging;

import org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender;
import org.apache.logging.log4j.core.appender.FileAppender;
import org.apache.logging.log4j.core.appender.FileManager;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
import org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy;
import org.apache.logging.log4j.core.appender.rolling.CronTriggeringPolicy;
import org.apache.logging.log4j.core.appender.rolling.NoOpTriggeringPolicy;
import org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;

public class FileAppenderFactoryTest {

private static final String FILENAME = "newrelic_agent.log";
private static final long BYTE_LIMIT = 1024L;

@Test
public void build_withLogDailyTrueAndByteLimitGreaterThanZero_createsDailyRollingFileAppender() {
FileAppenderFactory factory = new FileAppenderFactory(1, BYTE_LIMIT, FILENAME + "0", true);
AbstractOutputStreamAppender<? extends FileManager> fileAppender = factory.build();
RollingFileAppender rollingFileAppender = (RollingFileAppender) fileAppender;

Assert.assertEquals(RollingFileAppender.class, fileAppender.getClass());
Assert.assertEquals(CompositeTriggeringPolicy.class, rollingFileAppender.getTriggeringPolicy().getClass());
Assert.assertEquals(FILENAME + "0", rollingFileAppender.getFileName());

CompositeTriggeringPolicy policy = rollingFileAppender.getTriggeringPolicy();
Assert.assertTrue((policy.getTriggeringPolicies()[0] instanceof CronTriggeringPolicy) ||
(policy.getTriggeringPolicies()[0] instanceof SizeBasedTriggeringPolicy));
Assert.assertTrue((policy.getTriggeringPolicies()[1] instanceof CronTriggeringPolicy) ||
(policy.getTriggeringPolicies()[1] instanceof SizeBasedTriggeringPolicy));
}

@Test
public void build_withLogDailyTrueAndByteLimitEqualZero_createsDailyRollingFileAppender() {
FileAppenderFactory factory = new FileAppenderFactory(1, 0, FILENAME + "1", true);
AbstractOutputStreamAppender<? extends FileManager> fileAppender = factory.build();
RollingFileAppender rollingFileAppender = (RollingFileAppender) fileAppender;

Assert.assertEquals(RollingFileAppender.class, fileAppender.getClass());
Assert.assertEquals(CompositeTriggeringPolicy.class, rollingFileAppender.getTriggeringPolicy().getClass());
Assert.assertEquals(FILENAME + "1", rollingFileAppender.getFileName());

CompositeTriggeringPolicy policy = rollingFileAppender.getTriggeringPolicy();
Assert.assertTrue((policy.getTriggeringPolicies()[0] instanceof CronTriggeringPolicy) ||
(policy.getTriggeringPolicies()[0] instanceof NoOpTriggeringPolicy));
Assert.assertTrue((policy.getTriggeringPolicies()[1] instanceof CronTriggeringPolicy) ||
(policy.getTriggeringPolicies()[1] instanceof NoOpTriggeringPolicy));
}

@Test
public void build_withLogDailyFalseAndByteLimitGreaterThanZero_createsSizeBasedRollingFileAppender() {
FileAppenderFactory factory = new FileAppenderFactory(1, BYTE_LIMIT, FILENAME + "2", false);
AbstractOutputStreamAppender<? extends FileManager> fileAppender = factory.build();
RollingFileAppender rollingFileAppender = (RollingFileAppender) fileAppender;

Assert.assertEquals(RollingFileAppender.class, fileAppender.getClass());
Assert.assertEquals(FILENAME + "2", rollingFileAppender.getFileName());

SizeBasedTriggeringPolicy policy = rollingFileAppender.getTriggeringPolicy();
Assert.assertEquals(SizeBasedTriggeringPolicy.class, policy.getClass());
Assert.assertEquals(BYTE_LIMIT, policy.getMaxFileSize());
}

@Test
public void build_withLogDailyFalseAndByteLimitEqualZero_createsSizeBasedRollingFileAppender() {
FileAppenderFactory factory = new FileAppenderFactory(1, 0, FILENAME + "3", false);
AbstractOutputStreamAppender<? extends FileManager> fileAppender = factory.build();
FileAppender rollingFileAppender = (FileAppender) fileAppender;

Assert.assertEquals(FileAppender.class, fileAppender.getClass());
Assert.assertEquals(FILENAME + "3", rollingFileAppender.getFileName());
}

@AfterClass
public static void afterTests() {
for (int idx = 0; idx < 4; idx++) {
new File(FILENAME + Integer.toString(idx)).delete();
}
}
}