BAEL-3387 Hibernate - Different Levels Of Logging (#8326)
* Added module for article * Update pom.xml * Delete pom.xml * Update and rename ForecastProcessorTest.java to ForecastProcessorUnitTest.java * Updated code as per review comments * Delete .gitignore * Update pom.xml * BAEL-2904 Moved files * BAEL-2904 Deleted files from previous project * BAEL-2904 Moved classes for method reference article * Update pom.xml * BAEL-2904 Updated README.md * First draft * BAEL-3300 Added more tests * BAEL-3300 Removed unused imports * BAEL-3300 Run dos2unix on README.md * BAEL-3387 Added entity and test classes * BAEL-3387 Added logging configuration * BAEL-3387 Corrected log4j and log4j2 config
This commit is contained in:
parent
20bd58397d
commit
339913ef05
@ -27,6 +27,12 @@
|
|||||||
<artifactId>h2</artifactId>
|
<artifactId>h2</artifactId>
|
||||||
<version>1.4.200</version>
|
<version>1.4.200</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.baeldung.hibernate.logging;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Employee {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
private String employeeNumber;
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Employee() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Employee(String name, String employeeNumber) {
|
||||||
|
this.name = name;
|
||||||
|
this.employeeNumber = employeeNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmployeeNumber() {
|
||||||
|
return employeeNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmployeeNumber(String employeeNumber) {
|
||||||
|
this.employeeNumber = employeeNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
<?xml version='1.0' encoding='utf-8'?>
|
||||||
|
<!DOCTYPE hibernate-configuration PUBLIC
|
||||||
|
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||||
|
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||||
|
|
||||||
|
<hibernate-configuration>
|
||||||
|
|
||||||
|
<session-factory>
|
||||||
|
<property name="connection.driver_class">org.h2.Driver</property>
|
||||||
|
<property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1</property>
|
||||||
|
<property name="connection.username">sa</property>
|
||||||
|
<property name="connection.password"/>
|
||||||
|
|
||||||
|
<property name="connection.pool_size">1</property>
|
||||||
|
|
||||||
|
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
|
||||||
|
|
||||||
|
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
|
||||||
|
|
||||||
|
<property name="hibernate.generate_statistics">true</property>
|
||||||
|
|
||||||
|
<property name="hbm2ddl.auto">create</property>
|
||||||
|
|
||||||
|
<mapping class="com.baeldung.hibernate.logging.Employee"/>
|
||||||
|
</session-factory>
|
||||||
|
|
||||||
|
</hibernate-configuration>
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.baeldung.hibernatelogging;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.boot.MetadataSources;
|
||||||
|
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||||
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
|
import org.hibernate.query.Query;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.hibernate.logging.Employee;
|
||||||
|
|
||||||
|
public class HibernateLoggingIntegrationTest {
|
||||||
|
|
||||||
|
private SessionFactory sessionFactory;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws IOException {
|
||||||
|
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure("hibernate-logging.cfg.xml")
|
||||||
|
.build();
|
||||||
|
try {
|
||||||
|
sessionFactory = new MetadataSources(registry).buildMetadata()
|
||||||
|
.buildSessionFactory();
|
||||||
|
Session session = sessionFactory.openSession();
|
||||||
|
session.beginTransaction();
|
||||||
|
session.save(new Employee("John Smith", "001"));
|
||||||
|
session.getTransaction()
|
||||||
|
.commit();
|
||||||
|
session.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail(e);
|
||||||
|
StandardServiceRegistryBuilder.destroy(registry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAllEmployeesAreSelected_ThenSuccess() {
|
||||||
|
Query<Employee> query = sessionFactory.openSession().createQuery("from com.baeldung.hibernate.logging.Employee", Employee.class);
|
||||||
|
List<Employee> deptEmployees = query.list();
|
||||||
|
Employee deptEmployee = deptEmployees.get(0);
|
||||||
|
assertEquals("John Smith", deptEmployee.getName());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
|
||||||
|
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
|
||||||
|
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
|
||||||
|
<layout class="org.apache.log4j.PatternLayout">
|
||||||
|
<param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
|
||||||
|
</layout>
|
||||||
|
</appender>
|
||||||
|
<logger name="org.hibernate">
|
||||||
|
<level value="info" />
|
||||||
|
</logger>
|
||||||
|
<logger name="org.hibernate.SQL">
|
||||||
|
<level value="debug" />
|
||||||
|
</logger>
|
||||||
|
<logger name="org.hibernate.type.descriptor.sql">
|
||||||
|
<level value="trace" />
|
||||||
|
</logger>
|
||||||
|
<logger name="org.hibernate.stat">
|
||||||
|
<level value="debug" />
|
||||||
|
</logger>
|
||||||
|
<root>
|
||||||
|
<priority value ="info" />
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</log4j:configuration>
|
@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Configuration status="INFO">
|
||||||
|
<Appenders>
|
||||||
|
<Console name="console" target="SYSTEM_OUT">
|
||||||
|
<PatternLayout
|
||||||
|
pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
|
||||||
|
</Console>
|
||||||
|
</Appenders>
|
||||||
|
<Loggers>
|
||||||
|
<Logger name="org.hibernate" level="info"/>
|
||||||
|
<Logger name="org.hibernate.SQL" level="debug"/>
|
||||||
|
<Logger name="org.hibernate.type.descriptor.sql" level="trace"/>
|
||||||
|
<Logger name="org.hibernate.stat" level="debug" />
|
||||||
|
<Root level="info" additivity="false">
|
||||||
|
<AppenderRef ref="console" />
|
||||||
|
</Root>
|
||||||
|
</Loggers>
|
||||||
|
</Configuration>
|
@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{yyyy-MM-dd HH:mm:ss} | %-5p | [%thread] %logger{5}:%L - %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="org.hibernate" level="INFO" />
|
||||||
|
<logger name="org.hibernate.SQL" level="DEBUG" />
|
||||||
|
<logger name="org.hibernate.type.descriptor.sql" level="TRACE" />
|
||||||
|
<logger name="org.hibernate.stat" level="DEBUG" />
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
Loading…
x
Reference in New Issue
Block a user