diff --git a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/log/Log4JLogConfigInitializer.java b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/log/Log4JLogConfigInitializer.java index b6e0c0563a16..db6b81c9b208 100644 --- a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/log/Log4JLogConfigInitializer.java +++ b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/log/Log4JLogConfigInitializer.java @@ -71,6 +71,8 @@ public class Log4JLogConfigInitializer implements LogConfigInitializer { private static final String LOG_DIR = "logs"; + private static final String DATA_VALUE_INPUT = "dhis-datavalue-input.log"; + private static final String ANALYTICS_TABLE_LOGGER_FILENAME = "dhis-analytics-table.log"; private static final String DATA_EXCHANGE_LOGGER_FILENAME = "dhis-data-exchange.log"; @@ -127,6 +129,9 @@ public void initConfig() { locationManager.buildDirectory(LOG_DIR); + addConfigurableLogger( + DATA_VALUE_INPUT, Lists.newArrayList("org.hisp.dhis.webapi.controller.datavalue.input")); + addConfigurableLogger( ANALYTICS_TABLE_LOGGER_FILENAME, Lists.newArrayList("org.hisp.dhis.resourcetable", "org.hisp.dhis.analytics.table")); diff --git a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/datavalue/DataValueController.java b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/datavalue/DataValueController.java index 48f512455476..7486422b4a78 100644 --- a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/datavalue/DataValueController.java +++ b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/datavalue/DataValueController.java @@ -39,7 +39,10 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.hisp.dhis.category.CategoryCombo; import org.hisp.dhis.category.CategoryOption; import org.hisp.dhis.category.CategoryOptionCombo; @@ -100,6 +103,7 @@ */ @OpenApi.Tags("data") @RestController +@Slf4j @RequestMapping(value = DataValueController.RESOURCE_PATH) @ApiVersion({DhisApiVersion.DEFAULT, DhisApiVersion.ALL}) @RequiredArgsConstructor @@ -126,6 +130,10 @@ public class DataValueController { private final DhisConfigurationProvider dhisConfig; + // Custom logger for logging input payloads + private static final Logger inputLogger = + LogManager.getLogger("org.hisp.dhis.webapi.controller.datavalue.input"); + // --------------------------------------------------------------------- // POST // --------------------------------------------------------------------- @@ -148,22 +156,64 @@ public void saveDataValue( @RequestParam(required = false) boolean force, @CurrentUser User currentUser) throws WebMessageException { - DataValueCategoryDto attribute = dataValidator.getDataValueCategoryDto(cc, cp); - DataValueDto dataValue = - new DataValueDto() - .setDataElement(de) - .setCategoryOptionCombo(co) - .setAttribute(attribute) - .setPeriod(pe) - .setOrgUnit(ou) - .setDataSet(ds) - .setValue(value) - .setComment(comment) - .setFollowUp(followUp) - .setForce(force); + // Log the input payload using the custom logger + inputLogger.info( + "Received payload: username={}, de={}, co={}, cc={}, cp={}, pe={}, ou={}, ds={}, value={}, comment={}, followUp={}, force={}", + currentUser.getUsername(), + de, + co, + cc, + cp, + pe, + ou, + ds, + value, + comment, + followUp, + force); - saveDataValueInternal(dataValue, currentUser); + try { + DataValueCategoryDto attribute = dataValidator.getDataValueCategoryDto(cc, cp); + + DataValueDto dataValue = + new DataValueDto() + .setDataElement(de) + .setCategoryOptionCombo(co) + .setAttribute(attribute) + .setPeriod(pe) + .setOrgUnit(ou) + .setDataSet(ds) + .setValue(value) + .setComment(comment) + .setFollowUp(followUp) + .setForce(force); + + saveDataValueInternal(dataValue, currentUser); + } catch (Exception e) { + log.error( + "Failed to save data value, LOGGING DATAVALUE EXCEPTION AND INPUT VALUES, Exception:" + + e.getMessage(), + e); + + // Log the input payload using the custom logger + log.error( + "Payload accompanying previous thrown exception: username={}, de={}, co={}, cc={}, cp={}, pe={}, ou={}, ds={}, value={}, comment={}, followUp={}, force={}", + currentUser.getUsername(), + de, + co, + cc, + cp, + pe, + ou, + ds, + value, + comment, + followUp, + force); + + throw e; + } } @PreAuthorize("hasRole('ALL') or hasRole('F_DATAVALUE_ADD')")