Open source Logging tool in Java log4j

Source: Internet
Author: User

LOG: In addition to the ability to log exception information, you can also record the normal operation of the program key information.

Use log4j for log file Recording classic steps:

001. Create a lib folder in the project and copy the downloaded jar package to the folder

002. Right-click on the jar package that has been copied , and then perform the operation,

There will be an extra project in the project that introduces the external library.

003. create a name called log4j.properties file in the src Directory

004. write, paste the content of the configuration file that has been written, the encoding method can be amended. Right-click on the configuration file → properties →other→utf-8, then apply OK. If the error again, ignore him, click OK, again ctrl+s can.

005. just in the class where the main method is located, add the following code

The first is to create a static logger object

public static Logger Logger=logger.getlogger ("log4j");

Next, you can pass logger.debug ("Log Content");

Note: Through the decompile source code we know,GetLogger method, there are two overloads.

We are currently using overloads of incoming strings, and after a multi-data validation, any content passed in parentheses has no effect on the abstract factory producing an Logger instance. So you can use it with confidence.

where [level] is the log output levels, there are 5 levels:

FATAL 0
ERROR 3
WARN 4
INFO 6
DEBUG 7

  Output-level priority:

 All<trace<debug<info<warn<error<fatal<off

  Appender for the log output destination, LOG4J provides the following types of Appender:

Org.apache.log4j.ConsoleAppender (console),
Org.apache.log4j.FileAppender (file),
Org.apache.log4j.DailyRollingFileAppender (generates a log file every day),
Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size),
Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)


layout: Log output format, LOG4J provides the following types of layout:

Org.apache.log4j.HTMLLayout (Layout in HTML table Form),
Org.apache.log4j.PatternLayout (flexibility to specify layout mode),
Org.apache.log4j.SimpleLayout (contains the level of log information and the information string),
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log

Configuration file:

Log4j.rootlogger = Debug,a,b,c,

# Output to Console

LOG4J.APPENDER.A = Org.apache.log4j.ConsoleAppender

Log4j.appender.a.target = System.out

Log4j.appender.a.layout = Org.apache.log4j.PatternLayout

Log4j.appender.a.layout.conversionpattern =%p%t%c-%m%n

  

# Output to log file

Log4j.appender.b = Org.apache.log4j.DailyRollingFileAppender

Log4j.appender.b.file = Logs/log.log

Log4j.appender.b.append = True

Log4j.appender.b.threshold = DEBUG # output ebug levels above the log

Log4j.appender.b.layout = Org.apache.log4j.PatternLayout

Log4j.appender.b.layout.conversionpattern =%p%t%c-%m%n

# Save exception information to a separate file

LOG4J.APPENDER.C = Org.apache.log4j.DailyRollingFileAppender

Log4j.appender.c.file = logs/error.log # Exception Log file name

Log4j.appender.c.append = True

Log4j.appender.c.threshold = ERROR #只输出ERROR级别以上的日志

Log4j.appender.c.layout = Org.apache.log4j.PatternLayout

Log4j.appender.c.layout.conversionpattern =%p%t%c-%m%n

Problem:

How do I log logs using log4j?

Parsing: Introducing JAR Packages

Create a Properties file

Writing log4j Configuration Code

log4j Logging

Open source Logging tool in Java 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.