Java EE SSH Framework Integration (iv) Log processing spring combines log4j, slf4j [go]

Source: Internet
Author: User

1. Add log4j and slf4j jar packages

2. Web. xml:

[HTML]View Plaincopyprint?
  1. <context-param>
  2. <!--log4j configuration address --
  3. <param-name>log4jconfiglocation</param-name>
  4. <!--<param-value>/WEB-INF/classes/log4j.properties</param-value> --
  5. <param-value>classpath:log4j.properties</param-value>
  6. </context-param>
  7. <context-param>
  8. <!--log4j configuration scan refresh interval can be used without ---
  9. <param-name>log4jrefreshinterval</param-name>
  10. <param-value>10000</param-value>
  11. </context-param>
  12. <!--spring LOG4J configuration Listener--
  13. <listener>
  14. <listener-class>org.springframework.web.util.log4jconfiglistener</ Listener-class>
  15. </listener>

3. Src/log4j.properties:

[Plain]View Plaincopyprint?
  1. # # # set log levels # # #
  2. Log4j.rootlogger = Debug,stdout,d,e
  3. # # # Output to console # # #
  4. Log4j.appender.stdout = Org.apache.log4j.ConsoleAppender
  5. Log4j.appender.stdout.Target = System.out
  6. Log4j.appender.stdout.Threshold = INFO
  7. Log4j.appender.stdout.layout = Org.apache.log4j.PatternLayout
  8. Log4j.appender.stdout.layout.ConversionPattern =%d{absolute}%5p%c{1}:%l-%m%n
  9. Log4j.appender.stdout.encoding=utf-8
  10. # # # Output to log file # # #
  11. LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender
  12. Log4j.appender.d.file = ${ssh}web-inf/logs/debug.log
  13. log4j.appender.d.file.maxfilesize=500kb
  14. Log4j.appender.d.append = True
  15. # #输出DEBUG级别以上的日志
  16. Log4j.appender.d.threshold = DEBUG
  17. Log4j.appender.d.layout = Org.apache.log4j.PatternLayout
  18. Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n
  19. Log4j.appender.d.encoding=utf-8
  20. # # # Save exception information to a separate file # # #
  21. LOG4J.APPENDER.E = Org.apache.log4j.DailyRollingFileAppender
  22. # # Exception Log file name
  23. Log4j.appender.e.file = ${ssh}web-inf/logs/error.log
  24. log4j.appender.e.file.maxfilesize=500kb
  25. Log4j.appender.e.append = True
  26. # # Only output logs above the error level!!!
  27. Log4j.appender.e.threshold = ERROR
  28. Log4j.appender.e.layout = Org.apache.log4j.PatternLayout
  29. Log4j.appender.e.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n
  30. Log4j.appender.e.encoding=utf-8

Configure the Info-level output to the console, and debug and error are output to the corresponding file.

4. Using the Log Object

[Java]View Plaincopyprint?
    1. Import Org.slf4j.Logger;
    2. Import Org.slf4j.LoggerFactory;
    3. Import com.stone.action.DefaultAction;
    4. Public class Logutil {
    5. //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
    6. private static Logger Logger = Loggerfactory.getlogger (logutil.   Class);
    7. //Use log4j to create a log object
    8. Private Org.apache.log4j.Logger Logger = Org.apache.log4j.Logger.getLogger (GetClass ());
    9. public static void error (String message) {
    10. Logger.error (message);
    11. }
    12. public static void debug (String message) {
    13. Logger.debug (message);
    14. }
    15. public static void info (String message) {
    16. Logger.info (message);
    17. Org.apache.log4j.Logger.getLogger (logutil.class). info (message);
    18. }
    19. }

Java EE SSH Framework Integration (iv) Log processing spring combines log4j, slf4j [go]

Related Article

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.