Skip to content

Commit

Permalink
fix: mute JCS logging (again) (#6153)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylong authored Nov 27, 2023
1 parent a620c12 commit ed3912c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 70 deletions.
35 changes: 4 additions & 31 deletions ant/src/main/java/org/owasp/dependencycheck/taskdefs/Purge.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.owasp.dependencycheck.taskdefs;

import io.github.jeremylong.jcs3.slf4j.Slf4jAdapter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -158,39 +159,11 @@ public final void execute() throws BuildException {
}

/**
* Hacky method of muting the noisy logging from JCS. Implemented using a
* solution from SO: https://stackoverflow.com/a/50723801
* Hacky method of muting the noisy logging from JCS.
*/
private void muteJCS() {
if (System.getProperty("jcs.logSystem") == null) {
System.setProperty("jcs.logSystem", "slf4j");
System.setProperty("jcs.logSystem.mute", "true");
}

final String[] noisyLoggers = {
"org.apache.commons.jcs3.auxiliary.disk.AbstractDiskCache",
"org.apache.commons.jcs3.engine.memory.AbstractMemoryCache",
"org.apache.commons.jcs3.engine.control.CompositeCache",
"org.apache.commons.jcs3.auxiliary.disk.indexed.IndexedDiskCache",
"org.apache.commons.jcs3.engine.control.CompositeCache",
"org.apache.commons.jcs3.engine.memory.AbstractMemoryCache",
"org.apache.commons.jcs3.engine.control.event.ElementEventQueue",
"org.apache.commons.jcs3.engine.memory.AbstractDoubleLinkedListMemoryCache",
"org.apache.commons.jcs3.auxiliary.AuxiliaryCacheConfigurator",
"org.apache.commons.jcs3.engine.control.CompositeCacheManager",
"org.apache.commons.jcs3.utils.threadpool.ThreadPoolManager",
"org.apache.commons.jcs3.engine.control.CompositeCacheConfigurator"};
for (String loggerName : noisyLoggers) {
try {
final Logger l = LoggerFactory.getLogger(loggerName);
final Field f = l.getClass().getSuperclass().getDeclaredField("currentLogLevel");
f.setAccessible(true);
f.set(l, LocationAwareLogger.ERROR_INT);
} catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException | SecurityException e) {
LoggerFactory.getLogger(Purge.class)
.debug("Failed to reset the log level of " + loggerName + ", it will continue being noisy.");
}
}
System.setProperty("jcs.logSystem", "slf4j");
Slf4jAdapter.muteLogging(true);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions cli/src/main/java/org/owasp/dependencycheck/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import io.github.jeremylong.jcs3.slf4j.Slf4jAdapter;
import java.util.TreeSet;
import org.owasp.dependencycheck.utils.SeverityUtil;

Expand Down Expand Up @@ -79,9 +80,9 @@ public class App {
*/
@SuppressWarnings("squid:S4823")
public static void main(String[] args) {
if (System.getProperty("jcs.logSystem") == null) {
System.setProperty("jcs.logSystem", "slf4j");
System.setProperty("jcs.logSystem.mute", Boolean.toString(!LOGGER.isDebugEnabled()));
System.setProperty("jcs.logSystem", "slf4j");
if (!LOGGER.isDebugEnabled()) {
Slf4jAdapter.muteLogging(true);
}
final int exitCode;
final App app = new App();
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/org/owasp/dependencycheck/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.io.FileUtils;
import org.apache.commons.jcs3.JCS;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.owasp.dependencycheck.analyzer.AnalysisPhase;
Expand Down Expand Up @@ -222,6 +223,7 @@ public void close() {
} else {
System.clearProperty("javax.xml.accessExternalSchema");
}
JCS.shutdown();
}

/**
Expand Down
7 changes: 3 additions & 4 deletions core/src/test/java/org/owasp/dependencycheck/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.owasp.dependencycheck;

import io.github.jeremylong.jcs3.slf4j.Slf4jAdapter;
import java.io.File;
import java.io.InputStream;
import java.net.URISyntaxException;
Expand All @@ -41,10 +42,8 @@ public abstract class BaseTest {
*/
@Before
public void setUp() throws Exception {
if (System.getProperty("jcs.logSystem") == null) {
System.setProperty("jcs.logSystem", "slf4j");
System.setProperty("jcs.logSystem.mute", "true");
}
System.setProperty("jcs.logSystem", "slf4j");
Slf4jAdapter.muteLogging(true);
settings = new Settings();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL.StandardTypes;
import com.github.packageurl.PackageURL;
import io.github.jeremylong.jcs3.slf4j.Slf4jAdapter;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
Expand Down Expand Up @@ -2488,39 +2489,12 @@ private String[] determineSuppressions() {
}

/**
* Hacky method of muting the noisy logging from JCS. Implemented using a
* solution from SO: https://stackoverflow.com/a/50723801
* Hacky method of muting the noisy logging from JCS
*/
private void muteJCS() {
if (System.getProperty("jcs.logSystem") == null) {
System.setProperty("jcs.logSystem", "slf4j");

System.setProperty("jcs.logSystem.mute",Boolean.toString(!getLog().isDebugEnabled()));
}

final String[] noisyLoggers = {
"org.apache.commons.jcs3.auxiliary.disk.AbstractDiskCache",
"org.apache.commons.jcs3.engine.memory.AbstractMemoryCache",
"org.apache.commons.jcs3.engine.control.CompositeCache",
"org.apache.commons.jcs3.auxiliary.disk.indexed.IndexedDiskCache",
"org.apache.commons.jcs3.engine.control.CompositeCache",
"org.apache.commons.jcs3.engine.memory.AbstractMemoryCache",
"org.apache.commons.jcs3.engine.control.event.ElementEventQueue",
"org.apache.commons.jcs3.engine.memory.AbstractDoubleLinkedListMemoryCache",
"org.apache.commons.jcs3.auxiliary.AuxiliaryCacheConfigurator",
"org.apache.commons.jcs3.engine.control.CompositeCacheManager",
"org.apache.commons.jcs3.utils.threadpool.ThreadPoolManager",
"org.apache.commons.jcs3.engine.control.CompositeCacheConfigurator"};
for (String loggerName : noisyLoggers) {
try {
//This is actually a MavenSimpleLogger, but due to various classloader issues, can't work with the directly.
final Logger l = LoggerFactory.getLogger(loggerName);
final Field f = l.getClass().getSuperclass().getDeclaredField("currentLogLevel");
f.setAccessible(true);
f.set(l, LocationAwareLogger.ERROR_INT);
} catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException | SecurityException e) {
getLog().debug("Failed to reset the log level of " + loggerName + ", it will continue being noisy.");
}
System.setProperty("jcs.logSystem", "slf4j");
if (!getLog().isDebugEnabled()) {
Slf4jAdapter.muteLogging(true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ Copyright (c) 2012 - Jeremy Long
<dependency>
<groupId>io.github.jeremylong</groupId>
<artifactId>jcs3-slf4j</artifactId>
<version>1.0.3</version>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
Expand Down

0 comments on commit ed3912c

Please sign in to comment.