Log4j can be configured through a configuration file or through Java code. Configuration files can be written in XML, JSON, YAML, or properties file format. Within a configuration you can define three main components: Loggers, Appenders and Layouts. Add the monitorInterval setting to the Configuration section of the file and log4j will scan the file at the specified interval. Configuration: monitorInterval: 30 The interval is specified in seconds.
This post looks at using and configuring the Log4j 2 properties file, letting you change various configuration options without modifying your application code.
Join the DZone community and get the full member experience.
Join For Free
Log4J 2 is a logging framework designed to address the logging requirements of enterprise applications. Its predecessor Log4J 1.x has been around for more than one and a half decade and is still one of the most widely used Java logging frameworks. Log4J has even been ported to the .NET world. Log4net is one of the most popular logging frameworks for Microsoft’s .NET environment. Log4J 2 goes steps ahead by removing the inherent architectural flaws of Log4J 1.x. Since the initial release of Log4J 2 on August 2015, it’s quickly being adopted by the developer community. I wrote an introductory post on Log4J 2 here. If you have not read it, I recommend starting with the introductory post first. In this post, I will discuss how to configure Log4J 2 using a properties configuration file. This is just one of several ways you can configure Log4J 2. What Are Log4J 2 Configuration Files?Log4J 2 provides various components, such as loggers, appenders, and layouts that work together to perform logging in an application. As different applications have different logging requirements, you’re able to configure LogJ 2 accordingly. Also, you will often need to keep changing Log4J 2 configurations of an application across its deployment lifecycle. For example, it is common to set the logging level to DEBUG during development, and later switch it to ERROR to avoid filling your logs with excessive debug information. Similarly, during local development, you can work with the console appender to avoid file I/O overheads and in other deployment environments, set a file appender or some other persistent destination to preserve log messages. You can configure Log4J 2 either programmatically in your application or through configuration files, such as properties, XML, JSON, and YAML residing on your project classpath. Through the use of configuration files, you have the flexibility of changing the various configuration options without modifying your application code. In this post, we’re going to look at using the properties file. Setting Up Log4J 2 to Use the Properties FileUnlike its predecessor Log4J 1.x, Log4J 2 did not support configuration through the properties file when it was initially released. Winrar mac os free download. It was from Log4J 2.4 that support for the properties file was again added, but with a completely different syntax. Log4J4 Maven DependenciesTo use Log4J 2 in your application, you need to ensure that the Log4J 2 jars are on your project classpath. If you intend to use the properties file, give extra attention to ensure that you have the Log4J 2.4 or greater jars on the classpath. Otherwise, your properties file will not get picked. Log4J 2 Spring Boot DependenciesIf you want to use Log4J 2 in a Spring Boot project, things can be a bit tricky. Simply adding the dependencies above won’t work as Spring Boot will first find the default Logback classic on the classpath, and will use it. Therefore, you need to exclude the default dependency of the Spring Boot starter on Logback classic, and instead include the Spring Boot starter dependency on Log4J 2, like this: This will configure Spring Boot to use Log4J 2, but with a catch – You still won’t be able to use the properties file for configuration. As of Spring Boot 1.3.3 Release, Spring Boot starter dependency on Log4J 2 is for Log4J 2.1, and as I have already mentioned it is from Log4J 2.4 onward that the properties file is supported. Therefore, you need to explicitly specify dependencies of Log4J 2.4 or above after excluding Spring Boot starter logging, like this. The above dependencies will set up Log4J 2 to use the properties file in a Spring Boot application. Configuring Log4J 2 Using the Properties FileBy default, Log4J 2 looks for a properties file with the name log4j2.properties in the classpath. In a Spring Boot application, the log4j2.properties file will typically be in the resources folder. Before we start configuring Log4J 2, we will write a Java class to generate log messages via Log4J 2. Log4J2PropertiesConf.javaTo test the Log4J2PropertiesConfTest.javaWe will now configure Log4J 2 using a properties file. Like any other Java properties file, a log4j2.properties file is a set of key-value pairs with options to configure the various components of Log4J 2, such as loggers, appenders, and layouts. A basic log4j2.properties file starts with a name, optional properties to be used in other parts of the file, and appender declarations. The preceding code declares two appenders, named In the code above we configured two appenders: One to write log messages to the console and the other to a log file. Both the appenders use pattern layouts that are configurable with conversion pattern strings to format log messages. The Next, we will configure the loggers, starting from the root logger. In the code above, we configured the root logger to log debug and its lower level messages to the console (stdout). When we run the Log4J2PropertiesConfTest test class, the output in the IntelliJ console will be similar to this. The complete log4j2.properties file is as follows: log4j2.propertiesWhen we run the Log4J2PropertiesConfTest test class now, log messages will be sent to the logs/propertieslogs.log by the file logger and additively to the console by the root logger. The following figure shows the log messages sent to the file and console in IntelliJ. In the example above, it is due to logger additivity that caused log messages to be sent to the file by the logger and additively to the console by the root logger. You can override this default behavior by setting the additivity flag of a logger to false. The property above configures our file appender so that it is no longer additive, thus log messages will only be sent to the file. Download cracklock for mac free. Appender additivity can be somewhat confusing. I suggest reviewing the Log4J 2 documentation on the subject, where they have some good examples of how this works. Usb format for mac. SummaryUsing the properties file is one of the several options you have to configure Log4J 2. Log4J 2 is gradually moving to XML configuration and the new JSON and YAML configurations. Properties configuration cannot handle some advanced features, such as custom error handlers, time-based rolling policies, nested appenders, and special types of appenders, such as async appenders. However, properties configuration is still being extensively used. Often you don’t need many of the more advanced logging features of Log4J 2. So you’re fine using the simplicity of the properties file configuration. In future posts, I will cover using other configuration options for Log4J 2 to address logging configurations with more complex requirements. Like This Article? Read More From DZone
logging ,log4j ,log4j2 ,java ,tutorial
Published at DZone with permission of John Thompson , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Log4j Appenders provides configuration for logging such as console, file, database etc. Below image shows the log4j Appender class hierarchy.
Table of Contents
log4j AppenderLog4j File Appender Configuration Xml
This is the base of log4j Appenders that defines the methods to implement by an appender.
log4j AppenderSkeleton
This class provides the code for common functionality, such as support for threshold filtering and support for general filters. This is the base implementation that is extended by all other appenders such as
JDBCAppender , AsyncAppender , ConsoleAppender etc. It has only one abstract method append(LoggingEvent event) that is required to be implemented by all appenders. We can write our own custom appender by implementing this method.
Commonly used log4j Appenders
Some of the most commonly used appenders are:
Log4j Appenders XML ConfigurationLog4j Appender Example
Below is the XML based configuration of commonly used ConsoleAppender and RollingFileAppender classes.
You can check the appender classes code to find out the parameters you can configure. For example in JDBCAppender you can configure databaseURL, databaseUser, databasePassword etc.
Log4j Appender Properties Configuration
A simple example showing appenders configuration through property file. It’s defining all the above xml based configuration.
Log4j To File
That’s all for a quick roundup on log4j appenders.
Log4j File Appender Configuration Example
References:
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |