Skip to content

Commit

Permalink
feat(SpringBoot): ⬆️ Mise à jour vers Spring Boot 3
Browse files Browse the repository at this point in the history
  • Loading branch information
lschaeffer313 committed Apr 16, 2024
1 parent cc85c2c commit 5d3847d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 45 deletions.
16 changes: 8 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>nc.opt.core</groupId>
<artifactId>opt-logging</artifactId>
<version>3.0.1</version>
<version>3.0.2</version>
<name>opt-logging</name>
<description>Librairie OPT de gestion des logs pour les applications du socle.</description>
<inceptionYear>2017</inceptionYear>
Expand All @@ -23,14 +23,14 @@
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<java.version>17</java.version>
<!-- Dependencies versions -->
<logstash-logback-encoder.version>7.2</logstash-logback-encoder.version>
<spring-context.version>5.3.23</spring-context.version>
<jol-core.version>0.16</jol-core.version>
<logback-classic.version>1.2.11</logback-classic.version>
<junit-jupiter.version>5.9.1</junit-jupiter.version>
<spring-boot-starter-test.version>2.7.5</spring-boot-starter-test.version>
<logstash-logback-encoder.version>7.4</logstash-logback-encoder.version>
<spring-context.version>6.0.12</spring-context.version>
<jol-core.version>0.17</jol-core.version>
<logback-classic.version>1.4.7</logback-classic.version>
<junit-jupiter.version>5.9.2</junit-jupiter.version>
<spring-boot-starter-test.version>3.2.4</spring-boot-starter-test.version>

<!-- Plugins versions -->
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/nc/opt/core/logging/LogMetierService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package nc.opt.core.logging;

import net.logstash.logback.marker.Markers;
import java.util.Map;

import org.openjdk.jol.info.GraphLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Map;
import net.logstash.logback.marker.Markers;

