Files
spring-boot-rest/log-mdc/src/main/java/com/baeldung/ndc/controller/Log4JController.java
Sunil Mogadati 2fe2e2a971 BAEL-40: Add NDC and JBoss Logging to the demo application (#880)
* Add NDC and JBoss Logging to the demo application

* NDC for Log4j, Log4j2 and JBoss Logging

* Simplify NDC example by making it a single operation instead of two

* Make NDC example as RestController, Use JBoss Logging only as a logging bridge

* Fix merge conflicts in pull request - log-mdc pom.xml updated
2016-12-16 12:29:32 -06:00

41 lines
1.4 KiB
Java

package com.baeldung.ndc.controller;
import org.apache.log4j.NDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.ndc.Investment;
import com.baeldung.ndc.service.InvestmentService;
@RestController
public class Log4JController {
@Autowired
@Qualifier("log4jInvestmentService")
private InvestmentService log4jBusinessService;
@RequestMapping(value = "/ndc/log4j", method = RequestMethod.POST)
public ResponseEntity<Investment> postPayment(@RequestBody Investment investment) {
// Add transactionId and owner to NDC
NDC.push("tx.id=" + investment.getTransactionId());
NDC.push("tx.owner=" + investment.getOwner());
try {
log4jBusinessService.transfer(investment.getAmount());
} finally {
// take out owner from the NDC stack
NDC.pop();
// take out transactionId from the NDC stack
NDC.pop();
NDC.remove();
}
return new ResponseEntity<Investment>(investment, HttpStatus.OK);
}
}