[28.09.20] Logback and MDC ids
This commit is contained in:
parent
9603794f91
commit
a396b0f002
9
pom.xml
9
pom.xml
@ -21,6 +21,7 @@
|
|||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<logback.contrib.version>0.1.5</logback.contrib.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -98,6 +99,14 @@
|
|||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--- Logging -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.logstash.logback</groupId>
|
||||||
|
<artifactId>logstash-logback-encoder</artifactId>
|
||||||
|
<version>6.4</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<!-- Testing -->
|
<!-- Testing -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
|||||||
@ -23,6 +23,7 @@ import java.util.concurrent.CountDownLatch;
|
|||||||
import static cryptosky.me.ArtemisSyncMessaging.*;
|
import static cryptosky.me.ArtemisSyncMessaging.*;
|
||||||
|
|
||||||
import static cryptosky.me.helpers.Utils.getCorrelationId;
|
import static cryptosky.me.helpers.Utils.getCorrelationId;
|
||||||
|
import static cryptosky.me.logging.CorrelationInfo.setCorrelationId;
|
||||||
import static java.lang.String.format;
|
import static java.lang.String.format;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@ -60,6 +61,8 @@ public class PriceConsumer {
|
|||||||
logger.info("Received Message: " + message.getBody(String.class));
|
logger.info("Received Message: " + message.getBody(String.class));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
setCorrelationId(correlationId);
|
||||||
|
|
||||||
if (message.getBody(String.class) == null | message.getBody(String.class).equals("")) {
|
if (message.getBody(String.class) == null | message.getBody(String.class).equals("")) {
|
||||||
throw new NoBodyOrStringException();
|
throw new NoBodyOrStringException();
|
||||||
}
|
}
|
||||||
|
|||||||
22
src/main/java/cryptosky/me/logging/CorrelationInfo.java
Normal file
22
src/main/java/cryptosky/me/logging/CorrelationInfo.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package cryptosky.me.logging;
|
||||||
|
|
||||||
|
import org.slf4j.MDC;
|
||||||
|
|
||||||
|
public class CorrelationInfo {
|
||||||
|
|
||||||
|
public static final String CORRELATION_ID = "X-CRYPTO-Correlation-ID";
|
||||||
|
public static final String SYNC_ID = "X-CRYPTO-Sync-ID";
|
||||||
|
|
||||||
|
public static void setCorrelationId(String correlationId) {
|
||||||
|
MDC.put(CORRELATION_ID, correlationId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setSyncId(String syncId) {
|
||||||
|
MDC.put(SYNC_ID, syncId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clear() {
|
||||||
|
MDC.remove(CORRELATION_ID);
|
||||||
|
MDC.remove(SYNC_ID);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -32,6 +32,7 @@ import static cryptosky.me.ArtemisSyncMessaging.*;
|
|||||||
import static cryptosky.me.SupportedCurrencies.*;
|
import static cryptosky.me.SupportedCurrencies.*;
|
||||||
|
|
||||||
import static cryptosky.me.helpers.Utils.format;
|
import static cryptosky.me.helpers.Utils.format;
|
||||||
|
import static cryptosky.me.logging.CorrelationInfo.setSyncId;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class PriceService {
|
public class PriceService {
|
||||||
@ -79,6 +80,8 @@ public class PriceService {
|
|||||||
@Transactional
|
@Transactional
|
||||||
public void createRecord(CryptoPriceModel cryptoPriceModel, String correlationId, String syncId, TextMessage message) {
|
public void createRecord(CryptoPriceModel cryptoPriceModel, String correlationId, String syncId, TextMessage message) {
|
||||||
try {
|
try {
|
||||||
|
setSyncId(syncId);
|
||||||
|
|
||||||
switch (cryptoPriceModel.getType()) {
|
switch (cryptoPriceModel.getType()) {
|
||||||
case BTC:
|
case BTC:
|
||||||
BtcPriceModel priceModel = new BtcPriceModel();
|
BtcPriceModel priceModel = new BtcPriceModel();
|
||||||
|
|||||||
36
src/main/resources/logback-spring.xml
Normal file
36
src/main/resources/logback-spring.xml
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<property name="LOGS" value="./logs" />
|
||||||
|
|
||||||
|
<appender name="logstash" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
|
||||||
|
<fieldNames>
|
||||||
|
<version>[ignore]</version>
|
||||||
|
<levelValue>[ignore]</levelValue>
|
||||||
|
</fieldNames>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<appender name="console"
|
||||||
|
class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||||
|
<Pattern>
|
||||||
|
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg {%mdc}%n%throwable
|
||||||
|
</Pattern>
|
||||||
|
</layout>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<springProfile name="!local">
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="logstash"/>
|
||||||
|
</root>
|
||||||
|
</springProfile>
|
||||||
|
|
||||||
|
<springProfile name="local">
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="console"/>
|
||||||
|
</root>
|
||||||
|
</springProfile>
|
||||||
|
|
||||||
|
<jmxConfigurator/>
|
||||||
|
</configuration>
|
||||||
Loading…
x
Reference in New Issue
Block a user