SSH series: (2) Log record slf4j+log4j

Source: Internet
Author: User

(1) Add jar Package

(2) configuration

(3) test


1. Introduction of JAR Package

Set up the user Library, named slf4j


Jar packages that must be added


Log4j-1.2.17.jar

Slf4j-api-1.7.21.jar

Slf4j-log4j12-1.7.21.jar



Optional jar package (can be displayed in different colors for the console input log)



Color-loggers-1.0.4.1.jar


Note: you need to use the plug-in for Eclipse (ANSI Escape in Console)


Later needs to add the jar package (currently does not introduce this jar package, to the late error when the introduction)


Jcl-over-slf4j-1.7.21.jar


Note: The spring framework is used later, and spring uses the Commons logging jar package by default. In this project, using SLF4J instead of comons loggging, this jar package plays a very important role; If you use spring, you will get an error if you do not introduce this jar package.



2. Configuration

In the SRC directory, add the Log4j.properties file


First case: If not joined Color-loggers-1.0.4.1.jar

Log4j.rootlogger=info, console, file log4j.appender.console= org.apache.log4j.consoleappenderlog4j.appender.console.layout= org.apache.log4j.patternlayoutlog4j.appender.console.layout.conversionpattern=%d%p%c.%M ()-%m%n log4j.appender.file=org.apache.log4j.rollingfileappenderlog4j.appender.file.file=./logs/ mylog.loglog4j.appender.file.maxfilesize=5kblog4j.appender.file.maxbackupindex=100log4j.appender.file.layout= org.apache.log4j.patternlayoutlog4j.appender.file.layout.conversionpattern=%d%p%c.%M ()-%m%n

Second case: If you join Color-loggers-1.0.4.1.jar Note: Use with the ANSI Escape in console plugin is valid

Log4j.rootlogger=debug, console, file log4j.appender.console= com.colorlog.log4j.ansicolorconsoleappenderlog4j.appender.console.layout= org.apache.log4j.enhancedpatternlayoutlog4j.appender.console.layout.conversionpattern=%d%p%c.%M ()-%m% nlog4j.appender.console.fatalcolour={esc}[1;35mlog4j.appender.console.errorcolour={esc}[0; 31mlog4j.appender.console.warncolour ={esc}[0;33mlog4j.appender.console.infocolour ={esc}[0;  30mlog4j.appender.console.debugcolour={esc}[0;32mlog4j.appender.console.tracecolour={esc}[1;30m log4j.appender.file=org.apache.log4j.rollingfileappenderlog4j.appender.file.file=./logs/ mylog.loglog4j.appender.file.maxfilesize=5kblog4j.appender.file.maxbackupindex=100log4j.appender.file.layout= org.apache.log4j.patternlayoutlog4j.appender.file.layout.conversionpattern=%d%p%c.%M ()-%m%n



3. Testing

Package Com.rk.test.log;import Org.junit.test;import Org.slf4j.logger;import Org.slf4j.loggerfactory;public class HelloWorld {@Testpublic void Test () {Logger Logger = Loggerfactory.getlogger (Helloworld.class); Logger.debug ("Debug"); Logger.info ("info"); Logger.warn ("warn"); Logger.error ("Error");}}

Output

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/85/74/wKioL1ekBk_zHIVFAABLJ-n6NFs926.png "title=" Colorful_slf4j_log.png "alt=" Wkiol1ekbk_zhivfaablj-n6nfs926.png "/>

SSH series: (2) Log record slf4j+log4j

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.