J2EE hibernate 4 Learning Notes (XVIII) -- hibernate Advanced Configuration (log4j Configuration Log Framework)

Keywords: log4j Apache Session Hibernate

hibernate comes with a simple logging framework. Here we introduce a powerful logging framework log4j. Baidu Encyclopedia's introduction to log4j is very good. Most of the content below is copied from Baidu Encyclopedia.

Introduction to log4j (Baidu Encyclopedia)

Log4j is Apache An open source project, using Log4j, we can control the destination of log information delivery. Console , documents, GUI Components, even socket servers, NT Event logger, UNIX SyslogDaemon We can also control the output format of each log; by defining the level of each log information, we can control the generation process of the log more carefully. The most interesting thing is that these can be passed through a configuration file To configure flexibly without modifying the application code.

2. log4j configuration

1. Take the blog project as an example.

2. Add the jar package to the project:

3. Add the configuration file log4j.properties to the root directory of the project, as shown above.




Here is a description of the configuration (Baidu Encyclopedia)

Log4j. rootLogger = info, appender 1, appender 2: This sentence is to export log information with INFO level to appender 1 and appender 2 destinations. Appnder 1 and appender 2 are defined in the following code and can be named arbitrarily. Grades can be divided into OFF, FATAL, ERROR, WARN, INFO, etc. DEBUG ALL, if you configure OFF, you will not type any information, if you configure OFF INFO This shows only INFO, WARN, ERROR log Information, and DEBUG information will not be displayed, the specific explanation can refer to the third part of the definition of logger in the configuration file.

log4j.appender.appender1=org.apache.log4j.ConsoleAppender: This sentence defines what type of output is called appender1, which can be

org.apache.log4j.ConsoleAppender (console)
org.apache.log4j.FileAppender (file)
Org. apache. log4j. Daily Rolling File Appender (produces a log file every day)
org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size)
org.apache.log4j.WriterAppender (sending log information in streaming format to any designated place)
log4j.appender.appender2=org.apache.log4j.FileAppender: ibid.

log4j.appender.appender2.File=C:/logFile.txt: This sentence defines the output file named appender2 as C:/logFile.txt, which can be modified by itself.


org.apache.log4j.HTMLLayout HTML Tabular layout,
org.apache.log4j.PatternLayout (you can specify layout patterns flexibly)
org.apache.log4j.SimpleLayout (level and information containing log information) Character string),
org.apache.log4j.TTCCLayout (including the time of log generation, thread Categories, etc.
log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout: ibid.

log4j configuration information and other, you can refer to Baidu Encyclopedia.

4.log4j uses: to add related statements to the class to output the log:

Define attributes: static Logger logger = Logger.getLogger(LogDemo.class); //LogDemo is a related class
Modify the test file StudentTest.java to:
package com.test.service;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.test.model.Student;
import com.test.util.HibernateUtil;

public class StudentTest {

	private SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); // Get Session Factory
	private Session session;
	private Logger logger = Logger.getLogger(StudentTest.class);
	public void setUp() throws Exception {
		session=sessionFactory.openSession(); // Generate a session
	    session.beginTransaction(); // Open transaction

	public void tearDown() throws Exception {
		session.getTransaction().commit(); // Submission of affairs
	    session.close(); // Close session

	public void testSQLQuery() {
		String sql = "select * from t_student";
		Query query = session.createSQLQuery(sql).addEntity(Student.class);
		List studentList = query.list();
		Iterator it = studentList.iterator();
			Student s = (Student) it.next();
		logger.debug("This is a debug information");
		logger.info("This is a info information");
		logger.error("This is a error information");

Running this test method, you can see that the console output is:

You can see that the output of the console is different from that of the previous project. According to our configuration, we also export these information to the files of C disk:

Posted by xt3mp0r~ on Wed, 19 Dec 2018 17:30:04 -0800