diff --git a/log4j.properties b/log4j.properties new file mode 100644 index 0000000..d8adfad --- /dev/null +++ b/log4j.properties @@ -0,0 +1,5 @@ +log4j.rootLogger=INFO, stdout +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapMainApp.java b/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapMainApp.java index 87c3453..ad4517a 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapMainApp.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapMainApp.java @@ -19,8 +19,9 @@ package eu.omp.irap.vespa.epntapclient; import javax.swing.JFrame; import javax.swing.SwingUtilities; +import org.apache.log4j.Logger; + import eu.omp.irap.vespa.epntapclient.controller.EpnTapController; -import eu.omp.irap.vespa.epntapclient.utils.Log; /** * Simple class to have a main function to launch the EPNTap client. @@ -29,6 +30,9 @@ import eu.omp.irap.vespa.epntapclient.utils.Log; */ public class EpnTapMainApp { + /** The logger for this class. */ + private static final Logger logger = Logger.getLogger(EpnTapMainApp.class); + /** Constructor to hide the implicit public one. */ private EpnTapMainApp() { } @@ -40,7 +44,7 @@ public class EpnTapMainApp { */ public static void main(String[] args) { EpnTapController epnTapControl = new EpnTapController(); - Log.LOGGER.info("Lauching EPN-TAP application..."); + logger.info("Lauching EPN-TAP application..."); if (args.length != 0) { System.console().writer().println("Usage: EpnTapMainApp"); return; diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/controller/EpnTapController.java b/src/main/java/eu/omp/irap/vespa/epntapclient/controller/EpnTapController.java index f8e161d..0110657 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/controller/EpnTapController.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/controller/EpnTapController.java @@ -16,8 +16,9 @@ package eu.omp.irap.vespa.epntapclient.controller; +import org.apache.log4j.Logger; + import eu.omp.irap.vespa.epntapclient.utils.Const; -import eu.omp.irap.vespa.epntapclient.utils.Log; import eu.omp.irap.vespa.epntapclient.utils.Queries; import eu.omp.irap.vespa.epntapclient.view.EpnTapMainView; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableController; @@ -28,6 +29,9 @@ import eu.omp.irap.vespa.epntapclient.votable.view.VOTableView; * @author N. Jourdane */ public class EpnTapController { + /** The logger for this class. */ + private static final Logger logger = Logger.getLogger(EpnTapController.class); + /** The view of EPN-TAP application. */ EpnTapMainView view; @@ -85,7 +89,7 @@ public class EpnTapController { String serviceURL = (String) view.getServices().getValueAt(1, row); if (!serviceURL.equals(selectedServiceURL)) { selectedServiceURL = serviceURL; - Log.LOGGER.info("Selected service URL: " + selectedServiceURL); + logger.info("Selected service URL: " + selectedServiceURL); } } @@ -94,7 +98,7 @@ public class EpnTapController { * @throws VOTableException Can not fill the Table */ public void sendQuery(String query) throws VOTableException { - Log.LOGGER.info("Sending query: " + query + " on " + selectedServiceURL); + logger.info("Sending query: " + query + " on " + selectedServiceURL); resultsController.fillTable(selectedServiceURL, "ADQL", query); } } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/utils/Log.java b/src/main/java/eu/omp/irap/vespa/epntapclient/utils/Log.java index 90f59c4..694d645 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/utils/Log.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/utils/Log.java @@ -23,22 +23,25 @@ import java.io.PrintWriter; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; -import java.util.logging.Logger; + +import org.apache.log4j.Logger; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import eu.omp.irap.vespa.epntapclient.EpnTapMainApp; + /** - * Simple class to provide the logger. + * Simple class to provide logging features * * @author N. Jourdane */ public class Log { - /** The logger. */ - public static final Logger LOGGER = Logger.getAnonymousLogger(); + /** The logger for this class. */ + private static final Logger logger = Logger.getLogger(EpnTapMainApp.class); - /** The log message displayed when errors appends. */ - public static final String ERROR_MSG = "-- error --\n%1s\nbecause:\n%2s\n-- end of error --\n"; + /** The log file path used by `logInFile` and `clearLogFile`. */ + private static String logPath = Const.TMP_DIR + "/log"; /** Constructor to hide the implicit public one. */ private Log() { @@ -70,36 +73,39 @@ public class Log { writer.write(json); } catch (IOException e) { - Log.LOGGER.severe("Can not print in the file: \n" + e); + logger.error("Can not print in the file " + path, e); } return path; } - // TODO: Use Log4j instead + // TODO: Use log4j FileAppender instead /** * Delete the content of the log file. */ - public static void clearFile() { - try (PrintWriter writer = new PrintWriter(Const.TMP_DIR + "/log")) { + public static void clearLogFile() { + try (PrintWriter writer = new PrintWriter(logPath)) { writer.print(""); } catch (FileNotFoundException e) { - Log.LOGGER.severe("File not found, can not clear it: \n" + e); + logger.error("File " + logPath + " not found, can not clear it.", e); } } + // TODO: Use log4j FileAppender instead + /** * Print a message in a log file (named "log"). * * @param text The text to log in the file */ public static void logInFile(Object text) { + try { - Files.write(Paths.get(Const.TMP_DIR + "/log"), (text.toString() + "\n").getBytes(), + Files.write(Paths.get(logPath), (text.toString() + "\n").getBytes(), StandardOpenOption.APPEND); } catch (IOException e) { - Log.LOGGER.severe("Can not print in the file: \n" + e); + logger.error("Can not print in the file " + logPath, e); } } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/view/RequestView.java b/src/main/java/eu/omp/irap/vespa/epntapclient/view/RequestView.java index 61f8a06..9151ebf 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/view/RequestView.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/view/RequestView.java @@ -25,7 +25,8 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; -import eu.omp.irap.vespa.epntapclient.utils.Log; +import org.apache.log4j.Logger; + import eu.omp.irap.vespa.epntapclient.utils.Queries; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException; @@ -34,6 +35,9 @@ import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException; */ public class RequestView extends JPanel implements ActionListener { + /** The logger for this class. */ + private static final Logger logger = Logger.getLogger(RequestView.class); + /** The serial version UID (affected with a random number). */ private static final long serialVersionUID = 1262856496809315405L; @@ -69,7 +73,7 @@ public class RequestView extends JPanel implements ActionListener { try { mainView.getController().sendQuery(queryArea.getText()); } catch (VOTableException e) { - Log.LOGGER.warning("Can not send query when clicking on the send button:\n" + e); + logger.error("Can not send query when clicking on the send button.", e); } } } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java index 4a0b91a..c5d75e9 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java @@ -19,9 +19,10 @@ package eu.omp.irap.vespa.epntapclient.votable; import javax.swing.JFrame; import javax.swing.SwingUtilities; +import org.apache.log4j.Logger; + import com.google.gson.Gson; -import eu.omp.irap.vespa.epntapclient.utils.Log; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableController; /** @@ -31,6 +32,9 @@ import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableController; */ public class VOTableApp { + /** The logger for this class. */ + static final Logger logger = Logger.getLogger(VOTableApp.class); + /** Constructor to hide the implicit public one. */ private VOTableApp() { } @@ -55,7 +59,7 @@ public class VOTableApp { public static void main(String[] args) { // TODO: Add option to export to CSV and HTML in CLI. VOTableController voTableControl; - Log.LOGGER.info("Lauching VOTable app with arguments:\n " + new Gson().toJson(args)); + logger.info("Lauching VOTable app with arguments:\n " + new Gson().toJson(args)); if (args.length == 1) { voTableControl = new VOTableController(args[0]); } else if (args.length == 3) { diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java index 0f6d7d9..b8847f4 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java @@ -29,8 +29,9 @@ import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; +import org.apache.log4j.Logger; + import eu.omp.irap.vespa.epntapclient.utils.Const; -import eu.omp.irap.vespa.epntapclient.utils.Log; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.BadRequestException; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.HTTPRequestException; @@ -40,6 +41,10 @@ import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.HTTPRe * @author N. Jourdane */ public final class VOTableConnection { + + /** The logger for this class. */ + private static final Logger logger = Logger.getLogger(VOTableConnection.class); + /** The user agent used for the requests. */ private static final String USER_AGENT = "Mozilla/5.0"; @@ -71,7 +76,7 @@ public final class VOTableConnection { throw new HTTPRequestException("Can not encode URI " + uri, e); } - Log.LOGGER.info("request: " + uri + "?" + parameters); + logger.info("request: " + uri + "?" + parameters); VOTableConnection.sendGet(uri, parameters, voTablePath); return voTablePath; diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java index 63ecc3c..47976c8 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java @@ -18,7 +18,8 @@ package eu.omp.irap.vespa.epntapclient.votable.controller; import java.io.IOException; -import eu.omp.irap.vespa.epntapclient.utils.Log; +import org.apache.log4j.Logger; + import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableParsingException; import eu.omp.irap.vespa.epntapclient.votable.model.Table; import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE; @@ -29,6 +30,9 @@ import eu.omp.irap.vespa.epntapclient.votable.view.VOTableView; */ public class VOTableController { + /** The logger for this class. */ + private static final Logger logger = Logger.getLogger(VOTableController.class); + /** The view of the VOTable */ VOTableView view; @@ -69,7 +73,7 @@ public class VOTableController { try { fillTable(VOTableConnection.sendQuery(targetURL, queryLanguage, query)); } catch (VOTableException e) { - Log.LOGGER.info("VOTableController error: " + e); + logger.info("VOTableController error: " + e); } } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableDataParser.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableDataParser.java index 4fda507..eb9d90f 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableDataParser.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableDataParser.java @@ -23,6 +23,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.log4j.Logger; + import com.google.gson.Gson; import eu.omp.irap.vespa.epntapclient.utils.Log; @@ -37,6 +39,9 @@ import eu.omp.irap.vespa.epntapclient.votable.model.TableData; */ public class VOTableDataParser { + /** The logger for this class. */ + private static final Logger logger = Logger.getLogger(VOTableDataParser.class); + /** * A list of arrays, representing data stored in the VOTable. Each element is a VOTable row, * where arrays elements are in the same order as `columnNames`. @@ -73,7 +78,7 @@ public class VOTableDataParser { for (int i = 0; i < fields.size(); i++) { columnsName[i] = fields.get(i).getName(); } - Log.LOGGER.info("Columns name: " + new Gson().toJson(columnsName)); + logger.info("Columns name: " + new Gson().toJson(columnsName)); data = new ArrayList<>(); @@ -88,7 +93,7 @@ public class VOTableDataParser { } String logPath = Log.printObject(data); - Log.LOGGER.info("A json file representing the VOTable data has been created on " + logPath); + logger.info("A json file representing the VOTable data has been created on " + logPath); } /** @@ -177,14 +182,12 @@ public class VOTableDataParser { * @throws UnsupportedOperationException Data as arrays are not supported yet. */ private void parseBinaryStream(Stream stream, List fields) { - Log.LOGGER.info("Parsing data in BINARY stream..."); + logger.info("Parsing data in BINARY stream..."); String strStream = stream.getValue().replaceAll("(\\r|\\n)", ""); - Log.clearFile(); bytes = Base64.getDecoder().decode(strStream); Object[] row = new Object[columnsName.length]; - Log.clearFile(); int nValue = 0; while (cursor < bytes.length) { int nColumn = nValue % columnsName.length; @@ -258,7 +261,7 @@ public class VOTableDataParser { * @param fields The Fields corresponding to the Table. */ private static void parseBinary2Stream(Stream stream, List fields) { - Log.LOGGER.info("Parsing data in BINARY2 stream..."); + logger.info("Parsing data in BINARY2 stream..."); // TODO Implement parseBinary2Stream() } @@ -267,7 +270,7 @@ public class VOTableDataParser { * @param fields The Fields corresponding to the Table. */ private static void parseTableDataStream(TableData tabledata, List fields) { - Log.LOGGER.info("Parsing data in TABLEDATA stream..."); + logger.info("Parsing data in TABLEDATA stream..."); // TODO Implement parseTableDataStream() } @@ -276,7 +279,7 @@ public class VOTableDataParser { * @param fields The Fields corresponding to the Table. */ private static void parseFITSStream(Stream stream, List fields) { - Log.LOGGER.info("Parsing data in FITS stream..."); + logger.info("Parsing data in FITS stream..."); // TODO Implement parseFITSStream() } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java index 99e4bb7..fb65784 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java @@ -16,9 +16,7 @@ package eu.omp.irap.vespa.epntapclient.votable.controller; -import java.util.logging.Level; - -import eu.omp.irap.vespa.epntapclient.utils.Log; +import org.apache.log4j.Logger; /** * VOTable Exception class. @@ -27,35 +25,42 @@ import eu.omp.irap.vespa.epntapclient.utils.Log; */ @SuppressWarnings({ "javadoc", "serial" }) public class VOTableException extends Exception { + + /** The logger for this class. */ + static final Logger logger = Logger.getLogger(VOTableException.class); + + /** The log message displayed when errors appends. */ + private static final String ERROR_MSG = "-- error --\n%1s\nbecause:\n%2s\n-- end of error --\n"; + public VOTableException() { - Log.LOGGER.log(Level.SEVERE, "A VOTable error occured."); + logger.error("A VOTable error occured."); } public VOTableException(String message) { - Log.LOGGER.log(Level.SEVERE, message); + logger.error(message); } public VOTableException(String message, Exception e) { - Log.LOGGER.log(Level.SEVERE, String.format(Log.ERROR_MSG, message, e.getMessage())); + logger.error(String.format(ERROR_MSG, message, e.getMessage())); } public VOTableException(Exception e) { - Log.LOGGER.log(Level.SEVERE, e.getMessage()); + logger.error(e.getMessage()); } public static class HTTPRequestException extends VOTableException { public HTTPRequestException(String message) { - Log.LOGGER.log(Level.SEVERE, message); + logger.error(message); } public HTTPRequestException(String message, Exception e) { - Log.LOGGER.log(Level.SEVERE, String.format(Log.ERROR_MSG, message, e.getMessage())); + logger.error(String.format(ERROR_MSG, message, e.getMessage())); } } public static class VOTableParsingException extends VOTableException { public VOTableParsingException(String message, Exception e) { - Log.LOGGER.log(Level.SEVERE, String.format(Log.ERROR_MSG, message, e.getMessage())); + logger.error(String.format(ERROR_MSG, message, e.getMessage())); } } @@ -65,7 +70,7 @@ public class VOTableException extends Exception { public BadRequestException(String message, String info) { super(message); this.info = info; - Log.LOGGER.log(Level.WARNING, message + "\nDetails: " + info); + logger.error(message + "\nDetails: " + info); } public String getInfo() { diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java index 61b27c0..7051235 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java @@ -31,11 +31,11 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.xml.sax.SAXException; -import eu.omp.irap.vespa.epntapclient.utils.Log; import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableParsingException; import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE; @@ -44,6 +44,9 @@ import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE; */ public final class VOTableParser { + /** The logger for this class. */ + static final Logger logger = Logger.getLogger(VOTableParser.class); + /** The path of the VOTable to verify the VOTable XML file. */ private static final String VOTABLE_SHEMA = "http://www.ivoa.net/xml/VOTable/v"; @@ -105,11 +108,11 @@ public final class VOTableParser { String version = votAttrs.getNamedItem("version").getTextContent(); if (version != JAXB_VOTABLE_VERSION) { - Log.LOGGER.info("VOTable version is " + version + ", changing VOTable svhema to " + logger.info("VOTable version is " + version + ", changing VOTable svhema to " + JAXB_VOTABLE_VERSION); votAttrs.getNamedItem("xmlns").setTextContent(VOTABLE_SHEMA + JAXB_VOTABLE_VERSION); } else { - Log.LOGGER.info("VOTable version is " + version + ", everything is ok."); + logger.info("VOTable version is " + version + ", everything is ok."); } // write the content into xml file diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java index 2eaf49f..3c25c6f 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java @@ -18,7 +18,6 @@ package eu.omp.irap.vespa.epntapclient.votable.view; import java.awt.BorderLayout; import java.util.List; -import java.util.logging.Level; import javax.swing.JOptionPane; import javax.swing.JPanel; @@ -29,7 +28,7 @@ import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.table.DefaultTableModel; -import eu.omp.irap.vespa.epntapclient.utils.Log; +import org.apache.log4j.Logger; /** * The main class of the View of the application. @@ -37,6 +36,10 @@ import eu.omp.irap.vespa.epntapclient.utils.Log; * @author N. Jourdane */ public class VOTableView extends JPanel implements TableModelListener { + + /** The logger for this class. */ + static final Logger logger = Logger.getLogger(VOTableView.class); + // TODO: Create classes VOTableGUI and VOTableCLI which implements an interface VOTableView /** The serial version UID (affected with a random number). */ private static final long serialVersionUID = -6131752938586134234L; @@ -106,7 +109,7 @@ public class VOTableView extends JPanel implements TableModelListener { */ public void displayError(String message) { JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE); - Log.LOGGER.log(Level.WARNING, message); + logger.warn(message); } /** @@ -116,8 +119,9 @@ public class VOTableView extends JPanel implements TableModelListener { * @param e The exception displayed in the log for debug. */ public void displayError(String message, Exception e) { + String error_msg = "-- user error --\n%1s\nbecause:\n%2s\n-- end of user error --\n"; JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE); - Log.LOGGER.log(Level.SEVERE, String.format(Log.ERROR_MSG, message, e.getMessage())); + logger.warn(String.format(error_msg, message, e.getMessage())); } @Override -- libgit2 0.21.2