Configuration and use of Log4j

Source: Internet
Author: User

Log4j introduction:

Log4j is a powerful logging tool that records operation information in the system and helps administrators analyze and manage related data in the background. You can record the data console, files, interface servers, and GUI components. You can configure or flexibly record the log format through your own configuration to achieve data analysis and system analysis. Well, let's just talk about it. configure a simple log4j log system first.

Bytes --------------------------------------------------------------------------------------

Log4j configuration instructions

The jar package, configuration, and two configuration files of log4j. One is a single log of log4j and the other is multiple. You can configure them according to the document in it.

Free in http://linux.bkjia.com/

The username and password are both www.bkjia.com

The specific download directory is available for configuration and usage of Log4j/July 15, 2014/July 1/July 15, January

For the download method, see

Bytes --------------------------------------------------------------------------------------

Simple use of Log4j:

1. First, import the jar package of log4j: log4j-1.2.16.jar package, put it under lib.

2. Configure the log4j file named log4j. properties. Put this file under the src directory, for example:

3. Configure the log4j file with the following code:

Log4j. rootLogger = debug, B
Log4j. appender. B = org. apache. log4j. DailyRollingFileAppender
Log4j. appender. B. File = G:/test/error. log
Log4j. appender. B. DatePattern = '. 'yyyy-MM-dd
Log4j. appender. B. Append = true
Log4j. appender. B. Threshold = ERROR
Log4j. appender. B. layout = org. apache. log4j. PatternLayout
Log4j. appender. B. layout. ConversionPattern = % n % d % p [% c]-% m
Log4j. appender. B. MaxFileSize = 1024KB

Note:

Log4j. rootLogger = debug, B // This is a global variable, and B must be configured. Obviously, you can see the usage of this alias below.

Log4j. appender. B = org. apache. log4j. DailyRollingFileAppender // indicates the output mode, indicating that a log file is generated every day.

Log4j. appender. B. File = G:/test/error. log // indicates that the log is output to the test folder under the G drive letter. In this way, logs will be generated under the test folder on the G disk.

Log4j. appender. b. datePattern = '. 'yyyy-MM-dd // indicates that a date format name is added to the end of the file. In this way, every log generated every day has a date name.

Log4j. appender. B. Append = true // indicates that the log is appended, not overwritten. If this parameter is not set, only one log record may appear in a log.

Log4j. appender. B. Threshold = ERROR // indicates the log level. log4j has five levels, which are followed by comments.

Log4j. appender. B. layout = org. apache. log4j. PatternLayout // You can flexibly set the layout mode.

Log4j. appender. B. layout. ConversionPattern = % n % d % p [% c]-% m // layout mode. logs are displayed in various formats. 19:29:34, 984 ERROR [errorlog]-msg

Log4j. appender. B. MaxFileSize = 1024KB // indicates the maximum number of log files. When this limit is exceeded, a new file is created.

4. compile java

The Java code is as follows:

Package com. zy. other;
Import org. apache. log4j. Logger;
Import org. apache. log4j. PropertyConfigurator;
Public classLog {
// Logger instance
PublicLogger loger;
// Encapsulate the Log class into a single instance mode, independent of other classes. In the future, you only need to obtain the Log instance for easy use.
PrivatestaticLog errorlog;
PrivatestaticString str;
// Constructor, used to initialize the attributes required by Logger Configuration
PrivateLog ()
{
// Obtain the current directory path
String filePath = this. getClass (). getResource ("/"). getPath ();
// Find the directory where the log4j. properties configuration file is located (created)
FilePath = filePath. substring (1). replace ("bin", "src ");
// Obtain the log-type loger instance
Loger = Logger. getLogger (this. getClass ());
// Path of the configuration file required by loger
PropertyConfigurator. configure ("/" + filePath + "log4j. properties ");

Str = filePath;
}
PublicstaticLog getLoger ()
{
If (errorlog! = Null)
Return errorlog;
Else
Return new Log ();
}
// Test the Function
Publicstaticvoidmain (String args [])
{
Log log = Log. getLoger ();
System. out. println (str );
Log. loger. error ("msg ");
}
Publicstaticvoidwritelog (Exception e ){
Log log = Log. getLoger ();
System. out. println (str );
Log. loger. error (e. getMessage (), e );
}
}

Run the main function to create a log in your corresponding file.

Note: The preceding backup file is generated every day, and there is no date on the day. The log file of the previous day will only appear on the second day.

 

Log4j getting started

Configure Log4j in Hibernate to display SQL Parameters

Log4j learning notes (1) _ Log4j basics & configuration item Parsing

Log4j learning notes (2) _ Log4j configuration example & integrate Log4j with Spring

  • 1
  • 2
  • 3
  • Next Page

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.