/**
* Created by 2816ARN on 06/09/2017.
Expand All @@ -23,7 +24,8 @@ public void logObject(String objectName, Object obj) {
}

public void logObject(String objectName, String endPoint, String obj) {
LOGGER.info(Markers.append(objectName, new LogObject(endPoint, GraphLayout.parseInstance(obj).totalSize())), MSG, objectName);
LOGGER.info(Markers.append(objectName, new LogObject(endPoint, GraphLayout.parseInstance(obj).totalSize())),
MSG, objectName);
}

public void logAttributes(Object obj) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/META-INF/spring.factories

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nc.opt.core.logging.config.LogMetierAutoConfiguration
76 changes: 44 additions & 32 deletions src/test/java/nc/opt/core/logging/LogMetierServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package nc.opt.core.logging;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
Expand All @@ -17,16 +10,25 @@
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

@SpringBootTest(classes = LogMetierService.class)
public class LogMetierServiceTest {
class LogMetierServiceTest {

@Autowired
private LogMetierService service;

private ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
private ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false);

private ByteArrayOutputStream myOut;

Expand Down Expand Up @@ -55,70 +57,81 @@ private void assertEqualsLog(String objectName, AbstractLogObject log, TestObjec
}

@BeforeEach
public void init() {
void init() {
myOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(myOut));
}

@Test
public void testLogObject() throws Exception {
void testLogObject() throws Exception {
TestObject obj = new TestObject("toto");
service.logObject("test", obj);

//{"@timestamp":"2017-12-15T10:41:15.046+11:00","@version":1,"message":"add log entry for test","logger_name":"metiersLogger","thread_name":"main","level":"INFO","level_value":20000,"test":{"name":"toto"}}
//On vérifie que les logs sont bien en JSON
// {"@timestamp":"2017-12-15T10:41:15.046+11:00","@version":1,"message":"add log
// entry for
// test","logger_name":"metiersLogger","thread_name":"main","level":"INFO","level_value":20000,"test":{"name":"toto"}}
// On vérifie que les logs sont bien en JSON
final String standardOutput = myOut.toString();
LogObject log = mapper.readValue(standardOutput, LogObject.class);
assertEqualsLog("test", log, obj);
}

@Test
public void testLogArray() throws Exception {
void testLogArray() throws Exception {
TestObject obj1 = new TestObject("1");
TestObject obj2 = new TestObject("2");
service.logArray("test", new TestObject[]{obj1, obj2});
service.logArray("test", (Object[]) new TestObject[] { obj1, obj2 });

//{"@timestamp":"2017-12-15T10:55:09.564+11:00","@version":1,"message":"add log entry for test","logger_name":"metiersLogger","thread_name":"main","level":"INFO","level_value":20000,"test":[{"name":"1"},{"name":"2"}]}
//On vérifie que les logs sont bien en JSON
// {"@timestamp":"2017-12-15T10:55:09.564+11:00","@version":1,"message":"add log
// entry for
// test","logger_name":"metiersLogger","thread_name":"main","level":"INFO","level_value":20000,"test":[{"name":"1"},{"name":"2"}]}
// On vérifie que les logs sont bien en JSON
final String standardOutput = myOut.toString();
LogArray log = mapper.readValue(standardOutput, LogArray.class);
assertEqualsLog("test", log, obj1, obj2);
}

@Test
public void testLogAttributes() throws Exception {
void testLogAttributes() throws Exception {
TestObject obj1 = new TestObject("1");
service.logAttributes(obj1);

//{"@timestamp":"2017-12-15T11:36:09.817+11:00","@version":1,"message":"add log entry for class nc.opt.core.logging.LogMetierServiceTest$TestObject","logger_name":"metiersLogger","thread_name":"main","level":"INFO","level_value":20000,"name":"1"}
//On vérifie que les logs sont bien en JSON
// {"@timestamp":"2017-12-15T11:36:09.817+11:00","@version":1,"message":"add log
// entry for class
// nc.opt.core.logging.LogMetierServiceTest$TestObject","logger_name":"metiersLogger","thread_name":"main","level":"INFO","level_value":20000,"name":"1"}
// On vérifie que les logs sont bien en JSON
final String standardOutput = myOut.toString();
LogObject log = mapper.readValue(standardOutput, LogObject.class);
assertEqualsLog("class " + TestObject.class.getName(), log);
assertEquals("1", log.attributes.get("name"));
}

@Test
public void testLogJson() throws Exception {
void testLogJson() throws Exception {
TestObject obj1 = new TestObject("1");
service.logJson("test", mapper.writeValueAsString(obj1));

//{"@timestamp":"2017-12-15T11:55:05.818+11:00","@version":1,"message":"add log entry for test","logger_name":"metiersLogger","thread_name":"main","level":"INFO","level_value":20000,"test":{"name":"1"}}
//On vérifie que les logs sont bien en JSON
// {"@timestamp":"2017-12-15T11:55:05.818+11:00","@version":1,"message":"add log
// entry for
// test","logger_name":"metiersLogger","thread_name":"main","level":"INFO","level_value":20000,"test":{"name":"1"}}
// On vérifie que les logs sont bien en JSON
final String standardOutput = myOut.toString();
LogObject log = mapper.readValue(standardOutput, LogObject.class);
assertEqualsLog("test", log, obj1);
}

@Test
public void testLogEntries() throws Exception {
void testLogEntries() throws Exception {
TestObject obj1 = new TestObject("1");
Map<String, TestObject> entries = new HashMap<>();
entries.put("test", obj1);
service.logEntries(entries);

//{"@timestamp":"2017-12-15T11:59:09.672+11:00","@version":1,"message":"add log entry for map","logger_name":"metiersLogger","thread_name":"main","level":"INFO","level_value":20000,"test":{"name":"1"}}
//On vérifie que les logs sont bien en JSON
// {"@timestamp":"2017-12-15T11:59:09.672+11:00","@version":1,"message":"add log
// entry for
// map","logger_name":"metiersLogger","thread_name":"main","level":"INFO","level_value":20000,"test":{"name":"1"}}

// On vérifie que les logs sont bien en JSON
final String standardOutput = myOut.toString();
LogObject log = mapper.readValue(standardOutput, LogObject.class);
assertEqualsLog("map", log, obj1);
Expand Down Expand Up @@ -151,7 +164,6 @@ static abstract class AbstractLogObject {
private String level;
@JsonProperty("level_value")
private Integer levelValue;

public AbstractLogObject() {

}
Expand All @@ -170,7 +182,7 @@ public LogObject() {
}

@JsonAnySetter
public void set(String fieldName, Object value) {
void set(String fieldName, Object value) {
this.attributes.put(fieldName, value);
}
}
Expand Down

0 comments on commit 5d3847d

Please sign in to comment.