You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by sd...@apache.org on 2023/11/29 02:46:20 UTC

(logging-chainsaw) branch master updated: Add VFSLogFilePatternReceiver, minor tweaks to LogFilePatternReceiver

This is an automated email from the ASF dual-hosted git repository.

sdeboy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-chainsaw.git


The following commit(s) were added to refs/heads/master by this push:
     new 8763fec  Add VFSLogFilePatternReceiver, minor tweaks to LogFilePatternReceiver
8763fec is described below

commit 8763fecca588f0503832c85a505987001de2c589
Author: sdeboy <sd...@apache.org>
AuthorDate: Tue Nov 28 18:46:12 2023 -0800

    Add VFSLogFilePatternReceiver, minor tweaks to LogFilePatternReceiver
---
 .../vfs/VFSLogFilePatternReceiverFactory.java      | 144 +++++++++++++++++++++
 .../apache/log4j/varia/LogFilePatternReceiver.java | 101 +++++++--------
 ...log4j.chainsaw.receiver.ChainsawReceiverFactory |   1 +
 3 files changed, 191 insertions(+), 55 deletions(-)

diff --git a/src/main/java/org/apache/log4j/chainsaw/vfs/VFSLogFilePatternReceiverFactory.java b/src/main/java/org/apache/log4j/chainsaw/vfs/VFSLogFilePatternReceiverFactory.java
new file mode 100644
index 0000000..8aeec19
--- /dev/null
+++ b/src/main/java/org/apache/log4j/chainsaw/vfs/VFSLogFilePatternReceiverFactory.java
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.log4j.chainsaw.vfs;
+
+import java.beans.IntrospectionException;
+import java.beans.PropertyDescriptor;
+import org.apache.log4j.chainsaw.receiver.ChainsawReceiver;
+import org.apache.log4j.chainsaw.receiver.ChainsawReceiverFactory;
+import org.apache.log4j.varia.LogFilePatternReceiver;
+
+/**
+ *
+ */
+public class VFSLogFilePatternReceiverFactory implements ChainsawReceiverFactory {
+
+    @Override
+    public ChainsawReceiver create() {
+        return new VFSLogFilePatternReceiver();
+    }
+
+    @Override
+    public PropertyDescriptor[] getPropertyDescriptors() throws IntrospectionException {
+        return new PropertyDescriptor[]{
+            new PropertyDescriptor("name", LogFilePatternReceiver.class),
+            new PropertyDescriptor("fileURL", LogFilePatternReceiver.class),
+            new PropertyDescriptor("appendNonMatches", LogFilePatternReceiver.class),
+            new PropertyDescriptor("filterExpression", LogFilePatternReceiver.class),
+            new PropertyDescriptor("tailing", LogFilePatternReceiver.class),
+            new PropertyDescriptor("logFormat", LogFilePatternReceiver.class),
+            new PropertyDescriptor("group", LogFilePatternReceiver.class),
+            new PropertyDescriptor("timestampFormat", LogFilePatternReceiver.class),
+            new PropertyDescriptor("waitMillis", LogFilePatternReceiver.class),
+        };
+    }
+
+    @Override
+    public String getReceiverName() {
+        return "VFSLogFilePatternReceiver";
+    }
+
+    @Override
+    public String getReceiverDocumentation() {
+        return "<html>VFSLogFilePatternReceiver can parse and tail log files, converting entries into\n" +
+"LoggingEvents.  If the file doesn't exist when the receiver is initialized, the\n" +
+"receiver will look for the file once every 10 seconds.\n" +
+"<p>\n" +
+"This receiver relies on java.util.regex features to perform the parsing of text in the\n" +
+"log file, however the only regular expression field explicitly supported is\n" +
+" * a glob-style wildcard used to ignore fields in the log file if needed.  All other\n" +
+"fields are parsed by using the supplied keywords.\n" +
+"<p>\n" +
+"<b>Features:</b><br>\n" +
+"- specify the URL of the log file to be processed<br>\n" +
+"- specify the timestamp format in the file (if one exists, using patterns from {@link java.text.SimpleDateFormat})<br>\n" +
+"- specify the pattern (logFormat) used in the log file using keywords, a wildcard character (*) and fixed text<br>\n" +
+"- 'tail' the file (allows the contents of the file to be continually read and new events processed)<br>\n" +
+"- supports the parsing of multi-line messages and exceptions\n" +
+"- 'hostname' property set to URL host (or 'file' if not available)\n" +
+"- 'application' property set to URL path (or value of fileURL if not available)\n" +
+"- 'group' property can be set to associate multiple log file receivers\n" +
+"<p>\n" +
+"<b>Keywords:</b><br>\n" +
+"TIMESTAMP<br>\n" +
+"LOGGER<br>\n" +
+"LEVEL<br>\n" +
+"THREAD<br>\n" +
+"CLASS<br>\n" +
+"FILE<br>\n" +
+"LINE<br>\n" +
+"METHOD<br>\n" +
+"RELATIVETIME<br>\n" +
+"MESSAGE<br>\n" +
+"NDC<br>\n" +
+"PROP(key)<br>\n" +
+"(NL)<br>\n" +
+"<p>\n" +
+"(NL) represents a new line embedded in the log format, supporting log formats whose fields span multiple lines\n" +
+"<p>\n" +
+"Use a * to ignore portions of the log format that should be ignored\n" +
+"<p>\n" +
+"Example:<br>\n" +
+"If your file's patternlayout is this:<br>\n" +
+"<b>%d %-5p [%t] %C{2} (%F:%L) - %m%n</b>\n" +
+"<p>\n" +
+"specify this as the log format:<br>\n" +
+"<b>TIMESTAMP LEVEL [THREAD] CLASS (FILE:LINE) - MESSAGE</b>\n" +
+"<p>\n" +
+"To define a PROPERTY field, use PROP(key)\n" +
+"<p>\n" +
+"Example:<br>\n" +
+"If you used the RELATIVETIME pattern layout character in the file,\n" +
+"you can use PROP(RELATIVETIME) in the logFormat definition to assign\n" +
+"the RELATIVETIME field as a property on the event.\n" +
+"<p>\n" +
+"If your file's patternlayout is this:<br>\n" +
+"<b>%r [%t] %-5p %c %x - %m%n</b>\n" +
+"<p>\n" +
+"specify this as the log format:<br>\n" +
+"<b>PROP(RELATIVETIME) [THREAD] LEVEL LOGGER * - MESSAGE</b>\n" +
+"<p>\n" +
+"Note the * - it can be used to ignore a single word or sequence of words in the log file\n" +
+"(in order for the wildcard to ignore a sequence of words, the text being ignored must be\n" +
+"followed by some delimiter, like '-' or '[') - ndc is being ignored in the following example.\n" +
+"<p>\n" +
+"Assign a filterExpression in order to only process events which match a filter.\n" +
+"If a filterExpression is not assigned, all events are processed.\n" +
+"<p>\n" +
+"<b>Limitations:</b><br>\n" +
+"- no support for the single-line version of throwable supported by patternlayout<br>\n" +
+"(this version of throwable will be included as the last line of the message)<br>\n" +
+"- the relativetime patternLayout character must be set as a property: PROP(RELATIVETIME)<br>\n" +
+"- messages should appear as the last field of the logFormat because the variability in message content<br>\n" +
+"- exceptions are converted if the exception stack trace (other than the first line of the exception)<br>\n" +
+"is stored in the log file with a tab followed by the word 'at' as the first characters in the line<br>\n" +
+"- tailing may fail if the file rolls over.\n" +
+"<p>\n" +
+"<b>Example receiver configuration settings</b> (add these as params, specifying a LogFilePatternReceiver 'plugin'):<br>\n" +
+"param: \"timestampFormat\" value=\"yyyy-MM-d HH:mm:ss,SSS\"<br>\n" +
+"param: \"logFormat\" value=\"PROP(RELATIVETIME) [THREAD] LEVEL LOGGER * - MESSAGE\"<br>\n" +
+"param: \"fileURL\" value=\"file:///c:/events.log\"<br>\n" +
+"param: \"tailing\" value=\"true\"\n" +
+"<p>\n" +
+"This configuration will be able to process these sample events:<br>\n" +
+"710    [       Thread-0] DEBUG                   first.logger first - &lt;test&gt;   &lt;test2&gt;something here&lt;/test2&gt;   &lt;test3 blah=something/&gt;   &lt;test4&gt;       &lt;test5&gt;something else&lt;/test5&gt;   &lt;/test4&gt;&lt;/test&gt;<br>\n" +
+"880    [       Thread-2] DEBUG                   first.logger third - &lt;test&gt;   &lt;test2&gt;something here&lt;/test2&gt;   &lt;test3 blah=something/&gt;   &lt;test4&gt;       &lt;test5&gt;something else&lt;/test5&gt;   &lt;/test4&gt;&lt;/test&gt;<br>\n" +
+"880    [       Thread-0] INFO                    first.logger first - infomsg-0<br>\n" +
+"java.lang.Exception: someexception-first<br>\n" +
+"at Generator2.run(Generator2.java:102)<br>";
+    }
+}
diff --git a/src/main/java/org/apache/log4j/varia/LogFilePatternReceiver.java b/src/main/java/org/apache/log4j/varia/LogFilePatternReceiver.java
index 8e3559f..d384072 100644
--- a/src/main/java/org/apache/log4j/varia/LogFilePatternReceiver.java
+++ b/src/main/java/org/apache/log4j/varia/LogFilePatternReceiver.java
@@ -17,6 +17,7 @@
 
 package org.apache.log4j.varia;
 
