Apache Log4j api logging.properties configuration detailed explanation

Apache Log4j API is a widely used log record tool for generating log information that is easy to manage and understand in Java applications.LOG4J uses the configuration file to define the location, format and level of the log output.Among them, logging.properties are one of the main files used to define log4j configuration. Logging.properties file consists of a series of key values, each key value pair is used to define the configuration of a log recorder or application.The following is an example of a logging.properties file: # Set the root log level as INFO log4j.rootLogger=INFO, stdout # Configure the format of the output of the console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n # Configure log records of writing files log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=/var/log/myapp.log log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=5 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p %c - %m%n # Configuration log recorder level log4j.logger.com.myapp=INFO log4j.logger.org.hibernate=ERROR The above configuration example first sets the root log level as INFO, which means that all log recorders will only record the INFO level and above logs.Next, configure two different log output methods: console and files. For the console output, the ConsoleAppEnter was used and defined the output format.%D is the date and time,%T is a thread name,%-5P is the log level (the width of the position is 5),%C is the class name,%m is message, and%n is the royal symbol.By modifying these placeholders, the output format can be customized. For file log records, the RollingFileAPENDER is used and sets the path of the output file, the maximum file size, and the number of backups saved.The log will be added to the specified file. Once the file size exceeds the size of the specified maximum file, the log will be archived and a new empty file will be created for log records. Finally, a log recorder level with different packages is configured through log4j.logger.com.myApp's log level is set to Info, and the log level of Org.Hibernate is set to ERROR.This means that in these two bags and their sub -bags, only the logs above the INFO level will be recorded. In Java applications, we can load and apply the above configuration files in the following ways: import org.apache.log4j.LogManager; public class MyApp { private static final Logger logger = LogManager.getLogger(MyApp.class); public static void main(String[] args) { // Load the configuration file LogManager.getRootLogger(); // Record log logger.debug("This is a debug message"); logger.info("This is an info message"); logger.warn("This is a warning message"); logger.error("This is an error message"); logger.fatal("This is a fatal message"); } } In the above examples, the configuration file is loaded through the `Logmanager.getrootLogger ()`.Then, we obtained the Logger instance and used different levels of log record methods to record several log information. Through the above configuration and code examples, you can better understand and configure the logging.properties file in the Apache Log4j API and implement suitable log records in your Java application.