Tell me how you're going to configure MAVEN project dependencies and configuration log4j

Source: Internet
Author: User
Tags log4j

The whole idea Community Edition, I hope you also use the Community version. Features are not much less. Develop the habit of paying with the genuine, we are all programmers. Respect others ' success is respect oneself.
1. Create a new Mavena project
2, write several classes, and then create a new test class.

At this time Open Module Setting, to see libaries, found that has automatically introduced several JUnit maven dependency
At this moment, we want to use the log output, to slf4j, how to do
public class Hello {public
    static Logger Logger = Loggerfactory.getlogger (hello.class);
    public void xxx (String abc) {
        logger.error (ABC);

    }
} "

3, first put the above code about the log part of the code commented out
to Pom.xml, to add dependencies
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactid>slf4j-log4j12</ artifactid>
        <version>1.7.21</version>
        <scope>compile</scope>
    </ Dependency>

The above code, I input groupid, code hints automatically pop-up, the second is automatically pop-up. Version number: I found a random on the internet, as long as the right can. Scope: I chose to compile. can choose their own.

4, run test at this time, test can pass. But there is a clear hint that the appender can not be found. Because log4j is not configured, it is also configured.
5, create a new Log4j.propertier file, placed under the resource file (other tutorials can be placed directly under the SRC package, I tried not to), then run the test can be.

# # # set log Levels # # # Log4j.rootlogger = Debug, stdout, D, E # # # output to console # # # # log4j.appender.stdout = org.apache.log4j . Consoleappender Log4j.appender.stdout.Target = System.out Log4j.appender.stdout.layout = Org.apache.log4j.PatternLayout # # #log4j. Appender.stdout.layout.ConversionPattern =%d{absolute}%5p%c{1}:%l-%m%n # #
# Output to log file # # # LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender Log4j.appender.d.file = Logs/log.log Log4j.appender.d.append = True log4j.appender.d.threshold = Debug # # Output debug level above log log4j.appender.d.layout = Org.apache . log4j. Patternlayout Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n # # # Save the Debug letter To a separate file # # LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender log4j.appender.d.file = logs/error.log # #
Exception Log file name Log4j.appender.d.append = True Log4j.appender.d.threshold = error # # Output only logs above the error level!!! Log4j.appender.d.layout = Org.apache.log4j.PatternLayout Log4j.appender.d.layout.conversionpattErn =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n # # # Save exception information to a separate file # # # LOG4J.APPENDER.E = Org.apache.log4j.DailyRo Llingfileappender Log4j.appender.e.file = Logs/error.log Log4j.appender.e.append = True Log4j.appender.E.Threshold = ERROR log4j.appender.e.layout = org.apache.log4j.PatternLayout Log4j.appender.e.layout.conversionpattern =%d{ Yyyy-mm-dd HH:MM:SS} [%t:%r]-[%p]-%m%n

The logs are now working correctly

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.