Skip to content

Commit

Permalink
Aggiunto log con logback
Browse files Browse the repository at this point in the history
  • Loading branch information
pintorig committed Feb 3, 2025
1 parent f7b5e9d commit e61d78c
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 67 deletions.
3 changes: 3 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ spring.datasource.initialization-mode=always

# ------------ LOGGING -------------------

# Define the log file location
logging.file.dir=/var/log/govpay

#spring.jpa.show-sql=true
#spring.jpa.properties.hibernate.format_sql=true

Expand Down
63 changes: 0 additions & 63 deletions src/main/resources/log4j2.xml

This file was deleted.

94 changes: 94 additions & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>

<configuration status="WARN" monitorInterval="30">

<Property name="LOG_PATTERN"
value="%d{ISO8601} %-5level [%t] %C{1.}: %msg%n%throwable" />

<!-- Property for the log file destination -->
<property name="LOG_DIR" value="${logging.file.dir:/var/log/govpay}"/>

<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%white(%d{ISO8601}) %highlight(%-5level) [%blue(%t)]
%yellow(%C{1}): %msg%n%throwable</Pattern>
</encoder>
</appender>

<appender name="core" class="ch.qos.logback.core.FileAppender">
<!-- Specify the file name or path -->
<file>${LOG_DIR}/govpay-gde_core.log</file>

<!-- Rolling policy (optional, to create new log files periodically) -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- Define the log file rollover interval (e.g., every day) -->
<fileNamePattern>${LOG_DIR}/govpay-gde_core-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory> <!-- Keep log files for the last 30 days -->
</rollingPolicy>

<!-- Log file encoder (formatting the log output) -->
<encoder>
<pattern>%white(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1}): %msg%n%throwable</pattern>
</encoder>
</appender>

<appender name="spring" class="ch.qos.logback.core.FileAppender">
<!-- Specify the file name or path -->
<file>${LOG_DIR}/govpay-gde_spring.log</file>

<!-- Rolling policy (optional, to create new log files periodically) -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- Define the log file rollover interval (e.g., every day) -->
<fileNamePattern>${LOG_DIR}/govpay-gde_spring-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory> <!-- Keep log files for the last 30 days -->
</rollingPolicy>

<!-- Log file encoder (formatting the log output) -->
<encoder>
<pattern>%white(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1}): %msg%n%throwable</pattern>
</encoder>
</appender>

<appender name="hibernate" class="ch.qos.logback.core.FileAppender">
<!-- Specify the file name or path -->
<file>${LOG_DIR}/govpay-gde_hibernate.log</file>

<!-- Rolling policy (optional, to create new log files periodically) -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- Define the log file rollover interval (e.g., every day) -->
<fileNamePattern>${LOG_DIR}/govpay-gde_hibernate-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory> <!-- Keep log files for the last 30 days -->
</rollingPolicy>

<!-- Log file encoder (formatting the log output) -->
<encoder>
<pattern>%white(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1}): %msg%n%throwable</pattern>
</encoder>
</appender>

<logger name="it.govpay" level="INFO" additivity="false">
<appender-ref ref="core" />
</logger>

<logger name="org.springframework" level="ERROR"
additivity="false">
<appender-ref ref="spring" />
</logger>

<!-- Hibernate -->
<logger name="org.hibernate" level="ERROR" additivity="false">
<appender-ref ref="hibernate" />
</logger>

<Logger name="org.hibernate.type.descriptor.sql" level="ERROR" additivity="false">
<AppenderRef ref="hibernate" />
</Logger>

<root level="ERROR">
<appender-ref ref="core" />
</root>

</configuration>

2 changes: 1 addition & 1 deletion src/test/java/it/govpay/gde/test/UC_3_AddEventoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class UC_3_AddEventoTest {

@BeforeEach
public void init() {
SimpleDateFormat sdf = new SimpleDateFormat(Costanti.PATTERN_DATA_JSON_YYYY_MM_DD_T_HH_MM_SS_SSS_Z);
SimpleDateFormat sdf = new SimpleDateFormat(Costanti.PATTERN_TIMESTAMP_3_YYYY_MM_DD_T_HH_MM_SS_SSSXXX);
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Rome"));
sdf.setLenient(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class UC_4_AddEventoFailTest {

@BeforeEach
public void init() {
SimpleDateFormat sdf = new SimpleDateFormat(Costanti.PATTERN_DATA_JSON_YYYY_MM_DD_T_HH_MM_SS_SSS_Z);
SimpleDateFormat sdf = new SimpleDateFormat(Costanti.PATTERN_TIMESTAMP_3_YYYY_MM_DD_T_HH_MM_SS_SSSXXX);
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Rome"));
sdf.setLenient(false);

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/it/govpay/gde/test/costanti/Costanti.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class Costanti {
public static final String EVENTI_PATH = API_BASE_PATH + "/eventi";
public static final String EVENTO_PATH = EVENTI_PATH + "/{id}";

public static final String PATTERN_DATA_JSON_YYYY_MM_DD_T_HH_MM_SS_SSS_Z = it.govpay.gde.costanti.Costanti.PATTERN_TIMESTAMP_3_YYYY_MM_DD_T_HH_MM_SS_SSSXXX;
public static final String PATTERN_TIMESTAMP_3_YYYY_MM_DD_T_HH_MM_SS_SSSXXX = it.govpay.gde.costanti.Costanti.PATTERN_TIMESTAMP_3_YYYY_MM_DD_T_HH_MM_SS_SSSXXX;

public static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern(PATTERN_DATA_JSON_YYYY_MM_DD_T_HH_MM_SS_SSS_Z);
public static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern(PATTERN_TIMESTAMP_3_YYYY_MM_DD_T_HH_MM_SS_SSSXXX);

public static final String ID_DOMINIO_1 = "12345678901";

Expand Down
37 changes: 37 additions & 0 deletions src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>

<configuration status="WARN" monitorInterval="30">

<Property name="LOG_PATTERN"
value="%d{ISO8601} %-5level [%t] %C{1.}: %msg%n%throwable" />

<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%white(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1}): %msg%n%throwable</Pattern>
</encoder>
</appender>

<logger name="it.govpay" level="ERROR" additivity="false">
<appender-ref ref="STDOUT" />
</logger>

<!-- Hibernate -->
<logger name="org.hibernate" level="OFF" additivity="false">
<appender-ref ref="STDOUT" />
</logger>

<logger name="org.springframework" level="ERROR" additivity="false">
<appender-ref ref="STDOUT" />
</logger>

<logger name="reactor.netty.http.client" level="OFF" additivity="false">
<appender-ref ref="STDOUT" />
</logger>

<root level="ERROR">
<appender-ref ref="STDOUT" />
</root>

</configuration>

0 comments on commit e61d78c

Please sign in to comment.