Apache Extras ™ for Apache Log4j ™ in Java Library How to use

Apache Extras ™ for Apache Log4j ™ in Java Library How to use Apache Extras ™ for Apache Log4j ™ is a third -party expansion library for enhancing Apache Log4j, which aims to provide more functions and flexibility.This article will introduce how to use Apache Extras ™ for Apache Log4j ™ in the Java library and provide example code. The use steps of Apache Extras ™ for Apache Log4j ™ are as follows: Step 1: Import library First, the Apache Extras ™ for Apache Log4j ™ library needs to be added to the dependence of the project.You can import the library by adding the following dependencies by adding the following dependencies by adding the following management tools such as Maven or Gradle: Maven coordinates: <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.x.x</version> </dependency> <dependency> <groupId>org.apache.logging.log4j.extras</groupId> <artifactId>log4j-extras</artifactId> <version>1.x.x</version> </dependency> Gradle dependencies: implementation 'org.apache.logging.log4j:log4j-core:2.x.x' implementation 'org.apache.logging.log4j.extras:log4j-extras:1.x.x' Make sure the version number is replaced with the latest version available. Step 2: Configure log4j In the appropriate position in the code, the log4j needs to be configured for log output.According to the project needs, you can configure the files with XML or Properties, or configure it by programming.Here are an example of using XML configuration files. Log4j2.xml configuration file example: <?xml version="1.0" encoding="UTF-8"?> <Configuration> <Appenders> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Root level="debug"> <AppenderRef ref="console"/> </Root> </Loggers> </Configuration> Place log4j2.xml in the resource folder of the project. Step 3: Use Apache Extras ™ for Apache Log4j ™ Once the log4j configuration is complete, the features provided by Apache Extras ™ for Apache Log4j ™ can be used in the Java class library.Here are some examples. Example 1: Use MESSAGEPATERNORTER import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.Message; import org.apache.logging.log4j.message.MessageFactory; import org.apache.logging.log4j.message.ObjectMessage; import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.message.StringFormatterMessageFactory; public class MyApp { private static final Logger logger = LogManager.getLogger(MyApp.class); public static void main(String[] args) { // Use the default messageFactory MessageFactory messageFactory = new StringFormatterMessageFactory(); // Use MessagePatternoconverter to output logs Message message = new ObjectMessage("Hello, Log4j with Apache Extras!"); logger.info(message); // Use parameterizedMessage to output the log of parameters with parameters logger.info("Hello, {} with {}!", "Log4j", "Apache Extras"); } } Example 2: Use DynamicthResholdFilter import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.config.Configurator; import org.apache.logging.log4j.core.Filter; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.LoggerConfig; import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder; import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; import org.apache.logging.log4j.core.appender.ConsoleAppender; import org.apache.logging.log4j.core.layout.PatternLayout; import org.apache.logging.log4j.core.filter.DynamicThresholdFilter; public class MyApp { private static final Logger logger = LogManager.getLogger(MyApp.class); public static void main(String[] args) { // Create a configurationBuilder ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); // Configure consoleappender builder.add(builder.newAppender("console", "CONSOLE") .addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT) .add(builder.newLayout("PatternLayout") .addAttribute("pattern", "%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n"))); // Create DynamicthResholdFilter Filter filter = DynamicThresholdFilter.createFilter("INFO", "0", "org.example.myapplication.MyApp", null, "true"); // Configure loggerConfig builder.add(builder.newLogger("org.example.myapplication.MyApp", "INFO") .add(builder.newAppenderRef("console")) .addAttribute("additivity", false) .add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.DENY) .addAttribute("level", "INFO") .addAttribute("onMatch", Filter.Result.ACCEPT.name()) .addAttribute("onMismatch", Filter.Result.DENY.name()))) .add(builder.newRootLogger("INFO") .add(builder.newAppenderRef("console"))) .add(builder.newScriptFilter("js", filter)); // Construct configuration Configuration configuration = builder.build(); // Re -configure log4j LoggerContext context = Configurator.initialize(configuration); // Use the re -configured log4j for log output logger.info("Hello, Log4j with DynamicThresholdFilter!"); } } Example 1 shows how to output log messages with MESSAGEPATERNORTER of Apache Extras ™ for Apache Log4j ™ to output log messages.Example 2 demonstrates how to use DynamicthResholdFilter to dynamically set up log -level filters. I hope this article can help you understand and use Apache Extras ™ for Apache Log4j ™ in the Java library.For more details and more examples, see Apache Log4j ™ and Apache Extras ™ for Apache Log4j ™.