Skip to content

Commit

Permalink
added more javadocs
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Apr 7, 2024
1 parent 596fbea commit 81ab4ac
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,46 @@
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.filter.ThresholdFilter;
import ch.qos.logback.classic.spi.Configurator;
import ch.qos.logback.classic.tyler.TylerConfiguratorBase;
import ch.qos.logback.classic.util.LogbackMDCAdapter;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
import ch.qos.logback.core.rolling.helper.FileNamePattern;
import ch.qos.logback.core.util.StatusPrinter2;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import org.slf4j.spi.MDCAdapter;

public class LOGBACK_1393_Test extends TylerConfiguratorBase {

LoggerContext loggerCoontext = new LoggerContext();
LoggerContext loggerContext = new LoggerContext();
LogbackMDCAdapter mdcAdapter = new LogbackMDCAdapter();
StatusPrinter2 statusPrinter2 = new StatusPrinter2();


@BeforeEach
public void setup() {
loggerContext.setMDCAdapter(mdcAdapter);
}
public void configure(LoggerContext loggerCoontext) {
setContext(loggerCoontext);
propertyModelHandlerHelper.handlePropertyModel(this, "LOG_HOME", "log", "", "", "");
addOnConsoleStatusListener();
Appender appenderFILE = setupAppenderFILE();
Logger logger_com_mindsphere_china_poc_connectivity = setupLogger("com.mindsphere.china.poc.connectivity",
"DEBUG", Boolean.FALSE);
logger_com_mindsphere_china_poc_connectivity.addAppender(appenderFILE);
Logger logger_ROOT = setupLogger("ROOT", "ERROR", null);
Logger logger_ROOT = setupLogger("ROOT", "DEBUG", null);
logger_ROOT.addAppender(appenderFILE);
}

Appender setupAppenderFILE() {
RollingFileAppender appenderFILE = new RollingFileAppender();
appenderFILE.setContext(loggerCoontext);
appenderFILE.setContext(loggerContext);
appenderFILE.setName("FILE");

appenderFILE.setImmediateFlush(true);
// Configure component of type TimeBasedRollingPolicy
TimeBasedRollingPolicy timeBasedRollingPolicy = new TimeBasedRollingPolicy();
timeBasedRollingPolicy.setContext(loggerCoontext);
timeBasedRollingPolicy.setFileNamePattern(subst("${LOG_HOME}/connectivi.log.%d{yyyy-MM-dd}.log"));
timeBasedRollingPolicy.setContext(loggerContext);
timeBasedRollingPolicy.setFileNamePattern(subst("/tmp/log/lb1393.%d{yyyy-MM-dd}.log"));
timeBasedRollingPolicy.setMaxHistory(6);
timeBasedRollingPolicy.setParent(appenderFILE);
timeBasedRollingPolicy.start();
Expand All @@ -63,24 +68,24 @@ Appender setupAppenderFILE() {
PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
patternLayoutEncoder.setContext(context);
patternLayoutEncoder.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n");
patternLayoutEncoder.setImmediateFlush(true);

patternLayoutEncoder.setParent(appenderFILE);
patternLayoutEncoder.start();
// Inject component of type PatternLayoutEncoder into parent
appenderFILE.setEncoder(patternLayoutEncoder);

// Configure component of type SizeBasedTriggeringPolicy
SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy();
sizeBasedTriggeringPolicy.setContext(loggerCoontext);
sizeBasedTriggeringPolicy.setMaxFileSize(ch.qos.logback.core.util.FileSize.valueOf("10MB"));
sizeBasedTriggeringPolicy.setContext(loggerContext);
sizeBasedTriggeringPolicy.setMaxFileSize(ch.qos.logback.core.util.FileSize.valueOf("1000"));
// ===========no parent setter
sizeBasedTriggeringPolicy.start();
// Inject component of type SizeBasedTriggeringPolicy into parent
appenderFILE.setTriggeringPolicy(sizeBasedTriggeringPolicy);

// Configure component of type ThresholdFilter
ThresholdFilter thresholdFilter = new ThresholdFilter();
thresholdFilter.setContext(loggerCoontext);
thresholdFilter.setContext(loggerContext);
thresholdFilter.setLevel("TRACE");
// ===========no parent setter
thresholdFilter.start();
Expand All @@ -93,10 +98,21 @@ Appender setupAppenderFILE() {

@Test
void smoke() {
configure(loggerContext);
Logger logger = loggerContext.getLogger(this.getClass());
for(int i = 0; i < 100; i++) {
logger.atInfo().addKeyValue("i", i).log("hello world xxasdaasfasf asdfasfdsfd");
delay(100);
}
//statusPrinter2.print(loggerContext);
}

FileNamePattern fnp = new FileNamePattern("/log/connectivi.log.%d{yyyy-MM-dd}.log", context);
Instant now = Instant.now();
fnp.toRegexForFixedDate(now);
private void delay(int i) {
try {
Thread.sleep(i);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import ch.qos.logback.core.rolling.helper.TimeBasedArchiveRemover;

/**
* Default implementation of {@link TimeBasedFileNamingAndTriggeringPolicy}
* interface extending {@link TimeBasedFileNamingAndTriggeringPolicyBase}. This class is intended to be nested
* within a {@link TimeBasedRollingPolicy}.
*
*
* @author Ceki G&uuml;lc&uuml;
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,22 @@
import ch.qos.logback.core.util.InvocationGate;
import ch.qos.logback.core.util.SimpleInvocationGate;

/**
* This class implement {@link TimeBasedFileNamingAndTriggeringPolicy}
* interface extending {@link TimeBasedFileNamingAndTriggeringPolicyBase}. This class is intended to be nested
* within a {@link SizeAndTimeBasedFNATP} instance. However, it can also be instantiated directly for testing purposes.
*
* @author Ceki G&uuml;lc&uuml;
*
* @param <E>
*/
@NoAutoStart
public class SizeAndTimeBasedFNATP<E> extends TimeBasedFileNamingAndTriggeringPolicyBase<E> {

enum Usage {
EMBEDDED, DIRECT
}

;

volatile int currentPeriodsCounter = 0;
FileSize maxFileSize;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
/**
* This interface lists the set of methods that need to be implemented by
* triggering policies which are nested within a {@link TimeBasedRollingPolicy}.
*
*
* <p>This interface should be considered as an extension of {@link TimeBasedRollingPolicy} with file naming
* support methods.
* </p>
*
* @See also {@link TimeBasedFileNamingAndTriggeringPolicyBase}
*
* @author Ceki G&uuml;lc&uuml;
*
* @param <E>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
import ch.qos.logback.core.rolling.helper.RollingCalendar;
import ch.qos.logback.core.spi.ContextAwareBase;

/**
* Base implementation of {@link TimeBasedFileNamingAndTriggeringPolicy}.
*
* <p>See also derived classes {@link DefaultTimeBasedFileNamingAndTriggeringPolicy} and
* {@link SizeAndTimeBasedFNATP}.
*
* </p>
*
* @param <E>
*/
abstract public class TimeBasedFileNamingAndTriggeringPolicyBase<E> extends ContextAwareBase
implements TimeBasedFileNamingAndTriggeringPolicy<E> {

Expand Down

0 comments on commit 81ab4ac

Please sign in to comment.