Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIS-26: Implemented basic logging #7

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ jobs:
# # Skip tests since they were already run.
# run: mvn package -DskipTests

- name: Save the Jar file test artifact
- name: Upload the generated application jar file artifact
uses: actions/[email protected]
with:
name: smart-inventory-0.0.1-SNAPSHOT
path: target/smart-inventory-0.0.1-SNAPSHOT.jar
retention-days: 7

- name: Upload the test artifacts
- name: Upload the test report artifacts
uses: actions/[email protected]
if: ${{ always() }}
with:
Expand All @@ -83,11 +83,20 @@ jobs:
if-no-files-found: error
retention-days: 7

- name: Upload the HTML report test artifacts
- name: Upload the HTML test report artifacts
uses: actions/[email protected]
if: ${{ always() }}
with:
name: html-test-report
path: "**/target/site/**"
if-no-files-found: error
retention-days: 7

- name: Upload the application and test log file artifacts
uses: actions/[email protected]
if: ${{ always() }}
with:
name: application-and-test-logs
path: "**/logs/**"
if-no-files-found: error
retention-days: 7
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ build/

### VS Code ###
.vscode/

### Generated Log Files ###
src/main/java/sg/com/smartinventory/logfile
logs/**
10 changes: 10 additions & 0 deletions src/main/java/sg/com/smartinventory/SmartInventoryApplication.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package sg.com.smartinventory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SmartInventoryApplication {
// Name this according to your class name.
private static final Logger app_logger = LoggerFactory.getLogger(SmartInventoryApplication.class);

public static void main(String[] args) {
app_logger.info("Starting SmartInventoryApplication. ");

SpringApplication.run(SmartInventoryApplication.class, args);

app_logger.info("Exiting SmartInventoryApplication. ");
}
}
23 changes: 21 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
spring.application.name=smart-inventory

# Server Configuration.
# The default server port is 8080.
server.port=9090

# Database Configuration.
# PostgreSQL
spring.datasource.url=jdbc:postgresql://localhost:5432/smart_inventory
# for WSL, use postgres
# for Mac, use your Mac username
# For WSL, use postgres.
# For Mac, use your Mac username.
spring.datasource.username=postgres
# Password can be blank if we set it to trust in pg_hba.conf
spring.datasource.password=
Expand All @@ -14,6 +17,22 @@ spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create
# This can be used to update tables.
# spring.jpa.hibernate.ddl-auto=update

# Logging configuration.
# Application logging configuration.
# logging.level.root=INFO
# logging.file.name=logs/application.log
# Database logging configuration.
# The Spring/Hibernate classes, which generate SQL statements and set the parameters, already contain the code for logging them.
# However, the level of those log statements is set to DEBUG and TRACE respectively, which is lower than the default level in Spring Boot — INFO.
# By adding these properties, we are just setting those loggers to the required level.
# For logging statements.
# logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG
# For logging parameters of prepared statements.
# logging.level.org.springframework.jdbc.core.StatementCreatorUtils=TRACE
# Set up a profile-specific configuration for tests.
# logging.config=classpath:logback-testloglevel.xml

# SpringDoc Configuration.
# The OpenAPI descriptions are at /v3/api-docs, which is the default path: http://localhost:8080/v3/api-docs.
# For a custom path of the OpenAPI documentation in Json format,
Expand Down
68 changes: 68 additions & 0 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- Define a common variable -->
<property name="AppLogPath" value="./logs" />
<!-- Define a Console Appender -->
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<!-- Define the log message format -->
<encoder>
<pattern>
%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<!-- Define a Console2 Appender -->
<appender name="Console2" class="ch.qos.logback.core.ConsoleAppender">
<!-- Define the log message format -->
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}):
%msg%n%throwable
</pattern>
</layout>
</appender>
<!-- Define a File Appender -->
<appender name="File" class="ch.qos.logback.core.FileAppender">
<!-- Specify path to log file -->
<!-- <file>logs/application.log</file> -->
<file>${AppLogPath}/application.log</file>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${AppLogPath}/application.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- Rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${AppLogPath}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- Set root log level to "INFO" -->
<root level="info">
<!-- Reference the "Console" appender for console output -->
<appender-ref ref="Console" />
<!-- <appender-ref ref="Console2" /> -->
<!-- Reference the "File" appender for file output -->
<appender-ref ref="File" />
<!-- Reference the "RollingFile" appender for file output -->
<!-- <appender-ref ref="RollingFile" /> -->
</root>
<!-- LOG "sg.com.smartinventory*" at "TRACE" level -->
<!--
<logger name="sg.com.smartinventory" level="trace" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>
-->
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -15,6 +16,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import sg.com.smartinventory.SmartInventoryApplication;
import sg.com.smartinventory.entities.Customer;

@SpringBootTest
Expand All @@ -26,9 +28,14 @@ public class CustomerControllerTest {
@Autowired
private ObjectMapper objectMapper;

// Name this according to your class name.
private static final Logger test_logger = LoggerFactory.getLogger(CustomerControllerTest.class);

@DisplayName("Create customer")
@Test
public void createCustomerTest() throws Exception {
test_logger.info("Starting test: createCustomerTest. ");

// Step 1: Create a Customer object
Customer newCustomer = Customer.builder().firstName("Jackie").lastName("Chan").country("Hong Kong")
.address("123 HK St")
Expand All @@ -48,5 +55,7 @@ public void createCustomerTest() throws Exception {
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.firstName").value("Jackie"))
.andExpect(jsonPath("$.lastName").value("Chan"));

test_logger.info("Ending test: createCustomerTest. ");
}
}
6 changes: 6 additions & 0 deletions src/test/resources/application-logging-test.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Test logging configuration.
# logging.config=classpath:logback-testloglevel.xml

# Add a Spring profile to our test by using the ActiveProfiles annotation, for example @ActiveProfiles("logging-test").
# logging.level.sg.com.smartinventory.testloglevel=TRACE
# logging.level.root=ERROR
67 changes: 67 additions & 0 deletions src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<configuration>
<include resource="/org/springframework/boot/logging/logback/base.xml" />
<!-- Define a common variable -->
<property name="TestLogPath" value="./logs" />
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<!-- Define the log message format -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<appender name="Console2" class="ch.qos.logback.core.ConsoleAppender">
<!-- Define the log message format -->
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}):
%msg%n%throwable
</pattern>
</layout>
</appender>
<!-- Define a File Appender -->
<appender name="File" class="ch.qos.logback.core.FileAppender">
<!-- Specify path to log file -->
<!-- <file>logs/test.log</file> -->
<file>${TestLogPath}/test.log</file>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${TestLogPath}/test.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- Rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${TestLogPath}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- Set root log level to "ERROR" -->
<root level="error">
<!-- Reference the "Console" appender for console output -->
<appender-ref ref="Console" />
<!-- <appender-ref ref="Console2" /> -->
<!-- Reference the "File" appender for file output -->
<appender-ref ref="File" />
<!-- Reference the "RollingFile" appender for file output -->
<!-- <appender-ref ref="RollingFile" /> -->
</root>
<logger name="sg.com.smartinventory.testloglevel" level="debug" />
<!--
<springProfile name="logback-test1">
<logger name="sg.com.smartinventory.testloglevel" level="info" />
</springProfile>
<springProfile name="logback-test2">
<logger name="sg.com.smartinventory.testloglevel" level="trace" />
</springProfile>
-->
</configuration>