Skip to content

Commit

Permalink
add logback, setting log jdbc agent
Browse files Browse the repository at this point in the history
  • Loading branch information
altaec23 committed Apr 9, 2022
1 parent e9f1dbd commit dcfef1d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
13 changes: 7 additions & 6 deletions HiberModule/src/main/java/core/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

Expand All @@ -14,22 +16,21 @@
*/
public class Main {

/* public static final String URL = "jdbc:postgresql://localhost:5432/postgres";
public static final String USER = "postgres";
public static final String PASSWORD = "postgres";*/

private static final Logger log = LoggerFactory.getLogger(Main.class);

public static void main(String[] args) {

log.info("Hello");
var serviceRegistry = new StandardServiceRegistryBuilder()
.configure("hibernate.cfg.xml").build();
var metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build();

SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();

createRecord(User.builder().name("name").build(), sessionFactory);

var session = sessionFactory.openSession();
List<City> list = session.createQuery("from model.City", City.class).list();
System.out.println(list);
List<City> list = session.createQuery("from core.model.City", City.class).list();
}

private static <T> T createRecord(T record, SessionFactory factory) {
Expand Down
6 changes: 3 additions & 3 deletions HiberModule/src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>

<!-- Database Connection Settings -->
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property>
<property name="hibernate.connection.driver_class">net.sf.log4jdbc.sql.jdbcapi.DriverSpy</property>
<property name="hibernate.connection.url">jdbc:log4jdbc:postgresql://localhost:5432/postgres</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">postgres</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<!-- <property name="show_sql">true</property> -->
<mapping class="core.model.CityType"/>
<mapping class="core.model.City"/>
<mapping class="core.model.User"/>
Expand Down
1 change: 1 addition & 0 deletions HiberModule/src/main/resources/log4jdbc.log4j2.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
21 changes: 21 additions & 0 deletions HiberModule/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT"/>
</root>
<logger name="log4jdbc.log4j2" level="error" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<logger name="jdbc.audit" level="error"/>

</configuration>

0 comments on commit dcfef1d

Please sign in to comment.