Skip to content

Port log4j-mongodb4 changes from 2.x #2164

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

Merged
merged 18 commits into from
Jan 5, 2024
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 @@ -42,21 +42,25 @@ public void set(final String field, final Object value) {

@Override
public void set(final String field, final NoSqlObject<Map<String, Object>> value) {
this.map.put(field, value.unwrap());
this.map.put(field, value != null ? value.unwrap() : null);
}

@Override
public void set(final String field, final Object[] values) {
this.map.put(field, Arrays.asList(values));
this.map.put(field, values != null ? Arrays.asList(values) : null);
}

@Override
public void set(final String field, final NoSqlObject<Map<String, Object>>[] values) {
final List<Map<String, Object>> list = new ArrayList<>(values.length);
for (final NoSqlObject<Map<String, Object>> value : values) {
list.add(value.unwrap());
if (values == null) {
this.map.put(field, null);
} else {
final List<Map<String, Object>> list = new ArrayList<>(values.length);
for (final NoSqlObject<Map<String, Object>> value : values) {
list.add(value.unwrap());
}
this.map.put(field, list);
}
this.map.put(field, list);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
*
*/
public class PatternFormatter {

/**
* The empty array.
*/
public static final PatternFormatter[] EMPTY_ARRAY = {};

private final LogEventPatternConverter converter;
private final FormattingInfo field;
private final boolean skipFormattingInfo;
Expand Down
5 changes: 5 additions & 0 deletions log4j-jul/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,9 @@ public class ApiLogger extends Logger {
super(logger.getName(), null);
final Level javaLevel = LevelTranslator.toJavaLevel(logger.getLevel());
// "java.util.logging.LoggingPermission" "control"
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
ApiLogger.super.setLevel(javaLevel);
return null;
}
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
ApiLogger.super.setLevel(javaLevel);
return null;
});
this.logger = new WrappedLogger(logger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.logging.log4j.jul;

// note: NO import of Logger, Level, LogManager to prevent conflicts JUL/log4j

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Enumeration;
import java.util.HashSet;
Expand Down Expand Up @@ -93,7 +92,7 @@ public class Log4jBridgeHandler extends java.util.logging.Handler implements Con

private boolean doDebugOutput = false;
private String julSuffixToAppend = null;
private LoggerContext context;
private volatile boolean installAsLevelPropagator = false;

/**
* Adds a new Log4jBridgeHandler instance to JUL's root logger.
Expand Down Expand Up @@ -153,14 +152,7 @@ protected void init(boolean debugOutput, String suffixToAppend, boolean propagat
}
this.julSuffixToAppend = suffixToAppend;

this.context = LoggerContext.getContext(false);

if (propagateLevels) {
context.addConfigurationStartedListener(this);
propagateLogLevels(context.getConfiguration());
// note: java.util.logging.LogManager.addPropertyChangeListener() could also
// be set here, but a call of JUL.readConfiguration() will be done on purpose
}
this.installAsLevelPropagator = propagateLevels;

SLOGGER.debug(
"Log4jBridgeHandler init. with: suffix='{}', lvlProp={}, instance={}",
Expand All @@ -173,10 +165,7 @@ protected void init(boolean debugOutput, String suffixToAppend, boolean propagat
public void close() {
// cleanup and remove listener and JUL logger references
julLoggerRefs = null;
if (context != null) {
context.removeConfigurationStartedListener(this);
context = null;
}
LoggerContext.getContext(false).removeConfigurationStartedListener(this);
if (doDebugOutput) {
System.out.println("sysout: Log4jBridgeHandler close(): " + this);
}
Expand All @@ -188,6 +177,23 @@ public void publish(final LogRecord record) {
return;
}

// Only execute synchronized code if we really have to
if (this.installAsLevelPropagator) {
synchronized (this) {
// Check again to make sure we still have to propagate the levels at this point
if (this.installAsLevelPropagator) {
// no need to close the AutoCloseable ctx here
@SuppressWarnings("resource")
final LoggerContext context = LoggerContext.getContext(false);
context.addConfigurationStartedListener(this);
propagateLogLevels(context.getConfiguration());
// note: java.util.logging.LogManager.addPropertyChangeListener() could also
// be set here, but a call of JUL.readConfiguration() will be done on purpose
this.installAsLevelPropagator = false;
}
}
}

final org.apache.logging.log4j.Logger log4jLogger = getLog4jLogger(record);
final String msg = julFormatter.formatMessage(record); // use JUL's implementation to get real msg
/* log4j allows nulls:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public class LogManager extends java.util.logging.LogManager {
private final ThreadLocal<Set<String>> recursive = ThreadLocal.withInitial(HashSet::new);

public LogManager() {
super();
AbstractLoggerAdapter adapter = null;
final String overrideAdaptorClassName =
PropertiesUtil.getProperties().getStringProperty(JulPropertyKey.LOGGER_ADAPTER);
Expand Down Expand Up @@ -99,10 +98,9 @@ public Logger getLogger(final String name) {
} finally {
activeRequests.remove(name);
}
} else {
LOGGER.warn("Recursive call to getLogger for {} ignored.", name);
return new NoOpLogger(name);
}
LOGGER.warn("Recursive call to getLogger for {} ignored.", name);
return new NoOpLogger(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.MementoLogEvent;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.jul.ApiLogger;
import org.apache.logging.log4j.jul.LevelTranslator;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.Assert.assertThat;

import java.util.logging.Logger;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.jul.ApiLoggerAdapter;
import org.apache.logging.log4j.jul.JulPropertyKey;
import org.apache.logging.log4j.jul.LogManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.async.AsyncLoggerContextSelector;
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
import org.apache.logging.log4j.core.test.categories.AsyncLoggers;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

// @Category(AsyncLoggers.class)
@Ignore("https://issues.apache.org/jira/browse/LOG4J2-3523")
@Category(AsyncLoggers.class)
public class AsyncLoggerThreadsTest {

@BeforeClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.jul.LogManager;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,25 @@
*/
package org.apache.logging.log4j.jul.test;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

import java.net.URI;
import java.util.List;
import java.util.logging.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.apache.logging.log4j.jul.LogManager;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;

public class CallerInformationTest {

// config from log4j-core test-jar
private static final String CONFIG = "log4j2-calling-class.xml";

private LoggerContext ctx;
private ListAppender app;
@Rule
public final LoggerContextRule ctx = new LoggerContextRule(CONFIG);

@BeforeClass
public static void setUpClass() {
Expand All @@ -47,27 +46,9 @@ public static void tearDownClass() {
System.clearProperty("java.util.logging.manager");
}

@Before
public void beforeEach() throws Exception {
final URI uri = this.getClass().getClassLoader().getResource(CONFIG).toURI();
ctx = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(null, false, uri);
assertNotNull("No LoggerContext", ctx);
}

@After
public void afterEach() throws Exception {
if (ctx != null) {
ctx.stop();
ctx = null;
app = null;
}
}

@Test
public void testClassLogger() throws Exception {
app = ctx.getConfiguration().getAppender("Class");
assertNotNull("No ListAppender", app);
app.clear();
final ListAppender app = ctx.getListAppender("Class").clear();
final Logger logger = Logger.getLogger("ClassLogger");
logger.info("Ignored message contents.");
logger.warning("Verifying the caller class is still correct.");
Expand All @@ -81,9 +62,7 @@ public void testClassLogger() throws Exception {

@Test
public void testMethodLogger() throws Exception {
app = ctx.getConfiguration().getAppender("Method");
assertNotNull("No ListAppender", app);
app.clear();
final ListAppender app = ctx.getListAppender("Method").clear();
final Logger logger = Logger.getLogger("MethodLogger");
logger.info("More messages.");
logger.warning("CATASTROPHE INCOMING!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.jul.LogManager;
import org.apache.logging.log4j.util.Strings;
import org.junit.After;
Expand Down
Loading