Log4j Plus Log Method-Go

Source: Internet
Author: User

How to log logs using log4j
First step: Add the jar file used by log4j in the project
1: Project > Properties: Popup Project's Properties window
2:java construction Path; add an external jar: pops up the window that selects the jar
3: By selecting the Jar window, locate the Log4j-1.2.x.jar and confirm
4: Go back to the project's Properties window and click OK
Step two: Create a log4j.properties file
1: Select the project to use log4j; right click on src;? Other: Pop-up Selection wizard window
2: In the Select Wizard window, select General?> file?> Next: Pop Up the new file window
3: In the New File window, enter the filename log4j.properties? Complete: Create work ends
Step three: Log information using log4j

Import Org.apache.log4j.Logger;

public class Log4j {
public static void Main (string[] args) {
Logger Logger = Logger.getlogger (AccpTeacherLog4j.class.getName ());//Get logger, this logger will be responsible for controlling log information
try {
Logger.debug ("Sets the number of the instructor. ");//Use the Debug, info method of the Logger object to output the log information
} catch (IllegalArgumentException ex) {
Logger.info (Ex.getmessage ());//output log information using the Debug, info method of the Logger object
}
}
}

Fourth step: Write the Log4j.properties file, configure the log information

# # # to output log information to the console # # #
log4j.appender.stdout=org.apache.log4j.consoleappender//log information will be written to the console
log4j.appender.stdout.target=system.out//Information printed on System.out
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.STDOUT.LAYOUT.CONVERSIONPATTERN=%D{YYYY-MM-DD HH:mm:ss}%m%n//Specify output format: Display date and log information

# # # to output log information to file: Accp.log # # #
log4j.appender.file=org.apache.log4j.fileappender//log information will be written to the file
log4j.appender.file.file=accp.log//Specifies the file name of the log output
Log4j.appender.file.layout=org.apache.log4j.patternlayout
Log4j.appender.file.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss}%l%m%n//Specify output format: Display date, log occurrence location and log information

# # # Set priority, and Output source # # #
Log4j.rootlogger=debug, stdout, file//set the priority level to debug,
Logs are output to multiple output sources

Note: Priority levels from high to low are error, WARN, INFO, DEBUG
Here, if the priority level is set to info, then the log information printed using the Debug method will not be output

Self-summary: terminal, httpclient print no post parameters, if you need to print the specific post parameters, you need to output to the file:

log4j.rootlogger=alllog4j.appender.file=org.apache.log4j.fileappenderlog4j.appender.file.file=d:/ accp.loglog4j.appender.file.layout=org.apache.log4j.patternlayoutlog4j.appender.file.layout.conversionpattern= %5p [%c]%m%nlog4j.appender.f=org.apache.log4j.fileappenderlog4j.appender.f.file=wire.loglog4j.appender.f.layout =org.apache.log4j.patternlayoutlog4j.appender.f.layout.conversionpattern =%5p [%c]%m% Nlog4j.logger.httpclient.wire=all, Flog4j.logger.org.apache.commons.httpclient=all, stdout

The specific log can be consulted: http://hc.apache.org/httpclient-3.x/logging.html

Log4j Plus Log Method-Go

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.