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
- Public class Loginuseraction extends Action {
- private Userdao Userdao;
- private static Logger log = Logger.getlogger (loginuseraction. Class);
- /**
- * Get the Userdao object
- * @param Userdao
- */
- public void Setuserdao (Userdao userdao) {
- This.userdao = Userdao;
- }
- Public Actionforward Execute (actionmapping mapping, actionform form,
- HttpServletRequest request, HttpServletResponse HttpServletResponse)
- throws Exception {
- //sets the character set of the request
- Request.setcharacterencoding ("Utf-8");
- Loginuserform loginform = (loginuserform) Form;
- Log.warn ("User:" +loginform.getusername () +"Login" +"+" IP: "+request.getremoteaddr ());
- //Call method to get a loginuserform
- LoginForm = (loginuserform) userdao.getaccount (loginform);
- return Mapping.findforward ("Success");
- }
- }
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
- Log4j.rootlogger=warn,database
- #直接使用log4j包中的文件
- Log4j.appender.database=org.apache.log4j.jdbc.jdbcappender
- #与数据库建立连接
- Log4j.appender.database.url=jdbc:mysql://localhost:3306/test
- Log4j.appender.database.driver=com.mysql.jdbc.driver
- Log4j.appender.database.user=root
- Log4j.appender.database.password=icy
- #指定要插入数据库的格式, the format of the specific parameters to look at the beginning of the document
- 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