Skip to content

Commit

Permalink
fix LOGBACK-1767
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 5, 2024
1 parent 7587630 commit 596fbea
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2024, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/

package ch.qos.logback.classic.issue.lbcore258;

import ch.qos.logback.classic.Logger;
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.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 org.junit.jupiter.api.Test;

import java.time.Instant;

public class LOGBACK_1393_Test extends TylerConfiguratorBase {

LoggerContext loggerCoontext = new LoggerContext();

public void configure(LoggerContext loggerCoontext) {
setContext(loggerCoontext);
propertyModelHandlerHelper.handlePropertyModel(this, "LOG_HOME", "log", "", "", "");
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_ROOT.addAppender(appenderFILE);
}

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

// 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.setMaxHistory(6);
timeBasedRollingPolicy.setParent(appenderFILE);
timeBasedRollingPolicy.start();
// Inject component of type TimeBasedRollingPolicy into parent
appenderFILE.setRollingPolicy(timeBasedRollingPolicy);

// Configure component of type PatternLayoutEncoder
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"));
// ===========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.setLevel("TRACE");
// ===========no parent setter
thresholdFilter.start();
// Inject component of type ThresholdFilter into parent
appenderFILE.addFilter(thresholdFilter);

appenderFILE.start();
return appenderFILE;
}

@Test
void smoke() {

FileNamePattern fnp = new FileNamePattern("/log/connectivi.log.%d{yyyy-MM-dd}.log", context);
Instant now = Instant.now();
fnp.toRegexForFixedDate(now);
}

}


Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
* Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2015, QOS.ch. All rights
* reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
* This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License
* v1.0 as published by the Eclipse Foundation
*
* or (per the licensee's choosing)
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
* under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
*/
package ch.qos.logback.core.rolling;

Expand All @@ -32,7 +30,7 @@
/**
* <code>RollingFileAppender</code> extends {@link FileAppender} to back up the
* log files depending on {@link RollingPolicy} and {@link TriggeringPolicy}.
*
*
* <p>
* For more information about this appender, please refer to the online manual
* at http://logback.qos.ch/manual/appenders.html#RollingFileAppender
Expand All @@ -51,6 +49,7 @@ public class RollingFileAppender<E> extends FileAppender<E> {
static private String RFA_NO_RP_URL = CODES_URL + "#rfa_no_rp";
static private String COLLISION_URL = CODES_URL + "#rfa_collision";
static private String RFA_LATE_FILE_URL = CODES_URL + "#rfa_file_after";
static private String RFA_RESET_RP_OR_TP = CODES_URL + "#rfa_reset_rp_or_tp";

public void start() {
if (triggeringPolicy == null) {
Expand Down Expand Up @@ -132,8 +131,8 @@ private boolean checkForCollisionsInPreviousRollingFileAppenders() {
private boolean innerCheckForFileNamePatternCollisionInPreviousRFA(FileNamePattern fileNamePattern) {
boolean collisionsDetected = false;
@SuppressWarnings("unchecked")
Map<String, FileNamePattern> map = (Map<String, FileNamePattern>) context
.getObject(CoreConstants.RFA_FILENAME_PATTERN_COLLISION_MAP);
Map<String, FileNamePattern> map = (Map<String, FileNamePattern>) context.getObject(
CoreConstants.RFA_FILENAME_PATTERN_COLLISION_MAP);
if (map == null) {
return collisionsDetected;
}
Expand All @@ -151,10 +150,10 @@ private boolean innerCheckForFileNamePatternCollisionInPreviousRFA(FileNamePatte

@Override
public void stop() {
if(!isStarted()) {
if (!isStarted()) {
return;
}
super.stop();
super.stop();

if (rollingPolicy != null)
rollingPolicy.stop();
Expand Down Expand Up @@ -225,8 +224,6 @@ private void attemptRollover() {
}
}



/**
* This method differentiates RollingFileAppender from its super class.
*/
Expand All @@ -249,7 +246,6 @@ protected void subAppend(E event) {
triggeringPolicyLock.unlock();
}


super.subAppend(event);
}

Expand All @@ -270,6 +266,12 @@ public TriggeringPolicy<E> getTriggeringPolicy() {
*/
@SuppressWarnings("unchecked")
public void setRollingPolicy(RollingPolicy policy) {
if (rollingPolicy instanceof TriggeringPolicy) {
String className = rollingPolicy.getClass().getSimpleName();
addWarn("A rolling policy of type " + className + " was already set.");
addWarn("Note that " + className + " doubles as a TriggeringPolicy");
addWarn("See also "+RFA_RESET_RP_OR_TP);
}
rollingPolicy = policy;
if (rollingPolicy instanceof TriggeringPolicy) {
triggeringPolicy = (TriggeringPolicy<E>) policy;
Expand All @@ -278,6 +280,12 @@ public void setRollingPolicy(RollingPolicy policy) {
}

public void setTriggeringPolicy(TriggeringPolicy<E> policy) {
if (triggeringPolicy instanceof RollingPolicy) {
String className = triggeringPolicy.getClass().getSimpleName();
addWarn("A triggering policy of type " + className + " was already set.");
addWarn("Note that " + className + " doubles as a RollingPolicy");
addWarn("See also "+RFA_RESET_RP_OR_TP);
}
triggeringPolicy = policy;
if (policy instanceof RollingPolicy) {
rollingPolicy = (RollingPolicy) policy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class TimeBasedFileNamingAndTriggeringPolicyBaseTest {
RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
TimeBasedRollingPolicy<Object> tbrp = new TimeBasedRollingPolicy<Object>();
DefaultTimeBasedFileNamingAndTriggeringPolicy<Object> timeBasedFNATP = new DefaultTimeBasedFileNamingAndTriggeringPolicy<Object>();

StatusChecker statusChecker = new StatusChecker(context);
@BeforeEach
public void setUp() {
rfa.setContext(context);
Expand All @@ -50,6 +50,12 @@ public void setUp() {
timeBasedFNATP.setTimeBasedRollingPolicy(tbrp);
}

@Test
public void doublePolicySet() {
rfa.setTriggeringPolicy(new SizeBasedTriggeringPolicy<>());
statusChecker.assertContainsMatch(Status.WARN, "A triggering policy of type " );
}

@Test
public void singleDate() {
// Tuesday December 20th 17:59:01 CET 2011
Expand Down

0 comments on commit 596fbea

Please sign in to comment.