Alibabacloud.com offers a wide variety of articles about spring log4j configuration example, easily find your spring log4j configuration example information here online.
first, a simple introduction to the use of log4j steps
The whole is divided into the following steps: 1, the introduction of Log4j-*.*jar, the JAR package 2, add a configuration file (Log4j.xml or Log4j.properties), 3, the Packaging Log tool class in the business use. This is the traditional use of the steps, but we can directly with the Lombok wrapped Log too
I have not written examples in the log4j usage notes. In this article, I will write a complete application instance of log4j.
In the log4j usage notes, we already know that the same log information can be output to multiple output destinations at the same time. In the following example, I will demonstrate how to output
used to escape characters.2. Location of the log4j. properties File(1) In a Java project, it is placed in the root directory of the project rather than in the src directory of the project.(2) In the javaweb project, it is placed in the src directory, because Tomcat will load it by default, we do not need to manually load the log4j configuration file. We only nee
, thread, category, etc.) of the logConfiguration mode:Log4j.appender.appenderName.layout =classnameLog4j.appender.appenderName.layout.Option1 = value1...Log4j.appender.appenderName.layout.OptionN = ValuenSecond, configuration detailedIn the actual application, in order to make the log4j run in the system, the configuration file must be set beforehand. The
After you create a project based on the Spring Framework, the console prints a warning message when you use JUnit for unit testing:
Log4j:warn No Appenders could is found for logger (org.springframework.util.ClassUtils).Log4j:warn Please initialize the log4j system properly.
Check on the Internet, incredibly many "suggestions" is to ignore these two warning messages, other search results are not satisf
has been customized with a name of MyLogger and a log level of warn.2. Configure the output destination AppenderAppender's task is to send the layout formatted output to the specified destination. Each appender is associated with a layout, and the task of layout is to format the user's logging request.Log4j.appender.stdout=org.apache.log4j.consoleappenderThis sentence defines the type of output destination named stdout, which can beOrg.apache.log4j.ConsoleAppender (console),Org.apache.log4j.Fil
lowest priority: ERROR, WARN, INFO, DEBUGOnly log information with a priority higher than or equal to the set level is forwarded, for example, if the definition is info, the log information at level debug in the program is not output.Log4j.rootlogger is the parent object of all loggers (formerly known as Rootcategory), which can be used to set the default priority of all loggersLoggers can inherit, such as: Log4j.mylogger.childlogger=,fileLogger is o
=log4j.log
log4j.appender.r.maxfilesize=100kb
Log4j.appender.r.maxbackupindex=1
Log4j.appender.r.layout=org.apache.log4j.patternlayout
log4j.appender.r.layout.conversionpattern=%p%t%c-%m%n
This configuration file specifies two output sources A1 and R. The former output log information to the console, the latter is a rotary log file. The largest file is 100KB, when a log file reaches the maximum size, log4j
Spring uses log4j in two ways.
1. No configuration is made in Web. xml.
Log4j. properties is stored in the root directory of classpath,
At this time, the generated log file does not have a relative path. if you write a relative path, it is generated under the root path of Tomcat installation.
2. Set in Web. xml.
Not
Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import com.stone.action.DefaultAction;
Public class Logutil {
//Use SLF4J to create log objects, when the benefits are changed later to other log tools, just modify the configuration file without modifying the code
private static Logger Logger = Loggerfactory.getlogger (logutil. Class);
//Use log4j to create a log object
Privat
configure log4j with XML or Java at run time, the properties file is useful.
Log4j.rootlogger=info, stdout
Log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
# to output the caller ' s file name and line number.
log4j.appender.stdout.layout.conversionpattern=%5p [%t] (%f:%l)-%m%n
Analyze:
First line, configure Log4j.rootlogger you know. It should be the root, must be configured, otherwis
:%d{yyyy MMM dd hh:mm:ss,sss}, output similar to: October 18, 2002 22:10:28,921#%l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10)Examples of formatting:LOG4J.APPENDER.THISPROJECT.FILE.OUT.LAYOUT.CONVERSIONPATTERN=LOG4J--GT;%D{YYYY MMM DD HH:mm:ss,SSS}%5p{%F\:%L}-% m%nAttention:1. Small knowl
function need to output a separate log file, how to separate the required content from the original log, forming a separate log file. In fact, as long as the existing log4j based on a little configuration can easily achieve this function.
First look at a common log4j.properties file, which is logging in the console and the Myweb.log file:
Log4j.rootlogger=debug, stdout, logfile
Log4j.category.org.springf
the corresponding file.4. Using the Log Object1 ImportOrg.slf4j.Logger; 2 Importorg.slf4j.LoggerFactory; 3 4 Importcom.stone.action.DefaultAction; 5 6 Public classLogutil {7 //use SLF4J to create log objects, and when you change to other log tools later, you do not have to modify the code until you modify the configuration file8 Private StaticLogger Logger = Loggerfactory.getlogger (logutil.class); 9 //Create a Log object using log4j
The use of log4j logging is very convenient, it provides two types of log configuration, log4j.propertes and Log4j.xml, this document first posted Log4j.properties configuration method
1. Brief analysis of log4j.properties file configuration
The following gives the log4j.properties file commonly used in some informatio
First, a new log4j.properties file can be created in the classes of the project; In the actual programming, in order to make the log4j really run in the system in advance also to define the configuration file. The defining step is to use the logger, Appender, and layout separately. LOG4J supports two configuration file
First, a new log4j.properties file can be created in the classes of the project; In the actual programming, in order to make the log4j really run in the system in advance also to define the configuration file. The defining step is to use the logger, Appender, and layout separately. LOG4J supports two configuration file
In general, some important logs in our project want to be output to the specified file separately, instead of the total output to the System Log File. Therefore, log4j provides us with this function, next, let's take a step-by-step look at how it works. This is written in the property configuration mode. The XML method is similar. For more information, see the official documentation.
The project package str
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.