Skip to content

Commit

Permalink
Merge pull request #477 from dtcenter/issue_11_time_stamp
Browse files Browse the repository at this point in the history
record a time stamp indicating when the event occurred
  • Loading branch information
TatianaBurek authored May 10, 2023
2 parents 1a39ae8 + 23d2a6d commit c60e335
Show file tree
Hide file tree
Showing 22 changed files with 211 additions and 245 deletions.
54 changes: 33 additions & 21 deletions java/edu/ucar/metviewer/MVBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
import edu.ucar.metviewer.db.AppDatabaseManager;
import edu.ucar.metviewer.db.DatabaseManager;
import edu.ucar.metviewer.jobManager.*;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.io.IoBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class MVBatch {

public class MVBatch {

private static final Logger logger = LogManager.getLogger(MVBatch.class);
private PrintStream printStream;
private PrintStream printStreamSql;
private PrintStream printStreamEr;
Expand Down Expand Up @@ -80,33 +81,36 @@ public MVBatch(

public MVBatch() {

this(IoBuilder.forLogger(MVBatch.class).setLevel(org.apache.logging.log4j.Level.INFO)
.buildPrintStream(),
IoBuilder.forLogger(MVBatch.class).setLevel(org.apache.logging.log4j.Level.INFO)
.buildPrintStream(),
IoBuilder.forLogger().setLevel(org.apache.logging.log4j.Level.INFO)
.setMarker(new MarkerManager.Log4jMarker("ERROR"))
.buildPrintStream(), null);
this.printStream = null;
this.printStreamSql = null;
this.printStreamEr = null;
databaseManager = null;
}

public void print(String message) {
if (this.printStream != null) {
this.printStream.println(message);
this.printStream.flush();
}else {
System.out.println(message);
}
}

public void printSql(String message) {
if (this.printStreamSql != null) {
this.printStreamSql.print(message);
this.printStreamSql.flush();
}else {
System.out.println(message);
}
}

public void printError(String message) {
if (this.printStreamEr != null) {
this.printStreamEr.print(message);
this.printStreamEr.flush();
}else {
System.out.println(message);
}
}

Expand Down Expand Up @@ -252,12 +256,13 @@ public static void main(String[] argv) throws Exception {
stopWatch.start();
MVBatch mvBatch = new MVBatch();

mvBatch.print("---- MVBatch ----\n");
logger.info("---- MVBatch ----\n");
try {
MVPlotJob[] jobs;
// if no input file is present, bail
if (1 > argv.length) {
mvBatch.print(getUsage() + "\n---- MVBatch Done ----");
mvBatch.print(getUsage());
logger.info( "---- MVBatch Done ----");
return;
}
// parse the command line options
Expand All @@ -269,12 +274,14 @@ public static void main(String[] argv) throws Exception {
} else if (argv[intArg].equals("-printSql")) {
mvBatch.setVerbose(true);
}else if ("-h".equalsIgnoreCase(argv[0]) || "--h".equalsIgnoreCase(argv[0]) || "-help".equalsIgnoreCase(argv[0])) {
mvBatch.print(getUsage() + "\n\n---- MVBatch Done ----");
mvBatch.print(getUsage());
logger.info( "---- MVBatch Done ----");
return;
} else {
mvBatch.print(
" ** ERROR: unrecognized option '"
+ argv[intArg] + "'\n\n" + getUsage() + "\n---- MVBatch Done ----");
logger.error(" ** ERROR: unrecognized option '" + argv[intArg]);
mvBatch.print(getUsage());
logger.info( "---- MVBatch Done ----");

return;
}
}
Expand Down Expand Up @@ -311,7 +318,7 @@ public static void main(String[] argv) throws Exception {

// if only a list of plot jobs is requested, return
if (boolList) {
mvBatch.print("\n---- MVBatch Done ----");
logger.info("---- MVBatch Done ----");
return;
}

Expand All @@ -322,7 +329,11 @@ public static void main(String[] argv) throws Exception {
ArrayList listJobs = new ArrayList();
for (String listJobName : listJobNames) {
if (!mapJobs.containsKey(listJobName)) {
mvBatch.printStream.println(" ** WARNING: unrecognized job \"" + listJobName + "\"");
if (mvBatch.printStream == null){
logger.info(" ** WARNING: unrecognized job \"" + listJobName + "\"");
}else {
mvBatch.printStream.println(" ** WARNING: unrecognized job \"" + listJobName + "\"");
}
continue;
}
listJobs.add(mapJobs.get(listJobName));
Expand Down Expand Up @@ -434,13 +445,14 @@ public static void main(String[] argv) throws Exception {

} catch (Exception e) {
stopWatch.stop();
mvBatch.print(" ** ERROR: " + e.getMessage());
logger.error(" ** ERROR: " + e.getMessage());

}
mvBatch.closeDataSource();

mvBatch.print("---- MVBatch Done ----");
logger.info("---- MVBatch Done ----");

mvBatch.print("\nTotal execution time " + stopWatch.getFormattedTotalDuration());
logger.info("Total execution time " + stopWatch.getFormattedTotalDuration());

}

Expand Down
23 changes: 6 additions & 17 deletions java/edu/ucar/metviewer/MVLoad.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
package edu.ucar.metviewer;

import java.io.*;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.Charset;

import java.time.LocalDateTime;
import java.util.*;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;


import edu.ucar.metviewer.db.DatabaseManager;
import edu.ucar.metviewer.db.LoadDatabaseManager;
import org.apache.logging.log4j.*;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.io.IoBuilder;

public class MVLoad {

private static final Logger logger = LogManager.getLogger("MVLoad");
private static final Marker ERROR_MARKER = MarkerManager.getMarker("ERROR");
private static LoadDatabaseManager loadDatabaseManager;


Expand Down Expand Up @@ -178,8 +168,7 @@ public static void main(String[] argv) {
file = new File(listLoadFiles[i]);
processFile(file);
} catch (Exception e) {
logger.error(ERROR_MARKER,
" ** ERROR: caught " + e.getClass() + " loading file "
logger.error(" ** ERROR: caught " + e.getClass() + " loading file "
+ listLoadFiles[i] + ": " + e.getMessage());
logger.error(e);
logger.info(
Expand Down Expand Up @@ -208,7 +197,7 @@ public static void main(String[] argv) {
.setLevel(org.apache.logging.log4j.Level.INFO)
.buildPrintStream();
}catch (Exception e){
System.out.println(e.getMessage());
logger.error(e.getMessage());
}
// determine the name of the current folder
baseFolder = MVUtil.buildTemplateString(job.getFolderTmpl(), listPerm[intPerm],
Expand All @@ -232,7 +221,7 @@ public static void main(String[] argv) {
try {
processFile(listDataFile);
} catch (Exception e) {
logger.error(ERROR_MARKER, " ** ERROR: caught " + e.getClass() + " in processFile()\n"
logger.error(" ** ERROR: caught " + e.getClass() + " in processFile()\n"
+ e.getMessage() + "\n"
+ " ** WARNING: error(s) encountered loading file "
+ listDataFile + " - skipping file");
Expand Down Expand Up @@ -323,7 +312,7 @@ public static void main(String[] argv) {
logger.info("End time: " + MVUtil.APP_DATE_FORMATTER.format(LocalDateTime.now()));
logger.info("Load total: " + MVUtil.formatTimeSpan(intLoadTime) + "\n");
} catch (Exception e) {
logger.error(ERROR_MARKER, " ** ERROR: Caught " + e.getClass() + ": " + e.getMessage());
logger.error(" ** ERROR: Caught " + e.getClass() + ": " + e.getMessage());
logger.error(e);
}

Expand Down
17 changes: 8 additions & 9 deletions java/edu/ucar/metviewer/MVPlotJobParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
public final class MVPlotJobParser {

protected static final Map<String, Method> formatToBoolValues = new HashMap<>();
private static final Logger logger = LogManager.getLogger("MVPlotJobParser");
private static final Marker ERROR_MARKER = MarkerManager.getMarker("ERROR");
private static final Logger logger = LogManager.getLogger(MVPlotJobParser.class);

private static final Map<String, Method> formatToStrValues = new HashMap<>();

Expand Down Expand Up @@ -81,7 +80,7 @@ public final class MVPlotJobParser {
MVPlotJob.class.getDeclaredMethod("setEnsSsPtsDisp", boolean.class));

} catch (NoSuchMethodException e) {
logger.error(ERROR_MARKER, e.getMessage());
logger.error( e.getMessage());
}
}

Expand Down Expand Up @@ -265,7 +264,7 @@ public final class MVPlotJobParser {
MVPlotJob.class.getDeclaredMethod("setColorPalette", String.class));

} catch (NoSuchMethodException e) {
logger.error(ERROR_MARKER, e.getMessage());
logger.error( e.getMessage());
}
}

Expand Down Expand Up @@ -435,7 +434,7 @@ public static String checkJobFieldsOrder(MVPlotJob job) throws ValidationExcepti
try {
valuesSortedInt[i] = Integer.valueOf(indyVals[i]);
} catch (NumberFormatException e) {
logger.error(ERROR_MARKER, e.getMessage());
logger.error( e.getMessage());
}
}
Arrays.sort(valuesSortedInt);
Expand All @@ -447,7 +446,7 @@ public static String checkJobFieldsOrder(MVPlotJob job) throws ValidationExcepti
result = result + "Values for variable " + job.getIndyVar() + " are not sorted";
}
} catch (NumberFormatException e) {
logger.error(ERROR_MARKER, e.getMessage());
logger.error( e.getMessage());
}

}
Expand Down Expand Up @@ -501,7 +500,7 @@ private static String checkOrder(Map.Entry[] mapValues) throws ValidationExcepti
try {
valuesSortedInt[i] = Integer.valueOf(values[i]);
} catch (NumberFormatException e) {
logger.error(ERROR_MARKER, e.getMessage());
logger.error( e.getMessage());
}
}
Arrays.sort(valuesSortedInt);
Expand All @@ -513,7 +512,7 @@ private static String checkOrder(Map.Entry[] mapValues) throws ValidationExcepti
return "Values for variable " + entry.getKey().toString() + " are not sorted";
}
} catch (NumberFormatException e) {
logger.error(ERROR_MARKER, e.getMessage());
logger.error( e.getMessage());
}
}
} else {
Expand Down Expand Up @@ -631,7 +630,7 @@ else if (nodeIndyVal.tag.equalsIgnoreCase("date_list")) {
try {
listLabels.add(formatLabel.format(formatDb.parse(listDate)));
} catch (ParseException e) {
logger.error(ERROR_MARKER, e.getMessage());
logger.error( e.getMessage());
}

}
Expand Down
15 changes: 6 additions & 9 deletions java/edu/ucar/metviewer/MVServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -50,7 +49,6 @@ public class MVServlet extends HttpServlet {

private static final PrintStream errorStream
= IoBuilder.forLogger(MVUtil.class).setLevel(org.apache.logging.log4j.Level.INFO)
.setMarker(new MarkerManager.Log4jMarker("ERROR"))
.buildPrintStream();

private static final Pattern patDownload = Pattern.compile(".*/download");
Expand All @@ -60,7 +58,6 @@ public class MVServlet extends HttpServlet {
private static final String DATE_FORMAT_STRING = "yyyyMMdd_HHmmss";
private static final long serialVersionUID = 1L;
private static final Logger logger = LogManager.getLogger("MVServlet");
private static final Marker ERROR_MARKER = MarkerManager.getMarker("ERROR");
private static final Marker INFO_MARKER = MarkerManager.getMarker("INFO");

private static final FilenameFilter PNG_FILTER = new FilenameFilter() {
Expand Down Expand Up @@ -456,7 +453,7 @@ private String handlePlot(
}
job = jobs[0];
} catch (ParserConfigurationException | DatabaseException | ValidationException | IOException | SAXException e) {
logger.error(ERROR_MARKER, e.getMessage());
logger.error( e.getMessage());
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(plotXml + DELIMITER + plotPrefix + ".log");
Expand Down Expand Up @@ -550,7 +547,7 @@ private String handlePlot(
transformer.transform(new DOMSource(doc), streamResult);
stream.flush();
} catch (IllegalArgumentException | TransformerException | IOException e) {
logger.error(ERROR_MARKER,
logger.error(
"handlePlot() - ERROR: caught " + e.getClass() + " serializing plot xml: "
+ e.getMessage());
return "<error>failed to serialize plot xml</error>";
Expand Down Expand Up @@ -597,7 +594,7 @@ private String handlePlot(
try {
stopWatch.stop();
} catch (StopWatchException e) {
logger.error(ERROR_MARKER, e.getMessage());
logger.error( e.getMessage());
}
if (logSql != null) {
// build the job SQL using the batch engine
Expand Down Expand Up @@ -632,13 +629,13 @@ private String handlePlot(
try {
printStream.println("\nTotal execution time " + stopWatch.getFormattedTotalDuration());
} catch (StopWatchException e) {
logger.error(ERROR_MARKER, e.getMessage());
logger.error( e.getMessage());
}
if (!plotterOutput.isEmpty()) {
try (FileWriter fileWriter = new FileWriter(plotXml + DELIMITER + plotPrefix + ".log")) {
fileWriter.write(plotterOutput);
} catch (IOException e) {
logger.error(ERROR_MARKER, e.getMessage());
logger.error( e.getMessage());
}
}

Expand Down Expand Up @@ -1040,7 +1037,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) {
printWriter.println("howdy from MVServlet");
}
} catch (IOException e) {
logger.error(ERROR_MARKER, e.getMessage());
logger.error( e.getMessage());
}
}

Expand Down
Loading

0 comments on commit c60e335

Please sign in to comment.