The log frameworks available for MyBatis include SLF4J, Apache Commons Logging, Log4J, and JDK logging. This article will transform the previous example to demonstrate the simple use of Log4J source code: http://down.51cto.com/data/521820. This source code corresponds to the fifth case of the following content ).
First, copy the related Log4J jar package to the lib directory of the project, this example uses the log4j-1.2.16.jar.
The log4j. properties file should be created to specify the behavior of Log4J and placed in classpath. In this example, the file is placed in the src directory of the Project. before the project is compiled, the file is copied to the classes directory. For details, see the build file of the project. copy-resources target in xml, starting from 23 rows. Click here to download the sample source code), and the classes directory will be loaded to the classpath by ant. For details, see the 37-39 line of build. xml. For the Log Level of Log4J, see: http://www.51testing.com /? Uid-225738-action-viewspace-itemid-212359; configuration see: http://www.blogjava.net/zJun/archive/2006/06/28/55511.html ):
The main program is as follows:
Package com. demo;
Import org. springframework. context. ApplicationContext;
Import com. abc. mapper. StudentMapper;
Import com. abc. mapper. TeacherMapper;
Import com. abc. domain. Teacher;
Import com. abc. domain. Student;
Import org. springframework. context. support. ClassPathXmlApplicationContext;
Import java. util. List;
Publicclass CollectionDemo
{
Privatestatic ApplicationContext ctx;
Static
{
// Search for the resources/beans. xml file in the class path
Ctx = new ClassPathXmlApplicationContext ("resources/beans. xml ");
}
Publicstaticvoid main (String [] args)
{
// Request the er from the Spring container.
// @ Component annotation specifies the bean name
TeacherMapper mapper =
(TeacherMapper) ctx. getBean ("myTeacherMapper ");
// Query the instructors whose id is 1
Teacher teacher = mapper. getById (1 );
If (teacher = null)
{
System. out. println ("related instructor information not found. ");
}
Else
{
// Instructor Information
System. out. println ("************************************* *********");
System. out. println ("instructor name:" + "" + teacher. getName ());
System. out. println ("instructor title:" + "" + teacher. getTitle ());
System. out. println ("************************************* *********");
System. out. println ("Guiding student information :");
// Traverse the guided students
For (Student s: teacher. getSupStudents ())
{
System. out. println ("************************************* *********");
System. out. println (s. getName () + "" + s. getGender () + "" + s. getGrade ()
+ "" + S. getMajor ());
// Access the instructor from the student end
System. out. println ("instructor direction:" + s. getSupervisor (). getResearchArea ());
}
System. out. println ("************************************* *********");
}
}
}
It can be seen that the core of the program is to call the getById method of TeacherMapper.
1. If the content of log4j. properties is as follows:
# Global log Configuration
Log4j. rootLogger = DEBUG, stdout
# MyBatis log-level configuration. Configure com. abc. mapper
# The Log Level of all classes under the package
Log4j.logger.com. abc. mapper = DEBUG
# Use the following 10 and 11 lines to configure the getById of TeacherMapper and TeacherMapper.
# Method log level. Log4j.logger.com. abc. mapper. TeacherMapper
# The prefix of log4j.logger.com. abc. mapper indicates
# Log4j.logger.com. abc. mapper is its parent logger. Others and so on
# Log4j.logger.com. abc. mapper. TeacherMapper = TRACE
# Log4j.logger.com. abc. mapper. TeacherMapper. getById = INFO
# Log output to the console
Log4j. appender. stdout = org. apache. log4j. leleappender
Log4j. appender. stdout. layout = org. apache. log4j. PatternLayout
Log4j. appender. stdout. layout. ConversionPattern = % 5 p [% t]-% m % n
# Disable Spring logs
Log4j.category.org. springframework = OFF
The running result is as follows:
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201209/213311559.png "border =" 0 "/>
It can be seen that logs under the com. abc. mapper package have been printed at the DEBUG level.
2. If you change the Log Level of the com. abc. mapper package to INFO, that is:
Log4j.logger.com. abc. mapper = INFO
The execution result is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12135L128-1.png "border =" 0 "/>
The INFO level is higher than the DEBUG level. If it is changed to INFO, the DEBUG level log is no longer printed, but the DEBUG log is still printed as in the previous example. As a sub-logger of rootLogger, even if the log level defined by log4j.logger.com. abc. mapper is higher than that defined by rootLogger, it cannot take effect.
3. Now, change the Log Level of com. abc. mapper to TRACE level lower than DEBUG), that is:
Log4j.logger.com. abc. mapper = TRACE
The running result is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12135M2a-2.png "border =" 0 "/>
As you can see, TRACE-level logs have been printed and TRACE settings have taken effect.
4. If the Log Level of log4j.logger.com. abc. mapper is still TRACE, as its sub-logger, log4j.logger.com. abc. mapper. TeacherMapper is set to DEBUG higher than TRACE), that is:
Log4j.logger.com. abc. mapper. TeacherMapper = DEBUG
The running result is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12135L617-3.png "border =" 0 "/>
It can be seen that TRACE-level logs are no longer printed and used as the log4j.logger.com. abc. mapper. although TeacherMapper's log-level DEBUG is higher than log4j.logger.com. abc. mapper's log level, but it also takes effect.
5. If the Log Level of log4j.logger.com. abc. mapper is DEBUG, set log4j.logger.com. abc. mapper. TeacherMapper to a level lower than DEBUG:
Log4j.logger.com. abc. mapper = DEBUG
Log4j.logger.com. abc. mapper. TeacherMapper = TRACE
The running result is as follows:
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201209/201123351.png "border =" 0 "/>
As we can see, as we have guessed, as the sub-logger's log4j.logger.com. abc. mapper. TeacherMapper, its TRACE-level settings have taken effect.
Conclusion: If the logger level is higher than the rootLogger level, it does not take effect. The value is calculated based on the rootLogger level. Under this premise, the logger levels take effect based on their own settings.
MyBatis Study Notes series I: download and install ant
Prepare for MyBatis Study Notes Series II: ant getting started
MyBatis learning notes: MyBatis getting started
MyBatis Study Notes Series II: Example of adding, deleting, and modifying MyBatis
MyBatis Study Notes Series 3: association examples of MyBatis
MyBatis Study Notes Series 4: two forms of MyBatis association
MyBatis Study Notes Series 5: examples of integration between MyBatis and Spring
MyBatis Study Notes Series 6: examples of integration between MyBatis and Spring
MyBatis study notes 7: MyBatis one-to-multiple bidirectional Association
MyBatis Study Notes: MyBatis MapperScannerConfigurer Configuration
MyBatis Study Notes: two forms of MyBatis collection
MyBatis Study Notes Series 10: Log4j example of MyBatis logs
MyBatis Study Notes: example of how to annotate MyBatis with multiple parameters
MyBatis learning notes: Example of the default naming method for MyBatis multi-parameter transfer
MyBatis Study Notes: Example of Map mode for MyBatis multi-parameter transfer
MyBatis Study Notes: N + 1 in MyBatis
MyBatis Study Notes: a hybrid transfer of multiple parameters in MyBatis
MyBatis Study Notes:Spring declarative Transaction Management example
MyBatis Study Notes: Example of MyBatis multiple-to-multiple storage
This article is from the "Xiao fan's column" blog, please be sure to keep this source http://legend2011.blog.51cto.com/3018495/999944