This is an automated email from the ASF dual-hosted git repository.

arnold pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract.git


The following commit(s) were added to refs/heads/develop by this push:
     new 23887bdf1c FINERACT-2181: Tenant ID is included in the log messages
23887bdf1c is described below

commit 23887bdf1c4de9cb3278ac562829bbf003c93699
Author: Arnold Galovics <[email protected]>
AuthorDate: Thu Mar 6 17:13:43 2025 +0100

    FINERACT-2181: Tenant ID is included in the log messages
---
 .../logging/TenantIdentifierLoggingConverter.java  | 31 ++++++++++++++++++
 .../migration/TenantDatabaseUpgradeService.java    | 37 +++++++++++++---------
 .../src/main/resources/application.properties      |  2 +-
 .../src/main/resources/logback-spring.xml          |  3 ++
 4 files changed, 57 insertions(+), 16 deletions(-)

diff --git 
a/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/logging/TenantIdentifierLoggingConverter.java
 
b/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/logging/TenantIdentifierLoggingConverter.java
new file mode 100644
index 0000000000..dde208c339
--- /dev/null
+++ 
b/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/logging/TenantIdentifierLoggingConverter.java
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.infrastructure.core.logging;
+
+import ch.qos.logback.classic.pattern.ClassicConverter;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
+
+public class TenantIdentifierLoggingConverter extends ClassicConverter {
+
+    @Override
+    public String convert(ILoggingEvent event) {
+        return ThreadLocalContextUtil.getTenant() != null ? 
ThreadLocalContextUtil.getTenant().getTenantIdentifier() : "no-tenant";
+    }
+}
diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/service/migration/TenantDatabaseUpgradeService.java
 
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/service/migration/TenantDatabaseUpgradeService.java
index e44523aa48..9f4e810333 100644
--- 
a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/service/migration/TenantDatabaseUpgradeService.java
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/service/migration/TenantDatabaseUpgradeService.java
@@ -39,6 +39,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.fineract.infrastructure.core.boot.FineractProfiles;
 import org.apache.fineract.infrastructure.core.config.FineractProperties;
 import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenant;
+import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
 import 
