From 78fc37fe2085ad03f7822babf398dba8fce96ce3 Mon Sep 17 00:00:00 2001 From: ishitas Date: Fri, 11 Oct 2024 00:04:45 +0000 Subject: [PATCH] Added ThreadContext for hostname --- .../db/mysql/MysqlAppDatabaseManager.java | 3 ++- .../db/mysql/MysqlDatabaseManager.java | 17 ++++++++++++++++- .../db/mysql/MysqlLoadDatabaseManager.java | 10 ++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/java/edu/ucar/metviewer/db/mysql/MysqlAppDatabaseManager.java b/java/edu/ucar/metviewer/db/mysql/MysqlAppDatabaseManager.java index 48a0bdc9..4e84600c 100644 --- a/java/edu/ucar/metviewer/db/mysql/MysqlAppDatabaseManager.java +++ b/java/edu/ucar/metviewer/db/mysql/MysqlAppDatabaseManager.java @@ -13,7 +13,8 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.MarkerManager; - +import org.apache.logging.log4j.ThreadContext; +import java.net.InetAddress; import java.io.*; import java.sql.*; import java.text.SimpleDateFormat; diff --git a/java/edu/ucar/metviewer/db/mysql/MysqlDatabaseManager.java b/java/edu/ucar/metviewer/db/mysql/MysqlDatabaseManager.java index 8bc90adf..48b32e69 100644 --- a/java/edu/ucar/metviewer/db/mysql/MysqlDatabaseManager.java +++ b/java/edu/ucar/metviewer/db/mysql/MysqlDatabaseManager.java @@ -28,7 +28,8 @@ import edu.ucar.metviewer.db.DatabaseManager; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; - +import org.apache.logging.log4j.ThreadContext; +import java.net.InetAddress; import org.apache.tomcat.jdbc.pool.DataSource; import org.apache.tomcat.jdbc.pool.PoolConfiguration; import org.apache.tomcat.jdbc.pool.PoolProperties; @@ -57,6 +58,20 @@ public class MysqlDatabaseManager extends DatabaseManager { public MysqlDatabaseManager(DatabaseInfo databaseInfo, String password) { super(databaseInfo); + // Set the hostname in ThreadContext for logging purposes + try { + String hostName = InetAddress.getLocalHost().getHostName(); + ThreadContext.put("hostName", hostName); // Add hostname to the ThreadContext + } catch (Exception e) { + logger.error("Unable to fetch the hostname for logging context", e); + } + // Set the hostname in ThreadContext for logging purposes + try { + String hostName = InetAddress.getLocalHost().getHostName(); + ThreadContext.put("hostName", hostName); // Add hostname to the ThreadContext + } catch (Exception e) { + logger.error("Unable to fetch the hostname for logging context", e); + } String jdbcUrl = getJdbcUrl(databaseInfo.getHost(), databaseInfo.getDbName()); PoolConfiguration configurationToUse = new PoolProperties(); configurationToUse.setUrl(jdbcUrl); diff --git a/java/edu/ucar/metviewer/db/mysql/MysqlLoadDatabaseManager.java b/java/edu/ucar/metviewer/db/mysql/MysqlLoadDatabaseManager.java index e6fbcbd1..cfd8a4fd 100644 --- a/java/edu/ucar/metviewer/db/mysql/MysqlLoadDatabaseManager.java +++ b/java/edu/ucar/metviewer/db/mysql/MysqlLoadDatabaseManager.java @@ -13,7 +13,8 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.MarkerManager; - +import org.apache.logging.log4j.ThreadContext; +import java.net.InetAddress; import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -82,7 +83,12 @@ public class MysqlLoadDatabaseManager extends MysqlDatabaseManager implements Lo public MysqlLoadDatabaseManager( DatabaseInfo databaseInfo, String password) throws DatabaseException { super(databaseInfo, password); - + try { + String hostName = InetAddress.getLocalHost().getHostName(); + ThreadContext.put("hostName", hostName); // Add hostname to the ThreadContext + } catch (Exception e) { + logger.error("Unable to fetch the hostname for logging context", e); + } tableToInsert = new HashMap<>(); tableToInsert.put("line_data_fho", "INSERT INTO line_data_fho VALUES (?,?,?,?,?,?,?,?,?,?,?,?," + "?,?)");//14