Skip to content

Commit

Permalink
Feature internal 29 unique logging (#549)
Browse files Browse the repository at this point in the history
* Modified log file to have hostname and appname. Added threadcontext to read the hostname and put it into the logging context.

* Added import java.net.InetAddress

* Added threadContext for Servlet

* Added threadContext

* Added InetAddress import statement

* Added ThreadContext for hostname

* Mofied log4j2.xml such that hostname prints only if available

* Changed ThreadContext placement

* Changed placement of ThreadContext

* Syntax edit

* ThreadContext within constructor

* Added log4j settings to have hostname

* Added new pattern

* Replicated pattern from log4j

* added thread context at the beginning of the constructor

* Using hostname as an env variable

* Got rid of ThreadContext approach

* Removed ThreadContext clearall() function
  • Loading branch information
ishita9 authored Oct 12, 2024
1 parent 0030eca commit f1e5024
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 21 deletions.
5 changes: 1 addition & 4 deletions java/edu/ucar/metviewer/MVBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;



public class MVBatch {

private static final Logger logger = LogManager.getLogger(MVBatch.class);
Expand Down Expand Up @@ -450,7 +448,6 @@ public static void main(String[] argv) throws Exception {
logger.info("---- MVBatch Done by user " + username + " ----");

logger.info("Total execution time " + stopWatch.getFormattedTotalDuration());

}

}
2 changes: 1 addition & 1 deletion java/edu/ucar/metviewer/MVLoad.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.logging.log4j.*;
import org.apache.logging.log4j.io.IoBuilder;


public class MVLoad {

private static final Logger logger = LogManager.getLogger("MVLoad");
Expand Down Expand Up @@ -69,7 +70,6 @@ private MVLoad() {
public static void main(String[] argv) {

logger.info("---- MVLoad ----\n");

try {

// parse the input arguments
Expand Down
1 change: 1 addition & 0 deletions java/edu/ucar/metviewer/MVServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ private String getAvailableResults(String showAll) throws ParserConfigurationExc
@Override
public void init() {
logger.debug("init() - loading properties...");

try {
ResourceBundle bundle = ResourceBundle.getBundle("mvservlet");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

import java.io.*;
import java.sql.*;
import java.text.SimpleDateFormat;
Expand Down
9 changes: 4 additions & 5 deletions java/edu/ucar/metviewer/db/mysql/MysqlDatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import edu.ucar.metviewer.db.DatabaseManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolConfiguration;
import org.apache.tomcat.jdbc.pool.PoolProperties;
Expand All @@ -40,7 +39,6 @@
public class MysqlDatabaseManager extends DatabaseManager {

private static final Logger logger = LogManager.getLogger(MysqlDatabaseManager.class);

protected static Map<String, String> listDB = new TreeMap<>();
protected static Map<String, List<String>> groupToDatabases = new HashMap<>();
private static String DATE_FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";
Expand All @@ -57,6 +55,8 @@ public class MysqlDatabaseManager extends DatabaseManager {

public MysqlDatabaseManager(DatabaseInfo databaseInfo, String password) {
super(databaseInfo);
// Add hostname to the ThreadContext for logging

String jdbcUrl = getJdbcUrl(databaseInfo.getHost(), databaseInfo.getDbName());
PoolConfiguration configurationToUse = new PoolProperties();
configurationToUse.setUrl(jdbcUrl);
Expand Down Expand Up @@ -85,15 +85,14 @@ public MysqlDatabaseManager(DatabaseInfo databaseInfo, String password) {

dataSource = new DataSource();
dataSource.setPoolProperties(configurationToUse);



boolean updateGroups = false;
if (databaseInfo.getDbName() == null) {
updateGroups = true;
}
initDBList(updateGroups);
}

public synchronized String formatDate(Date date) {
return DATE_FORMAT.format(date.getTime());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
Expand Down Expand Up @@ -82,7 +81,6 @@ public class MysqlLoadDatabaseManager extends MysqlDatabaseManager implements Lo
public MysqlLoadDatabaseManager(
DatabaseInfo databaseInfo, String password) throws DatabaseException {
super(databaseInfo, password);

tableToInsert = new HashMap<>();
tableToInsert.put("line_data_fho", "INSERT INTO line_data_fho VALUES (?,?,?,?,?,?,?,?,?,?,?,?,"
+ "?,?)");//14
Expand Down
3 changes: 2 additions & 1 deletion java/edu/ucar/metviewer/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<!-- Suggested code may be subject to a license. Learn more: ~LicenseLog:251802877. -->
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">

<Appenders>
<Console name="appLog" target="SYSTEM_OUT">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%L}{blue}] [%style{%M}{bright,blue}] %style{%C{1.}}{bright,yellow}: \r\n%msg%n%throwable\r\n" />
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%L}{blue}] [%style{%M}{bright,blue}] %style{%C{1.}}{bright,yellow} %style{METviewer}{bright,green} %style{[%X{hostName}]}{bright,red} %msg%n%throwable%n" />
</Console>
</Appenders>

Expand Down
6 changes: 3 additions & 3 deletions webapp/metviewer/WEB-INF/classes/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%L}{blue}] [%style{%M}{bright,blue}] %style{%C{1.}}{bright,yellow} %style{METviewer}{bright,green} %style{[${env:HOSTNAME:-None}]}{bright,red} %msg%n%throwable%n" />
</Console>

<File name="RollingFile" fileName="/opt/vxwww/tomcat/logs/mvservlet.log" append="true">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%L}{blue}] [%style{%M}{bright,blue}] %style{%C{1.}}{bright,yellow} %style{METviewer}{bright,green} %style{[${env:HOSTNAME:-None}]}{bright,red} %msg%n%throwable%n" />
</File>
</Appenders>

Expand Down
8 changes: 4 additions & 4 deletions webapp/metviewer/WEB-INF/classes/log4j2.xml.orig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%L}{blue}] [%style{%M}{bright,blue}] %style{%C{1.}}{bright,yellow} %style{METviewer}{bright,green} %style{[${env:HOSTNAME:-None}]}{bright,red} %msg%n%throwable%n" />
</Console>

<File name="RollingFile" fileName="/opt/vxwww/tomcat/logs/mvservlet.log" append="true">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%L}{blue}] [%style{%M}{bright,blue}] %style{%C{1.}}{bright,yellow} %style{METviewer}{bright,green} %style{[${env:HOSTNAME:-None}]}{bright,red} %msg%n%throwable%n" />
</File>
</Appenders>

Expand All @@ -18,4 +18,4 @@
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
</Configuration>

0 comments on commit f1e5024

Please sign in to comment.