Log4j example of MyBatis log -- 10 of MyBatis Study Notes

Source: Internet
Author: User

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:

 
 
  1. Package com. demo;

  2. Import org. springframework. context. ApplicationContext;

  3. Import com. abc. mapper. StudentMapper;

  4. Import com. abc. mapper. TeacherMapper;

  5. Import com. abc. domain. Teacher;

  6. Import com. abc. domain. Student;

  7. Import org. springframework. context. support. ClassPathXmlApplicationContext;

  8. Import java. util. List;

  9. Publicclass CollectionDemo

  10. {

  11. Privatestatic ApplicationContext ctx;

  12. Static

  13. {

  14. // Search for the resources/beans. xml file in the class path

  15. Ctx = new ClassPathXmlApplicationContext ("resources/beans. xml ");

  16. }

  17. Publicstaticvoid main (String [] args)

  18. {

  19. // Request the er from the Spring container.

  20. // @ Component annotation specifies the bean name

  21. TeacherMapper mapper =

  22. (TeacherMapper) ctx. getBean ("myTeacherMapper ");

  23. // Query the instructors whose id is 1

  24. Teacher teacher = mapper. getById (1 );

  25. If (teacher = null)

  26. {

  27. System. out. println ("related instructor information not found. ");

  28. }

  29. Else

  30. {

  31. // Instructor Information

  32. System. out. println ("************************************* *********");

  33. System. out. println ("instructor name:" + "" + teacher. getName ());

  34. System. out. println ("instructor title:" + "" + teacher. getTitle ());

  35. System. out. println ("************************************* *********");

  36. System. out. println ("Guiding student information :");

  37. // Traverse the guided students

  38. For (Student s: teacher. getSupStudents ())

  39. {

  40. System. out. println ("************************************* *********");

  41. System. out. println (s. getName () + "" + s. getGender () + "" + s. getGrade ()

  42. + "" + S. getMajor ());

  43. // Access the instructor from the student end

  44. System. out. println ("instructor direction:" + s. getSupervisor (). getResearchArea ());

  45. }

  46. System. out. println ("************************************* *********");

  47. }

  48. }

  49. }

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:

 
 
  1. # Global log Configuration

  2. Log4j. rootLogger = DEBUG, stdout

  3. # MyBatis log-level configuration. Configure com. abc. mapper

  4. # The Log Level of all classes under the package

  5. Log4j.logger.com. abc. mapper = DEBUG

  6. # Use the following 10 and 11 lines to configure the getById of TeacherMapper and TeacherMapper.

  7. # Method log level. Log4j.logger.com. abc. mapper. TeacherMapper

  8. # The prefix of log4j.logger.com. abc. mapper indicates

  9. # Log4j.logger.com. abc. mapper is its parent logger. Others and so on

  10. # Log4j.logger.com. abc. mapper. TeacherMapper = TRACE

  11. # Log4j.logger.com. abc. mapper. TeacherMapper. getById = INFO

  12. # Log output to the console

  13. Log4j. appender. stdout = org. apache. log4j. leleappender

  14. Log4j. appender. stdout. layout = org. apache. log4j. PatternLayout

  15. Log4j. appender. stdout. layout. ConversionPattern = % 5 p [% t]-% m % n

  16. # Disable Spring logs

  17. 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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.