Skip to content

Commit

Permalink
Added ThreadContext for hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
ishita9 committed Oct 11, 2024
1 parent 3495c31 commit 78fc37f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 16 additions & 1 deletion java/edu/ucar/metviewer/db/mysql/MysqlDatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions java/edu/ucar/metviewer/db/mysql/MysqlLoadDatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 78fc37f

Please sign in to comment.