Skip to content

Commit

Permalink
Encapsulate class in log4j-jul
Browse files Browse the repository at this point in the history
  • Loading branch information
ppkarwasz committed Sep 13, 2024
1 parent d6df8ed commit b416ca5
Show file tree
Hide file tree
Showing 21 changed files with 220 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.jul.LevelTranslator;
import org.apache.logging.log4j.jul.Log4jBridgeHandler;
import org.apache.logging.log4j.jul.spi.LevelChangePropagator;
import org.apache.logging.log4j.status.StatusLogger;

/**
* Propagates Log4j Core level to JUL.
*/
@ServiceProvider(value = Log4jBridgeHandler.LevelPropagator.class)
public class JulLevelPropagator implements Log4jBridgeHandler.LevelPropagator, Consumer<Configuration> {
@ServiceProvider(value = LevelChangePropagator.class)
public class JulLevelPropagator implements LevelChangePropagator, Consumer<Configuration> {

private static final Logger LOGGER = StatusLogger.getLogger();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.jul.internal.DefaultLevelConverter;
import org.apache.logging.log4j.jul.internal.JulProperties;
import org.apache.logging.log4j.jul.spi.LevelConverter;
import org.apache.logging.log4j.kit.env.PropertyEnvironment;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.LoaderUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ServiceLoader;
import java.util.logging.LogRecord;
import org.apache.logging.log4j.jul.spi.LevelChangePropagator;
import org.apache.logging.log4j.spi.ExtendedLogger;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.ServiceLoaderUtil;
Expand Down Expand Up @@ -64,7 +65,7 @@
* @see <a href="https://logging.apache.org/log4j/3.x/log4j-jul.html">Log4j documentation site</a>
* @since 2.15.0
*/
@ServiceConsumer(value = Log4jBridgeHandler.LevelPropagator.class, cardinality = Cardinality.SINGLE)
@ServiceConsumer(value = LevelChangePropagator.class, cardinality = Cardinality.SINGLE)
public class Log4jBridgeHandler extends java.util.logging.Handler {
private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();

Expand All @@ -77,7 +78,7 @@ public class Log4jBridgeHandler extends java.util.logging.Handler {

private boolean doDebugOutput = false;
private String julSuffixToAppend = null;
private LevelPropagator levelPropagator;
private LevelChangePropagator levelPropagator;
private volatile boolean installAsLevelPropagator = false;

/**
Expand Down Expand Up @@ -135,8 +136,9 @@ private void configure(String suffixToAppend, boolean propagateLevels) {
installAsLevelPropagator = propagateLevels;
if (installAsLevelPropagator) {
levelPropagator = ServiceLoaderUtil.safeStream(
LevelPropagator.class,
ServiceLoader.load(LevelPropagator.class, getClass().getClassLoader()),
LevelChangePropagator.class,
ServiceLoader.load(
LevelChangePropagator.class, getClass().getClassLoader()),
LOGGER)
.findAny()
.orElse(null);
Expand Down Expand Up @@ -216,30 +218,4 @@ private org.apache.logging.log4j.Logger getLog4jLogger(final LogRecord record) {
}
return org.apache.logging.log4j.LogManager.getLogger(name);
}

/**
* Propagates level configuration from the Log4j API implementation used
* <p>
* Using the {@link Log4jBridgeHandler} is expensive, since disabled log events must be formatted by JUL,
* before they can be dropped by the Log4j API implementation.
* </p>
* <p>
* This class introduces a mechanism that can be implemented by each Log4j API implementation to be notified,
* whenever a {@link Log4jBridgeHandler} is used. The logging implementation will be able to synchronize
* its levels with the levels of JUL loggers each time it is reconfigured.
* </p>
* @since 3.0.0
*/
public interface LevelPropagator {

/**
* Start propagating log levels.
*/
void start();

/**
* Stop propagating log levels.
*/
void stop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
import org.apache.logging.log4j.jul.internal.ApiLoggerAdapter;
import org.apache.logging.log4j.jul.internal.JulProperties;
import org.apache.logging.log4j.jul.internal.NoOpLogger;
import org.apache.logging.log4j.jul.spi.AbstractLoggerAdapter;
import org.apache.logging.log4j.kit.env.PropertyEnvironment;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.LoaderUtil;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jul.internal;

import org.apache.logging.log4j.jul.support.AbstractLogger;
import org.apache.logging.log4j.spi.ExtendedLogger;

/**
* Implementation of {@link java.util.logging.Logger} that ignores all method calls that do not have an equivalent in
* the Log4j API.
*/
public class ApiLogger extends AbstractLogger {

public ApiLogger(ExtendedLogger logger) {
super(logger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jul;
package org.apache.logging.log4j.jul.internal;

import java.util.logging.Logger;
import org.apache.logging.log4j.jul.spi.AbstractLoggerAdapter;
import org.apache.logging.log4j.message.MessageFactory;
import org.apache.logging.log4j.message.MessageFormatMessageFactory;
import org.apache.logging.log4j.spi.LoggerContext;
Expand All @@ -32,7 +33,7 @@ public class ApiLoggerAdapter extends AbstractLoggerAdapter {
private static final MessageFactory MESSAGE_FACTORY = new MessageFormatMessageFactory();

@Override
protected Logger newLogger(final String name, final LoggerContext context) {
public Logger newLogger(final String name, final LoggerContext context) {
return new ApiLogger(context.getLogger(name, MESSAGE_FACTORY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jul;
package org.apache.logging.log4j.jul.internal;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -25,6 +25,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.jul.LevelTranslator;
import org.apache.logging.log4j.jul.spi.LevelConverter;

/**
* Default implementation of LevelConverter strategy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jul;
package org.apache.logging.log4j.jul.internal;

import org.apache.logging.log4j.jul.spi.AbstractLoggerAdapter;
import org.apache.logging.log4j.jul.spi.LevelConverter;
import org.apache.logging.log4j.kit.env.Log4jProperty;
import org.jspecify.annotations.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jul;
package org.apache.logging.log4j.jul.internal;

import java.util.ResourceBundle;
import java.util.function.Supplier;
Expand All @@ -27,7 +27,7 @@
*/
public class NoOpLogger extends Logger {

protected NoOpLogger(final String name) {
public NoOpLogger(final String name) {
super(name, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jul;
package org.apache.logging.log4j.jul.spi;

import java.util.logging.Logger;
import org.apache.logging.log4j.LogManager;
Expand All @@ -36,14 +36,29 @@
* If you wish to enable this feature again, you need to implement this class and provide its FQCN
* as {@code log4j.jul.loggerAdapter} configuration property.
* </p>
* <p>
* <strong>Implementation note:</strong> since version 3.0.0, this interface was moved to a new package.
* </p>
*
* @see <a href="https://github.com/apache/logging-log4j2/issues/2353">Issue #2353</a>
* @since 2.1
*/
public abstract class AbstractLoggerAdapter extends org.apache.logging.log4j.spi.AbstractLoggerAdapter<Logger> {

/**
* Creates a new {@link java.util.logging.Logger}
* <p>
* Each implementation should provide this method.
* </p>
*/
@Override
public abstract Logger newLogger(String name, LoggerContext context);

/**
* Provides the most appropriate {@link LoggerContext} for the caller.
*/
@Override
protected LoggerContext getContext() {
public LoggerContext getContext() {
return getContext(
LogManager.getFactory().isClassLoaderDependent()
? StackLocatorUtil.getCallerClass(java.util.logging.LogManager.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jul.spi;

import org.apache.logging.log4j.jul.Log4jBridgeHandler;

/**
* Propagates level configuration from the Log4j API implementation used
* <p>
* Using the {@link Log4jBridgeHandler} is expensive, since disabled log events must be formatted by JUL,
* before they can be dropped by the Log4j API implementation.
* </p>
* <p>
* This class introduces a mechanism that can be implemented by each Log4j API implementation to be notified,
* whenever a {@link Log4jBridgeHandler} is used. The logging implementation will be able to synchronize
* its levels with the levels of JUL loggers each time it is reconfigured.
* </p>
* @since 3.0.0
*/
public interface LevelChangePropagator {

/**
* Start propagating log levels.
*/
void start();

/**
* Stop propagating log levels.
*/
void stop();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.jul;
package org.apache.logging.log4j.jul.spi;

import org.apache.logging.log4j.Level;

/**
* Strategy interface to convert between custom Log4j {@link Level Levels} and JUL
* {@link java.util.logging.Level Levels}.
* <p>
* <strong>Implementation note:</strong> since version 3.0.0, this interface was moved to a new package.
* </p>
*
* @see JulProperties#levelConverter()
* @see org.apache.logging.log4j.jul.internal.JulProperties#levelConverter()
* @since 2.1
*/
public interface LevelConverter {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Contains interfaces an abstract classes to extend the functionality of Log4j JUL Adapter.
*/
@Export
@Version("3.0.0")
package org.apache.logging.log4j.jul.spi;

import org.osgi.annotation.bundle.Export;
import org.osgi.annotation.versioning.Version;
Loading

0 comments on commit b416ca5

Please sign in to comment.