Summary on slf4j + logback integration to provide complete log records

Source: Internet
Author: User

1. slf4j combination:

Several practical application modes of slf4j: slf4j + log4j

Slf4j (simple logging facade for Java) is a common log framework. It cannot be called a facade. The role played by the eye is equivalent to Jakarta commons logging. Just as jcl requires underlying log implementation, such as log4j, java. util. logging, and simple logger to complete specific information output, jcl + log4j is always a perfect configuration. The original purpose of slf4j is to support the implementation of various lower-layer log frameworks, but the best log implementation is log4j. Therefore, this article describes the first use of slf4j + log4j.

The following three jar files and a properties file must be placed on the classpath of the project.

1. slf4j-api-1.5.11.jar
2. slf4j-log4j12-1.5.11.jar
3. log4j-1.2.15.jar
4. log4j. properties (or log4j. XML, log4j. propertes is used in this example)

The first two packages are downloaded in the http://www.slf4j.org/download.html, And the last package is downloaded in the http://logging.apache.org/log4j/1.2/download.html. It may be that the version number in the package file name is poor, it doesn't matter.

Configuration:

How to Write log4j. properties in the past, and how to write it now. If not, you can Baidu it yourself.

Several practical application modes of slf4j: slf4j + logback

 

One of the usage of slf4j mentioned above is slf4j + log4j, and the combination to be introduced here is slf4j + logback. Do not use log4j? Is there a better log implementation than log4j? Yes, the answer is logback. If you know that logback and log4j are the same master, you will not be surprised. Logback has enhanced log4j and put it into the arms of slf4j. It is also recognized by the author as the successor of log4j.

 

I don't know why it's called logback. It's just not called Brokeback. Here are more than a dozen reasons why logback should be used to replace log4j: Reasons to prefer logback over log4j, which is faster; good testing; tight relationship with slf4j; rich documentation; the configuration file can be automatically loaded. Multiple JVMs write one log file, or other I/O errors do not affect program execution. Conditional Control is added to the configuration file. Powerful log filtering is supported; more powerful log splitting functions; automatic compression and deletion of log files; more data information in the exception stack.

 

Logback is divided into three modules: logback-core, logback-classic, and logback-access. Logback-core is the core. logback-classic improves log4j and implements slf4j APIs. Therefore, you can use logback to implement other logs, such as the original log4j and Java. util. logging, etc.; logback-access allows you to easily access log information, such as through HTTP.

 

It is worth mentioning that logback can be better placed in the osgi environment. Now, let's simply describe how to use logback.

 

The following three jar files and an XML file must be placed on the classpath of the project.

 

1. slf4j-api-1.5.11.jar
2. logback-core-0.9.20.jar
3. logback-classic-0.9.20.jar
4. logback. xml or logback-test.xml (similar to log4j. properties/log4j. XML)

Configuration:

Compared with log4j, the configuration file of logback is logback. xml. The specific configuration example of logback. XML is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <! -- For advance ance related to logback-translator or configuration --> <! -- Files in general, please contact the logback user mailing list --> <! -- At http://www.qos.ch/mailman/listinfo/logback-user --> <! --> <! -- For professional support please see --> <! -- Http://www.qos.ch/shop/products/professionalSupport --> <! --> <Configuration>
<! -- Control panel output format configuration --> <appender name = "stdappender" class = "ch. qoS. logback. core. consoleappender "> <encoder> <pattern> % d {yyyy-mm-dd hh: mm: SS, SSS} [% C]-[% P] % m % n </pattern> </encoder> </appender> <appender name = "leleappender" class = "ch. qoS. logback. core. consoleappender "> <encoder> <pattern> % d {yyyy-mm-dd hh: mm: SS, SSS} [% C]-[% P] % m % n </pattern> </encoder> </appender>
<! -- Database configuration, which can store logs to the database --> <appender name = "DB" class = "org. dingo. logbackext. DB. dbappender "> <connectionsource class =" ch. qoS. logback. core. DB. drivermanagerconnectionsource "> <datasource class =" com. mchange. v2.c3p0. combopooleddatasource "> <driverclass> oracle. JDBC. driver. oracledriver </driverclass> <URL> JDBC: oracle: thin: @ IP: 1521: orcl </URL> <user> username </user> <password> password </password> </datasource> </connection Source> <! -- <Nameresolver class = "org. dingo. logbackext. names. success "> --> <strong> logging_event </loggingeventtablename> <strong> logging_event_exception </strong> <strong> logging_event_property </strong> </nameresolver> <printstacktrace> true <> </appender> <! -- Configure file storage --> <appender name = "fileappender" class = "ch. QoS. logback. Core. Rolling. rollingfileappender"> <! -- See also http://logback.qos.ch/manual/appenders.html#RollingFileAppender --> <File> D: \ logs \ system_log.txt </File> <encoder> <pattern> % d {yyyy-mm-dd hh: mm: SS, SSS} [% T] [% C] [% P]-% m % n </pattern> </encoder> <rollingpolicy class = "ch. qoS. logback. core. rolling. fixedwindowrollingpolicy "> <maxindex> 10 </maxindex> <filenamepattern> system_log.txt. % I </filenamepattern> </rollingpolicy> <triggeringpolicy class = "ch. qoS. lo Gback. Core. Rolling. sizebasedtriggeringpolicy "> <maxfilesize> 128 MB </maxfilesize> </triggeringpolicy> </appender> <! -- Configure and package the compressed file --> <appender name = "atomikos" class = "ch. qoS. logback. core. rolling. rollingfileappender "> <File> TM. out </File> <rollingpolicy class = "ch. qoS. logback. core. rolling. fixedwindowrollingpolicy "> <filenamepattern> week </filenamepattern> <minindex> 1 </minindex> <maxindex> 3 </maxindex> </rollingpolicy> <triggeringpolicyclass =" ch. qoS. logback. core. rolling. sizebasedtriggeringpolicy "> <maxfilesize> 2 MB </maxfile Size> </triggeringpolicy> <encoder> <pattern> % d {yyyy-mm-dd hh: mm: SS, SSS} %-4 relative [% thread] %-5 level % logger {35}-% MSG % n </pattern> </encoder> </appender> <! -- Configure log email sending --> <appender name = "SMTP" class = "ch.qos.logback.classic.net. smtpappender "> <evaluator class =" ch. qoS. logback. classic. boolex. onerrorevaluator "/> <subject> % logger-% m </subject> <layout class =" ch. qoS. logback. classic. patternlayout "> <pattern> % d {yyyy-mm-dd hh: mm: Ss. SSS} [% thread] %-5 level % logger-% MSG % n </pattern> </layout> <from> [email protected] </from> <smtphost> smtp.126.com </smtphost> <SSL> false </ SSL> <username> [email protected] </username> <password> xxxxxxxxxxx </password> <to> [email protected] </to> </appender> <! -- The name attribute is the package name --> <logger name = "org. apache. ibatis "level =" error "> <appender-ref =" stdappender "/> </logger> <logger name =" com. ibatis. common. JDBC. simpledatasource "level =" error "> <appender-ref =" leleappender "/> </logger> <logger name =" com. ibatis. common. JDBC. scriptrunner "level =" error "> <appender-ref =" leleappender "/> </logger> <logger name =" com. ibatis. sqlmap. engine. impl. sqlmapclientdelegate "Level =" error "> <appender-ref =" leleappender "/> </logger> <logger name =" Java. SQL. preparedstatement "level =" error "> <appender-ref =" stdappender "/> </logger> <logger name =" Java. SQL. statement "level =" error "> <appender-ref =" stdappender "/> </logger> <logger name =" Java. SQL. resultset "level =" error "> <appender-ref =" stdappender "/> </logger> <logger name =" Java. SQL. connection "level =" error "> <appe Nder-ref = "stdappender"/> </logger> <logger name = "com. atomikos "level =" error "additivity =" false "> <appender-ref =" atomikos "/> </logger> <root level =" error "> <appender-ref ref = "stdappender"/> <appender-ref = "fileappender"/> <! -- <Appender-ref = "DB"/> --> </root> </configuration>

The description of the DB configuration file is described here. I will not talk much about configuring the data source. It is to remind you that you need to create an oracle table after the configuration is complete, logging_event, logging_event_property, and logging_event_exception are all contained in the source jar package of the three table structures. Download the source code using Maven and find it in the souurces package of the source jar package,

My directory path is in: logback-classic-1.0.13-sources.jar \ CH \ QoS \ logback \ classic \ dB \ script-zip compressed file, the size of the package is 513,393 bytes, there are Oracle, MySQL, SQL scripts of popular databases, such as sqlserver, can be used to create the table structure. If you want to know more about the meaning of the configuration representative: can access: http://logback.qos.ch/manual/configuration.html

Here I will provide some JDBC connection configurations for each database ~

 1  <appender name="db-classic-mysql" class="ch.qos.logback.classic.db.DBAppender">   2         <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">   3             <driverClass>com.mysql.jdbc.Driver</driverClass>   4             <url>jdbc:mysql://localhost:3306/logbackdb</url>   5             <user>root</user>   6             <password>qqqqqq</password>   7         </connectionSource>   8     </appender>   9   10     <appender name="db-access" class="ch.qos.logback.access.db.DBAppender">  11         <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">  12             <driverClass>com.mysql.jdbc.Driver</driverClass>  13             <url>jdbc:mysql://localhost:3306/logbackdb</url>  14             <user>root</user>  15             <password>qqqqqq</password>  16         </connectionSource>  17         <insertHeaders>true</insertHeaders>  18     </appender>  19     -->  20     <appender name="db-classic-mysql-pool" class="ch.qos.logback.classic.db.DBAppender">  21         <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">  22             <dataSource class="org.apache.commons.dbcp.BasicDataSource">  23                 <driverClassName>com.mysql.jdbc.Driver</driverClassName>  24                 <url>jdbc:mysql://localhost:3306/logbackdb</url>  25                 <username>root</username>  26                 <password>qqqqqq</password>  27             </dataSource>  28         </connectionSource>  29     </appender>  

The table name is the same as previously described.

Now, let's write it here. If there are any supplements or needs to be added, we can update them later.

 

Summary on slf4j + logback integration to provide complete log records

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.