bema/func/log4j_logging #6
| @@ -14,6 +14,7 @@ | ||||
|     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||||
|     <jackson.version>2.14.1</jackson.version> | ||||
|     <slf4j.version>2.0.6</slf4j.version> | ||||
|     <log4j.version>2.19.0</log4j.version> | ||||
|   </properties> | ||||
|  | ||||
|   <build> | ||||
| @@ -133,8 +134,15 @@ | ||||
|     <dependency> | ||||
|       <groupId>org.apache.logging.log4j</groupId> | ||||
|       <artifactId>log4j-core</artifactId> | ||||
|       <version>2.19.0</version> | ||||
|       <version>${log4j.version}</version> | ||||
|     </dependency> | ||||
|  | ||||
|     <dependency> | ||||
|       <groupId>org.apache.logging.log4j</groupId> | ||||
|       <artifactId>log4j-api</artifactId> | ||||
|       <version>${log4j.version}</version> | ||||
|     </dependency> | ||||
|  | ||||
|  | ||||
|   </dependencies> | ||||
| </project> | ||||
| @@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.JsonMappingException; | ||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||
| import com.r11.tools.xslt.processors.Saxon; | ||||
| import com.r11.tools.xslt.processors.Xalan; | ||||
| import org.apache.logging.log4j.LogManager; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import spark.*; | ||||
|  | ||||
| import java.util.HashMap; | ||||
| @@ -14,6 +16,9 @@ import java.util.Map; | ||||
|  * @author Wojciech Czop | ||||
|  */ | ||||
| public class SparkInitializer { | ||||
|  | ||||
|     private static final Logger LOG = LogManager.getLogger(SparkInitializer.class); | ||||
|  | ||||
|     /** | ||||
|      * Initializes spark framework | ||||
|      */ | ||||
| @@ -32,7 +37,8 @@ public class SparkInitializer { | ||||
|         Spark.post("/xpathpost", xpathHandler); | ||||
|         Spark.post("/xsdpost", xsdHandler); | ||||
|         Spark.get("/procinfo", procinfoHandler); | ||||
|         System.out.println("Server is online"); | ||||
|  | ||||
|         LOG.info("Server is online at port: " + Spark.port()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -53,7 +59,7 @@ public class SparkInitializer { | ||||
|      */ | ||||
|     private static Route xsdHandler = (Request req, Response resp) -> { | ||||
|         String body = req.body(); | ||||
|         System.out.println("Request: " + body); | ||||
|         LOG.info("Request: " + body); | ||||
|         ObjectMapper mapper = new ObjectMapper(); | ||||
|         Map<String, String> requestMap = null; | ||||
|         Map<String, String> responseMap = new HashMap<>(); | ||||
| @@ -76,12 +82,13 @@ public class SparkInitializer { | ||||
|             responseMap.put("result", tmp); | ||||
|             responseMap.put("status", "OK"); | ||||
|         } catch (Exception ex) { | ||||
|             System.out.println("Exception: "+ex.getMessage()); | ||||
|             LOG.error("Exception: "+ ex.getMessage()); | ||||
|             responseMap.put("result", ex.getMessage()); | ||||
|             responseMap.put("status", "ERR"); | ||||
|             resp.status(400); | ||||
|         } | ||||
|         long duration = System.currentTimeMillis() - timeStart; | ||||
|         LOG.info("Request: " + body + " processed in " + duration + " ms."); | ||||
|         responseMap.put("processor", Xalan.getVersion()); | ||||
|         responseMap.put("time", "" + duration); | ||||
|         resp.body(mapper.writeValueAsString(responseMap)); | ||||
| @@ -92,16 +99,16 @@ public class SparkInitializer { | ||||
|      * Handler that returns output of xpath query and processor data | ||||
|      */ | ||||
|     private static Route xpathHandler = (Request req, Response resp) -> { | ||||
|  | ||||
|         String body = req.body(); | ||||
|         System.out.println("Request: " + body); | ||||
|         LOG.info("Request: " + body); | ||||
|         ObjectMapper mapper = new ObjectMapper(); | ||||
|         Map<String, String> requestMap = null; | ||||
|         Map<String, String> responseMap = new HashMap<>(); | ||||
|         try { | ||||
|             requestMap = mapper.readValue(body, Map.class); | ||||
|         } catch (JsonMappingException ex) { | ||||
|             ex.printStackTrace(); | ||||
|             LOG.error("Exception: "+ ex.getMessage()); | ||||
|             //ex.printStackTrace(); | ||||
|         } | ||||
|  | ||||
|         String data = requestMap.get("data"); | ||||
| @@ -109,6 +116,7 @@ public class SparkInitializer { | ||||
|         String processor = requestMap.get("processor"); | ||||
|         String version = requestMap.get("version"); | ||||
|  | ||||
|  | ||||
|         String tmp = ""; | ||||
|         long timeStart; | ||||
|         long duration; | ||||
| @@ -120,19 +128,21 @@ public class SparkInitializer { | ||||
|             switch (processor) { | ||||
|                 case "saxon": | ||||
|                     resp.header("processor", "Saxon " + Saxon.getVersion() + " " + version + " over s9api"); | ||||
|                     System.out.print("Processing start..."); | ||||
|                     //LOG.info("Processing start..."); | ||||
|                     timeStart = System.currentTimeMillis(); | ||||
|                     try { | ||||
|                         tmp = Saxon.processXPath(data, query, version).trim(); | ||||
|                         responseMap.put("result", tmp); | ||||
|                         responseMap.put("status", "OK"); | ||||
|                     } catch (Exception ex) { | ||||
|                         LOG.error("Exception: "+ ex.getMessage()); | ||||
|                         responseMap.put("result", ex.getMessage()); | ||||
|                         responseMap.put("status", "ERR"); | ||||
|                         resp.status(400); | ||||
|                     } | ||||
|                     duration = System.currentTimeMillis() - timeStart; | ||||
|                     System.out.println("end(" + duration + ")"); | ||||
|                     //System.out.println("end(" + duration + ")"); | ||||
|                     LOG.info("Request" + body + " processed in " + duration + " ms."); | ||||
|                     responseMap.put("processor", "Saxon " + Saxon.getVersion() + " " + version + " over s9api"); | ||||
|                     responseMap.put("time", "" + duration); | ||||
|                     resp.body(mapper.writeValueAsString(responseMap)); | ||||
| @@ -146,6 +156,7 @@ public class SparkInitializer { | ||||
|                         responseMap.put("result", tmp); | ||||
|                         responseMap.put("status", "OK"); | ||||
|                     } catch (Exception ex) { | ||||
|                         LOG.error("Exception: "+ ex.getMessage()); | ||||
|                         responseMap.put("result", ex.getMessage()); | ||||
|                         responseMap.put("status", "ERR"); | ||||
|                         resp.status(400); | ||||
| @@ -177,9 +188,11 @@ public class SparkInitializer { | ||||
|         Map<String, String> responseMap = new HashMap<>(); | ||||
|         try { | ||||
|             jsonMap = mapper.readValue(body, Map.class); | ||||
|             System.out.println(jsonMap); | ||||
|             LOG.info(jsonMap); | ||||
|             //System.out.println(jsonMap); | ||||
|         } catch (JsonMappingException ex) { | ||||
|             ex.printStackTrace(); | ||||
|             LOG.error("Exception: "+ ex.getMessage()); | ||||
|             //ex.printStackTrace(); | ||||
|         } | ||||
|         String data = jsonMap.get("data"); | ||||
|         String query = jsonMap.get("process"); | ||||
| @@ -201,6 +214,7 @@ public class SparkInitializer { | ||||
|                         responseMap.put("result", tmp); | ||||
|                         responseMap.put("status", "OK"); | ||||
|                     } catch (Exception ex) { | ||||
|                         LOG.error("Exception: "+ ex.getMessage()); | ||||
|                         responseMap.put("result", ex.getMessage()); | ||||
|                         responseMap.put("status", "ERR"); | ||||
|                         resp.status(400); | ||||
| @@ -220,6 +234,7 @@ public class SparkInitializer { | ||||
|                         responseMap.put("result", tmp); | ||||
|                         responseMap.put("status", "OK"); | ||||
|                     } catch (Exception ex) { | ||||
|                         LOG.error("Exception: "+ ex.getMessage()); | ||||
|                         responseMap.put("result", ex.getMessage()); | ||||
|                         responseMap.put("status", "ERR"); | ||||
|                         resp.status(400); | ||||
|   | ||||
| @@ -4,6 +4,8 @@ import net.sf.saxon.om.NamespaceBinding; | ||||
| import net.sf.saxon.om.NamespaceMap; | ||||
| import net.sf.saxon.s9api.XPathCompiler; | ||||
| import net.sf.saxon.s9api.XdmNode; | ||||
| import org.apache.logging.log4j.LogManager; | ||||
| import org.apache.logging.log4j.Logger; | ||||
|  | ||||
| import java.util.Iterator; | ||||
|  | ||||
| @@ -13,6 +15,7 @@ import java.util.Iterator; | ||||
|  * @author Wojciech Czop | ||||
|  */ | ||||
| public class NewNamespaceResolver { | ||||
|     private static final Logger LOG = LogManager.getLogger("NewNamespaceResolver"); | ||||
|  | ||||
|     private NamespaceMap namespaceMap; | ||||
|  | ||||
| @@ -63,10 +66,7 @@ public class NewNamespaceResolver { | ||||
|                 XdmNode rNode = it.next(); | ||||
| //                TODO: remove | ||||
|                 if(rNode.getUnderlyingNode().getPrefix().isEmpty() && !rNode.getParent().getUnderlyingNode().getPrefix().isEmpty()){ | ||||
|  | ||||
|                     System.out.println("prefix missing, parent has "+rNode.getParent().getUnderlyingNode().getPrefix() + ", but child has none"); | ||||
|  | ||||
|                     System.out.println(); | ||||
|                     LOG.warn("Missing prefix. Parent has " + rNode.getParent().getUnderlyingNode().getPrefix() + ", but child has none"); | ||||
|                 } | ||||
| //                end | ||||
|                 extractNamespace(rNode); | ||||
|   | ||||
| @@ -1,6 +1,9 @@ | ||||
| package com.r11.tools.xslt.processors; | ||||
|  | ||||
| import net.sf.saxon.s9api.*; | ||||
| import org.apache.logging.log4j.LogManager; | ||||
| import org.apache.logging.log4j.Logger; | ||||
|  | ||||
| import javax.xml.transform.stream.StreamSource; | ||||
| import java.io.StringReader; | ||||
| import java.io.StringWriter; | ||||
| @@ -11,6 +14,8 @@ import java.io.StringWriter; | ||||
|  */ | ||||
| public class Saxon { | ||||
|  | ||||
|     private static final Logger LOG = LogManager.getLogger("Saxon"); | ||||
|  | ||||
|     /** | ||||
|      * Transforms string containing xml document via xslt | ||||
|      * @param data xml to be transformed | ||||
|   | ||||
| @@ -1,5 +1,7 @@ | ||||
| package com.r11.tools.xslt.processors; | ||||
|  | ||||
| import org.apache.logging.log4j.LogManager; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import org.w3c.dom.Document; | ||||
| import org.xml.sax.InputSource; | ||||
|  | ||||
| @@ -25,6 +27,9 @@ import java.io.StringWriter; | ||||
|  * @author Wojciech Czop | ||||
|  */ | ||||
| public class Xalan { | ||||
|  | ||||
|     private static final Logger LOG = LogManager.getLogger("Xalan"); | ||||
|  | ||||
|     /** | ||||
|      * Transforms string containing xml document via xslt | ||||
|      * @param data xml to be transformed | ||||
|   | ||||
							
								
								
									
										21
									
								
								Backend/xslt-rest/src/main/resources/log4j2.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								Backend/xslt-rest/src/main/resources/log4j2.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <Configuration status="WARN"> | ||||
| 	<Appenders> | ||||
| 		<Console name="Console" target="SYSTEM_OUT"> | ||||
| 			<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level - %msg%n"/> | ||||
| 		</Console> | ||||
| 		<File name="File" fileName="/tmp/log4j2.log" append="true"> | ||||
| 			<PatternLayout> | ||||
| 				<Pattern>%d{HH:mm:ss.SSS} [%t] %-5level - %msg%n</Pattern> | ||||
| 			</PatternLayout> | ||||
| 		</File> | ||||
| 	</Appenders> | ||||
| 	<Loggers> | ||||
| 		<Logger name="com.r11.tools.xslt.SparkInitializer" level="info" additivity="true"> | ||||
| 			<AppenderRef ref="Console"/> | ||||
| 		</Logger> | ||||
| 		<Root level="info"> | ||||
| 			<AppenderRef ref="File"/> | ||||
| 		</Root> | ||||
| 	</Loggers> | ||||
| </Configuration> | ||||
		Reference in New Issue
	
	Block a user