Write logs to the database with log4j

Source: Internet
Author: User
Tags log4j

The following are the configuration parameters in log4j:

%M the message specified in the output code

%p output priority, i.e. Debug,info,warn,error,fatal

%r output the number of milliseconds that the log information is consumed from the application boot to output

%t output The name of the thread that generated the log event;

%n output a carriage return newline character, Windows platform is "\ r \ n", Unix platform is "\ n"

%d the date or time of the output log time, the default format is ISO8601, or the format can be specified later,
For example:%d{yyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2008 22:10:28:832

%c output belongs to the class, which is usually the full name of the class, such as "Inotes.default";

%l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code

%l the number of lines in the output code;

%F the file name in the output code;

pay attention to case

In Java Engineering, the log file (log4j.properties) is placed in the SRC directory, in the Web project, the log file (log4j.properties) is also placed in the SRC root directory.


The code in the action is as follows:

Java code
  1. Public class Loginuseraction extends Action {
  2. private Userdao Userdao;
  3. private static Logger log = Logger.getlogger (loginuseraction.   Class);
  4. /** 
  5. * Get the Userdao object
  6. * @param Userdao
  7. */
  8. public void Setuserdao (Userdao userdao) {
  9. This.userdao = Userdao;
  10. }
  11. Public Actionforward Execute (actionmapping mapping, actionform form,
  12. HttpServletRequest request, HttpServletResponse HttpServletResponse)
  13. throws Exception {
  14. //sets the character set of the request
  15. Request.setcharacterencoding ("Utf-8");
  16. Loginuserform loginform = (loginuserform) Form;
  17. Log.warn ("User:" +loginform.getusername () +"Login" +"+" IP: "+request.getremoteaddr ());
  18. //Call method to get a loginuserform
  19. LoginForm = (loginuserform) userdao.getaccount (loginform);
  20. return Mapping.findforward ("Success");
  21. }
  22. }

private static Logger log = Logger.getlogger (Loginuseraction.class);
Log.warn ("User:" +loginform.getusername () + "login" + "+" IP: "+request.getremoteaddr ());


Note the red Word, these two lines are mainly associated with the log, the first red line is to establish a log object, the second red line is to write to the database log.

Code in Log4j.properties:

Java code
    1. Log4j.rootlogger=warn,database
    2. #直接使用log4j包中的文件
    3. Log4j.appender.database=org.apache.log4j.jdbc.jdbcappender
    4. #与数据库建立连接
    5. Log4j.appender.database.url=jdbc:mysql://localhost:3306/test
    6. Log4j.appender.database.driver=com.mysql.jdbc.driver
    7. Log4j.appender.database.user=root
    8. Log4j.appender.database.password=icy
    9. #指定要插入数据库的格式, the format of the specific parameters to look at the beginning of the document
    10. Log4j.appender.database.sql=insert to Log4j (Log_date, Log_level, location, message) VALUES ('%d{iso8601} ', ' %p ', '%c,%l ', '%m ')


This makes it possible to write logs to the database.

Original: http://blog.csdn.net/zhengbo0/article/details/38731617

Write logs to the database with log4j

Related Article

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.