я только начал изучать log4j и когда я запускаю проект все работает никаких ошибок однако у меня не создаются файлы с данными, работаю в eclipse ide
log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC
"-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>
<appender name="console" class="org.apche.log4j.ConsoleAppender">
<param name="target" value="System.out"/>
<layout class="org.apche.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{HH:mm:ss,SSS}] %p [%c: %L] %m%n"/>
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="CrunchifySpringMVCFramework.log"/>
<param name="append" value="true"/>
<param name="maxFileSize" value="1KB"/>
<param name="maxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss, SSS}] %p [%c: %L] %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ALL"/>
</filter>
</appender>
<root>
<level value="info"/>
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>
entity, user класс:
import jakarta.persistence.*;
import lombok.*;
import package1.Birthday;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "users")
public class User {
@Id
private String username;
private String firstname;
private String lastname;
@Column(name = "birth_date")
private Birthday birthDate;
@Enumerated(EnumType.STRING)
private Role role;
}
main класс:
public class HibernateRunner{
private static final Logger log = LoggerFactory.getLogger(HibernateRunner.class);
public static void main(String[] args) {
var user = User.builder()
.username("Ivan@gmail.com")
.firstname("Ivan")
.lastname("Ivanov")
.build();
log.info("User entity is in transient state, object: {}", user);
try ( SessionFactory sessionFactory = HibernateUtil.buildSessionFactory()){
Session session1 = sessionFactory.openSession();
try(session1){
var transaction = session1.beginTransaction();
log.trace("Transaction is created, {}", transaction);
session1.saveOrUpdate(user);
log.trace("User is in persisent state: {}, session {}", user, session1);
session1.getTransaction().commit();
}
log.warn("User is in detached state: {}, session is closed {}", user, session1);
}catch(Exception exception) {
log.error("Exception occurred", exception);
throw exception;
}
}
}
pom.xml:
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.12</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.12</version>
<scope>test</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.4.4.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
</dependencies>