Use steps and basic technical knowledge of log4j logs

Source: Internet
Author: User

*

Step 1: Add jar package support
First, create a web project and import the jar package of log4j on the log4j official website to the lib directory of the project.

Step 2: Add and load the configuration file
Create a log4j. properties file in the src directory.
When log4j is started, it will find the source folder by default (the folder that stores the Java source code, including some package folders, and other files, even if SRC. if the xml configuration file does not exist, log4j will be searched. properties file. Then load the configuration. If the configuration file is correctly placed, you do not need to manually load the log4j configuration file in the program.
Propertyconfigurator. Configure ("log4j. properties") reads the path of the Project root directory by default. In this case, log4j. properties should be placed in the project directory.
A Java project has many configuration files. We recommend that you put all the configuration files in one folder, for example, in the config folder. Add the subdirectory name when reading these configuration files. Propertyconfigurator. Configure ("config/log4j. properties ");

The configuration file content can be:
### Set the logger level ###
Log4j. rootlogger = info, stdout, A1

### Appender. stdout output to the console ###
Log4j. appender. stdout = org. Apache. log4j. leleappender
Log4j. appender. stdout. Target = system. Out
Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout
Log4j. appender. stdout. layout. conversionpattern = [% 5 p] [bysxxglxt] % d {yyyy-mm-dd hh: mm: SS }: %-4r [%-5 p] [% T] (% F, % L)-% m % N

### Appender. A1 output to the log file ###
Log4j. appender. A1 = org. Apache. log4j. dailyrollingfileappender
Log4j. appender. a1.file =$ {Catalina. Base}/logs/test. Log ### the log file is in the logs folder of Tomcat ###
# Note the method of writing the above Log File relative to the application root directory path
Log4j. appender. a1.datepattern = '. 'yyyy-mm-dd'. Log'
Log4j. appender. a1.append = true
# Output logs at or above the debug level
Log4j. appender. a1.threshold = debug
Log4j. appender. a1.layout = org. Apache. log4j. patternlayout
Log4j. appender. a1.layout. conversionpattern = [% 5 p] [bysxxglxt] % d {yyyy-mm-dd hh: mm: SS }:%-4r [% T] (% F, % L) -% m % N

Step 3: Obtain the logger
// Obtain the logger name of the logger class.
Logger logger = logger. getlogger (this. getclass ());

Step 4: Use the logger to generate the log information. After the preceding three necessary steps are completed, you can easily use the log record statements of different priorities to insert anywhere you want to record the log.
Logger. Fatal ("this is the fatal information generated from testservlet! ");
Logger. Error ("this is an error message generated from testservlet! ");
Logger. Warn ("this is a warn message generated from testservlet! ");
Logger. debug ("this is a debug message generated from testservlet! ");
Logger.info ("this is the info generated from testservlet! ");



* ************* The following is the basic knowledge of log4j.

Log4j is an open-source sub-project under Jakarta. Through log4j, you can use a custom format to output debugging information and log information to one or more desired places.

Log4j Composition
Log4j includes three important components: Public logger, public interface appender, and public abstract class layout.
1. Public Logger
Logger is the core component of log processing and is responsible for generating log information. Outputs or screenshots of generated logs based on the configured log level.

Level description
Off close output of all log records
Fatal output will cause serious error event information that will cause application exit
Error output error event information that does not affect the system to continue running
Warn outputs Potential error event information
Info: output the running process information of the application system
Debug outputs application debugging information
All open output of all log records

2. appender
Appender controls the output of log record operations, it is used to specify the output destination of log information (such as console, file, rollback file, send log mail, output to database log table, and custom tag ).

Appender Name Description
Leleappender output to the console
Fileappender output to the specified file
Rollingfileappender outputs data to a file. When the file size reaches the specified size, a new file is generated.
Dailyrollingfileappender: output to a file. A new file is generated every day.
Writerappender sends log information to any specified place in the stream format
Jdbcappender output to the specified database
Smtpappender sends log information by email

3. Public abstract class Layout
Layout is responsible for formatting appender output
The layout format provided by log4j is as follows:

Layout Name Description
Simplelayout contains the log information level and information string
Patternlayout format log output based on the specified Conversion Mode
Htmllayout output in HTML form
Ttcclayout contains the log generation time, thread, category, and other information.

Output functions provided by log4j
Parameter Name Description
% M output the specified message content in the code
% P output priority, namely debug, info, warn, error, fatal
% R the number of milliseconds it takes to output the log information from application startup to output
% C output class, usually the full name of the class
% T name of the thread that outputs the log event
% N output a carriage return line break
% D date or time of the log output time point
% L location of log event output, including class name, thread, and number of lines in the code


Log4j configuration file
Log4j supports two configuration file formats: XML and Java attribute files (Key = value)

Example of log4j. Properties
Log4j. rootlogger = debug, A1, A2, A3, A4, A5, A6

# Apply to the console
Log4j. appender. A1 = org. Apache. log4j. leleappender
Log4j. appender. a1.layout = org. Apache. log4j. patternlayout
Log4j. appender. a1.layout. conversionpattern = % d {yyyy-mm-dd hh \: mm \: SS} [info] % m % N

# Apply to files
Log4j. appender. A2 = org. Apache. log4j. fileappender
Log4j. appender. a2.file =$ {Catalina. Home}/webapps/testlog/logging. Log
Log4j. appender. a2.append = true
Log4j. appender. a2.layout = org. Apache. log4j. patternlayout
Log4j. appender. a2.layout. conversionpattern = % d {yyyy-mm-dd hh \: mm \: SS} [info] % m % N


# Apply to file rollback
Log4j. appender. A3 = org. Apache. log4j. rollingfileappender
Log4j. appender. a3.file =$ {Catalina. Home}/webapps/testlog/logging. Log
Log4j. appender. a3.append = true
Log4j. appender. a3.maxfilesize = 1000kb
Log4j. appender. a3.maxbackupindex = 1
Log4j. appender. a3.layout = org. Apache. log4j. patternlayout
Log4j. appender. a3.layout. conversionpattern = % d {yyyy-mm-dd hh \: mm \: SS} [info] % m % N


# Send Logs to emails
Log4j. appender. A4 = org.apache.log4j.net. smtpappender
Log4j. appender. a4.buffersize = 10
Log4j. appender. a4.from = producer address
Log4j. appender. a4.smtphost = SMTP Server
Log4j. appender. a4.subject = Mail title
Log4j. appender. a4.to = inbox address
Log4j. appender. a4.layout = org. Apache. log4j. patternlayout
Log4j. appender. a4.layout. conversionpattern = % d {yyyy-mm-dd hh \: mm \: SS} [info] % m % N



# For Databases
Log4j. appender. A5 = org. Apache. log4j. JDBC. jdbcappender
Log4j. appender. a5.url = JDBC: mysql: // localhost: 3306/test
Log4j. appender. a5.driver = com. MySQL. JDBC. Driver
Log4j. appender. a5.user = root
Log4j. appender. a5.password = root
Log4j. appender. a5. SQL = insert into log4j (Message) values ('% d {yyyy-mm-dd hh \: mm \: SS} [info] % m % n ')
Log4j. appender. a5.layout = org. Apache. log4j. patternlayout
Log4j. appender. a5.layout. conversionpattern = % d {yyyy-mm-dd hh \: mm \: SS} [info] % m % N

# The application generates a log file every day.
Log4j. appender. A6 = org. Apache. log4j. daliyrollingfileappender
Log4j. appender. a6.file =$ {Catalina. Home}/webapps/testlog/logging. Log
Log4j. appender. a6.datepattern = yyyymmdd-hh '. Log'
Log4j. appender. a3.layout = org. Apache. log4j. patternlayout
Log4j. appender. a3.layout. conversionpattern = % d {yyyy-mm-dd hh \: mm \: SS} [info] % m % N

Use steps and basic technical knowledge of log4j logs

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.