Skip to content

Commit

Permalink
Workaround lack of dynamic level change in SimpleLogger. Only sm0l cr…
Browse files Browse the repository at this point in the history
…imes committed. (#25)
  • Loading branch information
kittylyst authored Feb 15, 2024
1 parent 76410d5 commit 588a23e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<apache.httpclient.version>4.5.14</apache.httpclient.version>
<apache.httpcore.version>4.4.16</apache.httpcore.version>
<apache.httpmime.version>4.5.14</apache.httpmime.version>
<org.slf4j.version>2.0.7</org.slf4j.version>
<org.slf4j.version>2.0.12</org.slf4j.version>

<!-- Test libraries versions -->
<junit.version>5.9.2</junit.version>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/redhat/insights/agent/AgentBasicReport.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights.agent;

import com.redhat.insights.config.InsightsConfiguration;
Expand All @@ -10,7 +10,7 @@
import java.util.Map;

public class AgentBasicReport extends AbstractTopLevelReportBase {
private static final InsightsLogger logger = new SLF4JLogger(AgentBasicReport.class);
private static final InsightsLogger logger = AgentLogger.getLogger();

private AgentBasicReport(
InsightsConfiguration config, Map<String, InsightsSubreport> subReports) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights.agent;

import com.redhat.insights.logging.InsightsLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.simple.SimpleLogger;

public class SLF4JLogger implements InsightsLogger {
public class AgentLogger implements InsightsLogger {

private final Logger delegate;
private static AgentLogger instance;

public SLF4JLogger(Class<?> clazz) {
private Logger delegate;

private AgentLogger(Class<?> clazz) {
this.delegate = LoggerFactory.getLogger(clazz);
}

public static synchronized AgentLogger getLogger() {
if (instance == null) {
instance = new AgentLogger(AgentMain.class);
}
return instance;
}

static class DebugSimpleLogger extends SimpleLogger {
public DebugSimpleLogger() {
super("com.redhat.insights.agent.AgentLogger");
currentLogLevel = SimpleLogger.LOG_LEVEL_DEBUG;
}
}

public void setDebugDelegate() {
this.delegate = new DebugSimpleLogger();
}

@Override
public void debug(String message) {
delegate.debug(message);
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/redhat/insights/agent/AgentMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.redhat.insights.http.InsightsFileWritingClient;
import com.redhat.insights.http.InsightsHttpClient;
import com.redhat.insights.jars.JarInfo;
import com.redhat.insights.logging.InsightsLogger;
import com.redhat.insights.reports.InsightsReport;
import com.redhat.insights.tls.PEMSupport;
import java.lang.instrument.Instrumentation;
Expand All @@ -22,7 +21,7 @@

/** Main class for the agent. */
public final class AgentMain {
private static final InsightsLogger logger = new SLF4JLogger(AgentMain.class);
private static final AgentLogger logger = AgentLogger.getLogger();

private final AgentConfiguration configuration;
private final BlockingQueue<JarInfo> waitingJars;
Expand Down Expand Up @@ -72,6 +71,10 @@ public static void startAgent(String agentArgs, Instrumentation instrumentation)
"Config indicates Red Hat Insights agent is not to be run. Not starting agent capability.");
return;
}
if (config.isDebug()) {
logger.setDebugDelegate();
logger.debug("Running in debug mode");
}

final BlockingQueue<JarInfo> jarsToSend = new LinkedBlockingQueue<>();
try {
Expand Down Expand Up @@ -163,7 +166,7 @@ static Optional<AgentConfiguration> parseArgs(String agentArgs) {

private static boolean shouldLookForCerts(AgentConfiguration config) {
boolean hasToken = config.getMaybeAuthToken().isPresent();
return !hasToken && !config.isDebug() && !config.isFileOnly() && !config.isOptingOut();
return !hasToken && !config.isFileOnly() && !config.isOptingOut();
}

private void start() {
Expand All @@ -181,7 +184,7 @@ private void start() {

private Supplier<InsightsHttpClient> getInsightsClientSupplier() {
final Supplier<InsightsHttpClient> out;
if (configuration.isDebug() || configuration.isFileOnly()) {
if (configuration.isFileOnly()) {
out = () -> new InsightsFileWritingClient(logger, configuration);
} else if (configuration.isOCP()) {
out = () -> new InsightsAgentHttpClient(configuration);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/redhat/insights/agent/AgentSubreport.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights.agent;

import static java.lang.System.getProperty;
Expand All @@ -19,7 +19,7 @@
import java.util.function.Function;

public class AgentSubreport implements InsightsSubreport {
private static final InsightsLogger logger = new SLF4JLogger(AgentSubreport.class);
private static final InsightsLogger logger = AgentLogger.getLogger();

private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class[0];
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/redhat/insights/agent/ClassNoticer.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights.agent;

import com.redhat.insights.jars.JarAnalyzer;
Expand All @@ -15,7 +15,7 @@
import java.util.concurrent.BlockingQueue;

public final class ClassNoticer implements ClassFileTransformer {
private static final InsightsLogger logger = new SLF4JLogger(ClassNoticer.class);
private static final InsightsLogger logger = AgentLogger.getLogger();

private final BlockingQueue<JarInfo> jarsToSend;
private final JarAnalyzer analyzer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.apache.http.util.EntityUtils;

public final class InsightsAgentHttpClient implements InsightsHttpClient {
private static final InsightsLogger logger = new SLF4JLogger(InsightsAgentHttpClient.class);
private static final InsightsLogger logger = AgentLogger.getLogger();
private static final ContentType GENERAL_CONTENT_TYPE = ContentType.create(GENERAL_MIME_TYPE);
private final Supplier<SSLContext> sslContextSupplier;
private final InsightsConfiguration configuration;
Expand Down

0 comments on commit 588a23e

Please sign in to comment.