+import java.nio.charset.StandardCharsets;
 import org.apache.log4j.helpers.Constants;
 import org.apache.log4j.rule.ExpressionRule;
 import org.apache.log4j.rule.Rule;
@@ -180,7 +181,7 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
 
     private Rule expressionRule;
 
-    private Map currentMap;
+    private Map<String, String> currentMap;
     private List<String> additionalLines;
     private List<String> matchingKeywords;
 
@@ -200,7 +201,7 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
 
     protected boolean active = false;
 
-    private ChainsawLoggingEventBuilder build = new ChainsawLoggingEventBuilder();
+    private final ChainsawLoggingEventBuilder build = new ChainsawLoggingEventBuilder();
 
     private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger();
 
@@ -437,7 +438,7 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
      * @return message
      */
     private String buildMessage(String firstMessageLine, int exceptionLine) {
-        if (additionalLines.size() == 0) {
+        if (additionalLines.isEmpty()) {
             return firstMessageLine;
         }
         StringBuilder message = new StringBuilder();
@@ -483,8 +484,8 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
      * @return event
      */
     private ChainsawLoggingEvent buildEvent() {
-        if (currentMap.size() == 0) {
-            if (additionalLines.size() > 0) {
+        if (currentMap.isEmpty()) {
+            if (!additionalLines.isEmpty()) {
                 for (Object additionalLine : additionalLines) {
                     logger.debug("found non-matching line: " + additionalLine);
                 }
@@ -497,8 +498,8 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
         String[] exception = buildException(exceptionLine);
 
         //messages are listed before exceptions in additionallines
-        if (additionalLines.size() > 0 && exception.length > 0) {
-            currentMap.put(MESSAGE, buildMessage((String) currentMap.get(MESSAGE),
+        if (!additionalLines.isEmpty() && exception.length > 0) {
+            currentMap.put(MESSAGE, buildMessage(currentMap.get(MESSAGE),
                 exceptionLine));
         }
         ChainsawLoggingEvent event = convertToEvent(currentMap, exception);
@@ -612,14 +613,13 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
      * @param result
      * @return map
      */
-    private Map processEvent(MatchResult result) {
-        Map map = new HashMap();
+    private Map<String, String> processEvent(MatchResult result) {
+        Map<String, String> map = new HashMap<>();
         //group zero is the entire match - process all other groups
         for (int i = 1; i < result.groupCount() + 1; i++) {
-            Object key = matchingKeywords.get(i - 1);
-            Object value = result.group(i);
+            String key = matchingKeywords.get(i - 1);
+            String value = result.group(i);
             map.put(key, value);
-
         }
         return map;
     }
@@ -675,7 +675,7 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
             path = fileURL;
         }
 
-        currentMap = new HashMap();
+        currentMap = new HashMap<>();
         additionalLines = new ArrayList<>();
         matchingKeywords = new ArrayList<>();
 
@@ -736,19 +736,18 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
          * contain only one of these per entry...properties are the only 'keyword'
          * that can occur multiple times in an entry
          */
-        for (Object keyword1 : keywords) {
-            String keyword = (String) keyword1;
-            int index2 = newPattern.indexOf(keyword);
+        for (String keyword1 : keywords) {
+            int index2 = newPattern.indexOf(keyword1);
             if (index2 > -1) {
-                buildingKeywords.add(keyword);
-                newPattern = singleReplace(newPattern, keyword, Integer.toString(buildingKeywords.size() - 1));
+                buildingKeywords.add(keyword1);
+                newPattern = singleReplace(newPattern, keyword1, Integer.toString(buildingKeywords.size() - 1));
             }
         }
 
         StringBuilder buildingInt = new StringBuilder();
 
         for (int i = 0; i < newPattern.length(); i++) {
-            String thisValue = String.valueOf(newPattern.substring(i, i + 1));
+            String thisValue = newPattern.substring(i, i + 1);
             if (isInteger(thisValue)) {
                 buildingInt.append(thisValue);
             } else {
@@ -799,7 +798,7 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
             customLevelDefinitionMap.clear();
             while (entryTokenizer.hasMoreTokens()) {
                 StringTokenizer innerTokenizer = new StringTokenizer(entryTokenizer.nextToken(), "=");
-//                customLevelDefinitionMap.put(innerTokenizer.nextToken(), Level.toLevel(innerTokenizer.nextToken()));
+                customLevelDefinitionMap.put(innerTokenizer.nextToken(), Level.valueOf(innerTokenizer.nextToken()));
             }
         }
     }
@@ -898,7 +897,7 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
      * @param exception
      * @return logging event
      */
-    private ChainsawLoggingEvent convertToEvent(Map fieldMap, String[] exception) {
+    private ChainsawLoggingEvent convertToEvent(Map<String, String> fieldMap, String[] exception) {
         if (fieldMap == null) {
             return null;
         }
@@ -911,7 +910,7 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
             exception = emptyException;
         }
 
-        String logger;
+        String loggerValue;
         long timeStamp = 0L;
         String level;
         String threadName;
@@ -921,13 +920,13 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
         String methodName;
         String eventFileName;
         String lineNumber;
-        Hashtable properties = new Hashtable();
+        Map<String, String> properties = new HashMap<>();
 
-        logger = (String) fieldMap.remove(LOGGER);
+        loggerValue = fieldMap.remove(LOGGER);
 
         if ((dateFormat != null) && fieldMap.containsKey(TIMESTAMP)) {
             try {
-                timeStamp = dateFormat.parse((String) fieldMap.remove(TIMESTAMP))
+                timeStamp = dateFormat.parse(fieldMap.remove(TIMESTAMP))
                     .getTime();
             } catch (Exception e) {
                 e.printStackTrace();
@@ -938,43 +937,35 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
             timeStamp = System.currentTimeMillis();
         }
 
-        message = (String)fieldMap.remove(MESSAGE);
+        message = fieldMap.remove(MESSAGE);
         if (message == null) {
             message = "";
         }
 
-        level = (String) fieldMap.remove(LEVEL);
-//        Level levelImpl;
-//        if (level == null) {
-//            levelImpl = Level.DEBUG;
-//        } else {
-//            //first try to resolve against custom level definition map, then fall back to regular levels
-//            levelImpl = customLevelDefinitionMap.get(level);
-//            if (levelImpl == null) {
-//                levelImpl = Level.toLevel(level.trim());
-//                if (!level.equals(levelImpl.toString())) {
-//                    //check custom level map
-//                    if (levelImpl == null) {
-//                        levelImpl = Level.DEBUG;
-//                        logger.debug("found unexpected level: " + level + ", logger: " + logger.getName() + ", msg: " + message);
-//                        //make sure the text that couldn't match a level is added to the message
-//                        message = level + " " + message;
-//                    }
-//                }
-//            }
-//        }
+        level = fieldMap.remove(LEVEL);
+        Level levelImpl;
+        if (level == null) {
+            levelImpl = Level.DEBUG;
+        } else {
+            //first try to resolve against custom level definition map, then fall back to regular levels
+            levelImpl = customLevelDefinitionMap.get(level);
+            if (levelImpl == null) {
+                levelImpl = Level.valueOf(level.trim());
+            }
+        }
+        level = levelImpl.name();
 
-        threadName = (String) fieldMap.remove(THREAD);
+        threadName = fieldMap.remove(THREAD);
 
-        ndc = (String) fieldMap.remove(NDC);
+        ndc = fieldMap.remove(NDC);
 
-        className = (String) fieldMap.remove(CLASS);
+        className = fieldMap.remove(CLASS);
 
-        methodName = (String) fieldMap.remove(METHOD);
+        methodName = fieldMap.remove(METHOD);
 
-        eventFileName = (String) fieldMap.remove(FILE);
+        eventFileName = fieldMap.remove(FILE);
 
-        lineNumber = (String) fieldMap.remove(LINE);
+        lineNumber = fieldMap.remove(LINE);
 
         properties.put(Constants.HOSTNAME_KEY, host);
         properties.put(Constants.APPLICATION_KEY, path);
@@ -990,12 +981,12 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
 
         if ((eventFileName != null) || (className != null) || (methodName != null)
             || (lineNumber != null)) {
-            info = new LocationInfo(eventFileName, className, methodName, 
+            info = new LocationInfo(eventFileName, className, methodName,
                     Integer.parseInt(lineNumber));
         }
 
         build.clear();
-        build.setLogger(logger)
+        build.setLogger(loggerValue)
                 .setTimestamp(Instant.ofEpochMilli(timeStamp))
                 .setLevelFromString(level)
                 .setMessage(message)
@@ -1053,7 +1044,7 @@ public class LogFilePatternReceiver extends ChainsawReceiverSkeleton {
                 while (reader == null) {
                     logger.info("attempting to load file: " + getFileURL());
                     try {
-                        reader = new InputStreamReader(new URL(getFileURL()).openStream(), "UTF-8");
+                        reader = new InputStreamReader(new URL(getFileURL()).openStream(), StandardCharsets.UTF_8);
                     } catch (FileNotFoundException fnfe) {
                         logger.info("file not available - will try again");
                         synchronized (this) {
diff --git a/src/main/resources/META-INF/services/org.apache.log4j.chainsaw.receiver.ChainsawReceiverFactory b/src/main/resources/META-INF/services/org.apache.log4j.chainsaw.receiver.ChainsawReceiverFactory
index ea467ac..3ae791d 100644
--- a/src/main/resources/META-INF/services/org.apache.log4j.chainsaw.receiver.ChainsawReceiverFactory
+++ b/src/main/resources/META-INF/services/org.apache.log4j.chainsaw.receiver.ChainsawReceiverFactory
@@ -3,3 +3,4 @@ org.apache.log4j.net.UDPReceiverFactory
 org.apache.log4j.net.XMLReceiverFactory
 org.apache.log4j.varia.LogFilePatternReceiverFactory
 org.apache.log4j.net.JsonReceiverFactory
+org.apache.log4j.chainsaw.vfs.VFSLogFilePatternReceiverFactory