org.apache.fineract.infrastructure.core.service.tenant.TenantDetailsService;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.beans.factory.annotation.Qualifier;
@@ -186,22 +187,28 @@ public class TenantDatabaseUpgradeService implements 
InitializingBean {
      * @throws LiquibaseException
      */
     private void upgradeIndividualTenant(FineractPlatformTenant tenant) throws 
LiquibaseException {
-        log.info("Upgrade for tenant {} has started", 
tenant.getTenantIdentifier());
-        try (HikariDataSource tenantDataSource = 
tenantDataSourceFactory.create(tenant)) {
-            // 'initial_switch' and 'custom_changelog' contexts should be 
controlled by the application configuration
-            // settings, and we should not use them to control the script order
-            if 
(databaseStateVerifier.isFirstLiquibaseMigration(tenantDataSource)) {
-                ExtendedSpringLiquibase liquibase = 
liquibaseFactory.create(tenantDataSource, TENANT_DB_CONTEXT, 
CUSTOM_CHANGELOG_CONTEXT,
-                        INITIAL_SWITCH_CONTEXT, tenant.getTenantIdentifier());
-                applyInitialLiquibase(tenantDataSource, liquibase, 
tenant.getTenantIdentifier(),
-                        (ds) -> 
!databaseStateVerifier.isTenantOnLatestUpgradableVersion(ds));
+        try {
+            ThreadLocalContextUtil.setTenant(tenant);
+            log.info("Upgrade for tenant {} has started", 
tenant.getTenantIdentifier());
+            try (HikariDataSource tenantDataSource = 
tenantDataSourceFactory.create(tenant)) {
+                // 'initial_switch' and 'custom_changelog' contexts should be 
controlled by the application
+                // configuration
+                // settings, and we should not use them to control the script 
order
+                if 
(databaseStateVerifier.isFirstLiquibaseMigration(tenantDataSource)) {
+                    ExtendedSpringLiquibase liquibase = 
liquibaseFactory.create(tenantDataSource, TENANT_DB_CONTEXT,
+                            CUSTOM_CHANGELOG_CONTEXT, INITIAL_SWITCH_CONTEXT, 
tenant.getTenantIdentifier());
+                    applyInitialLiquibase(tenantDataSource, liquibase, 
tenant.getTenantIdentifier(),
+                            (ds) -> 
!databaseStateVerifier.isTenantOnLatestUpgradableVersion(ds));
+                }
+                SpringLiquibase tenantLiquibase = 
liquibaseFactory.create(tenantDataSource, TENANT_DB_CONTEXT, 
CUSTOM_CHANGELOG_CONTEXT,
+                        tenant.getTenantIdentifier());
+                tenantLiquibase.afterPropertiesSet();
+                log.info("Upgrade for tenant {} has finished", 
tenant.getTenantIdentifier());
+            } catch (Exception e) {
+                throw new RuntimeException("Exception while upgrading tenant " 
+ tenant.getTenantIdentifier(), e);
             }
-            SpringLiquibase tenantLiquibase = 
liquibaseFactory.create(tenantDataSource, TENANT_DB_CONTEXT, 
CUSTOM_CHANGELOG_CONTEXT,
-                    tenant.getTenantIdentifier());
-            tenantLiquibase.afterPropertiesSet();
-            log.info("Upgrade for tenant {} has finished", 
tenant.getTenantIdentifier());
-        } catch (Exception e) {
-            throw new RuntimeException("Exception while upgrading tenant " + 
tenant.getTenantIdentifier(), e);
+        } finally {
+            ThreadLocalContextUtil.reset();
         }
     }
 
diff --git a/fineract-provider/src/main/resources/application.properties 
b/fineract-provider/src/main/resources/application.properties
index 286a5a2bc1..93934d9ab4 100644
--- a/fineract-provider/src/main/resources/application.properties
+++ b/fineract-provider/src/main/resources/application.properties
@@ -284,7 +284,7 @@ fineract.sql-validation.profiles[2].patternRefs[6].order=6
 fineract.sql-validation.profiles[2].enabled=true
 
 # Logging pattern for the console
-logging.pattern.console=${CONSOLE_LOG_PATTERN:%clr(%d{yyyy-MM-dd 
HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} 
%clr(%replace([%X{correlationId}]){'\\[\\]', ''}) %clr(---){faint} 
%clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} 
%m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}}
+logging.pattern.console=${CONSOLE_LOG_PATTERN:%clr(%d{yyyy-MM-dd 
HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} 
%clr(%replace([%X{correlationId}]){'\\[\\]', ''}) [%15.15tenantId] 
%clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} 
%clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}}
 logging.pattern.level=%5p 
[${spring.application.name:},%X{traceId:-},%X{spanId:-}]
 
 management.health.jms.enabled=${FINERACT_MANAGEMENT_HEALTH_JMS_ENABLED:false}
diff --git a/fineract-provider/src/main/resources/logback-spring.xml 
b/fineract-provider/src/main/resources/logback-spring.xml
index 2b2c2a301b..84fe601f2c 100644
--- a/fineract-provider/src/main/resources/logback-spring.xml
+++ b/fineract-provider/src/main/resources/logback-spring.xml
@@ -26,6 +26,9 @@
          and 
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback
 -->
     <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
 
+    <conversionRule conversionWord="tenantId"
+                    
converterClass="org.apache.fineract.infrastructure.core.logging.TenantIdentifierLoggingConverter"
 />
+
     <appender name="CONSOLE" target="System.out" 
class="ch.qos.logback.core.ConsoleAppender">
         <encoder>
             <pattern>${CONSOLE_LOG_PATTERN}</pattern>

Reply via email to