How to write logs to the database with log4j

Source: Internet
Author: User
Tags log4j

Log4j is an excellent open source logging project, we can not only customize the format of the output log, but also define the destination of the log output, such as: screen, text file, database, even through the socket output. This section uses the MySQL database to mainly describe how to enter log information into the database.
Using log4j to write the log to the database is mainly used under the log4j package Jdbcappender class, which provides the ability to write log information asynchronously to the data, we can directly use this class to write our log information to the database, you can also extend the Jdbcappender class, is to develop the Jdbcappender class as a base class two times to obtain a new subclass with all Jdbcappender class properties and behaviors.
Below you can use the Jdbcappender class directly under the log4j package to explain how log4j writes log information to the database through an instance:
Requirements: In the process of software development, we need to record debug information, operation information and so on, so that the following audit, the log information including user ID, user name, Operation class, Path, method, operation time, log information.

Design idea: We use the Jdbcappender class to directly insert log information into the database, so only need to configure this class in the configuration file can be, to obtain user information needs to be implemented with a filter (if you do not need the user's information, there is no need to design filters, in fact, most of the situation is to need these user information , especially in Web application development). In the log information to obtain user information, is through the filter request or Session object to achieve, but how to upload user information to log4j, log4j provide us with MDC this log4j very useful class, It is used to store contextual information about the application (context infomation), which facilitates the use of these contextual information in log. The MDC internally uses a map-like mechanism to store information, and contextual information is stored separately for each thread, and the difference is that the information is stored in "map" with their key value. The corresponding method is: "Mdc.put (key, value); Mdc.remove (key); Mdc.get (key); ”。 When configuring Patternlayout, use:%x{key} to output the corresponding value). With the MDC, we can get the user information in the filter first, and then use the Mdc.put ("key") method, log to output the corresponding value by%x{key} when executing the SQL statement.

" Download Javaweb project "

How to write logs to the database with 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.