modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/event/log/Log4JLogEntryProcessor.java | 26 - modules/core/plugin-api/src/test/java/org/rhq/core/pluginapi/event/log/Log4JLogEntryProcessorTest.java | 251 ++++++---- 2 files changed, 179 insertions(+), 98 deletions(-)
New commits: commit f9a99a4e1f83f3bc74b4fae5a8515914f69b15a5 Author: Jay Shaughnessy jshaughn@redhat.com Date: Thu Jan 12 13:46:23 2012 -0500
[Bug 535725 - (RHQ-2391) Logfile parser ignores lines where severity is not surrounded by spaces] I've updated our logEntry pattern handling to allow for bracket or paren delimiters, with optional spacing, around the severity. So, the following should all now match, for example:
2012-01-05 11:42:13,607 INFO [org.foo.Baz] my message 2012-01-05 11:42:13,607 [INFO] [org.foo.Baz] my message 2012-01-05 11:42:13,607 ( INFO ) [org.foo.Baz] my message
diff --git a/modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/event/log/Log4JLogEntryProcessor.java b/modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/event/log/Log4JLogEntryProcessor.java index 8b5caa0..9eb4f09 100644 --- a/modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/event/log/Log4JLogEntryProcessor.java +++ b/modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/event/log/Log4JLogEntryProcessor.java @@ -35,15 +35,23 @@ import org.rhq.core.domain.event.EventSeverity;
/** * A {@link org.rhq.core.pluginapi.event.log.LogEntryProcessor} for Log4J log files. - * + * <pre> + * By default we support log entries of the format: date [delimiter]severity[delimiter] message + * Where: + * - the optional delimiters are either square brackets ort parens, with or without spaces + * - the date format is one of the built-ins below, or a user-specified date format for the logfile source + * - the message portion may contain multiple lines. + * For example: + * 2007-12-21 15:32:49,514 DEBUG [com.example.FooBar] run: IdleRemover notifying pools, interval: 450000 + * 2007/12/21 15:32:49,514 (DEBUG) [com.example.FooBar] run: IdleRemover notifying pools, interval: 450000 + * 2007-Dec-21 15:32:49 [ DEBUG ] [com.example.FooBar] run: IdleRemover notifying pools, interval: 450000 + * </pre> * @author Ian Springer */ public class Log4JLogEntryProcessor extends MultiLineLogEntryProcessor { - // For now, we only support the default pattern: date priority '['category']' message - // e.g.: 2007-12-09 15:32:49,514 DEBUG [com.example.FooBar] run: IdleRemover notifying pools, interval: 450000 - // NOTE: The message portion may contain multiple lines. - private static final String REGEX = "(.*?) (TRACE|DEBUG|INFO|WARN|ERROR|FATAL) (.*)"; - private static final Pattern PATTERN = Pattern.compile(REGEX); + + private static final String REGEX; + private static final Pattern PATTERN;
private static final String ISO8601_DATE_PATTERN = "yyyy-MM-dd kk:mm:ss,SSS"; private static final DateFormat ISO8601_DATE_FORMAT = new SimpleDateFormat(ISO8601_DATE_PATTERN); @@ -55,6 +63,12 @@ public class Log4JLogEntryProcessor extends MultiLineLogEntryProcessor { private static final Map<Priority, EventSeverity> PRIORITY_TO_SEVERITY_MAP = new LinkedHashMap<Priority, EventSeverity>();
static { + // just in case there is something unanticipated that our default pattern doesn't like, allow + // a backdoor prop to set the REGEX pattern. + String regex = System.getProperty("rhq.agent.event.log4j.regex"); + REGEX = (null != regex) ? regex : "(.*?) [\[\(]??\s*(TRACE|DEBUG|INFO|WARN|ERROR|FATAL)\s*[\]\)]?? (.*)"; + PATTERN = Pattern.compile(REGEX); + PRIORITY_TO_SEVERITY_MAP.put(Priority.TRACE, EventSeverity.DEBUG); PRIORITY_TO_SEVERITY_MAP.put(Priority.DEBUG, EventSeverity.DEBUG); PRIORITY_TO_SEVERITY_MAP.put(Priority.INFO, EventSeverity.INFO); diff --git a/modules/core/plugin-api/src/test/java/org/rhq/core/pluginapi/event/log/Log4JLogEntryProcessorTest.java b/modules/core/plugin-api/src/test/java/org/rhq/core/pluginapi/event/log/Log4JLogEntryProcessorTest.java index e38e1be..78d98ce 100644 --- a/modules/core/plugin-api/src/test/java/org/rhq/core/pluginapi/event/log/Log4JLogEntryProcessorTest.java +++ b/modules/core/plugin-api/src/test/java/org/rhq/core/pluginapi/event/log/Log4JLogEntryProcessorTest.java @@ -1,92 +1,159 @@ - /* - * RHQ Management Platform - * Copyright (C) 2005-2008 Red Hat, Inc. - * All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License, version 2, as - * published by the Free Software Foundation, and/or the GNU Lesser - * General Public License, version 2.1, also as published by the Free - * Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License and the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU General Public License - * and the GNU Lesser General Public License along with this program; - * if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.rhq.core.pluginapi.event.log; - -import java.io.BufferedReader; -import java.io.File; -import java.io.StringReader; -import java.util.Calendar; -import java.util.Iterator; -import java.util.Set; -import java.util.regex.Pattern; - -import org.testng.annotations.Test; - -import org.rhq.core.domain.event.Event; -import org.rhq.core.domain.event.EventSeverity; - -/** - * @author Ian Springer - */ -@Test -public class Log4JLogEntryProcessorTest { - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); - private static final StringBuilder TEST_LOG = new StringBuilder(); - static { - TEST_LOG.append( - "2007-12-09 15:32:49,909 DEBUG [com.example.FooBar] run: IdleRemover notifying pools, interval: 450000") - .append(LINE_SEPARATOR); - TEST_LOG.append("2008-02-09 02:10:11,909 INFO [com.example.FooBar] a multi-line entry").append(LINE_SEPARATOR); - TEST_LOG.append("\tyada yada yada").append(LINE_SEPARATOR); - TEST_LOG.append("\twocka wocka").append(LINE_SEPARATOR); - } - - public void testProcessLine() throws Exception { - String eventType = "logEntry"; - File logFile = new File("C:/test.log"); - System.out.println("Testing with no includes pattern..."); - Log4JLogEntryProcessor processor = new Log4JLogEntryProcessor(eventType, logFile); - BufferedReader bufferedReader = new BufferedReader(new StringReader(TEST_LOG.toString())); - Set<Event> events = processor.processLines(bufferedReader); - assert events != null && events.size() == 2; - Iterator<Event> eventIterator = events.iterator(); - Event event1 = eventIterator.next(); - assert eventType.equals(event1.getType()); - assert new File(event1.getSourceLocation()).equals(logFile); - Calendar calendar = Calendar.getInstance(); - calendar.set(2007, 11, 9, 15, 32, 49); - calendar.set(Calendar.MILLISECOND, 909); - long expectedTimestamp = calendar.getTimeInMillis(); - assert event1.getTimestamp() == expectedTimestamp; - assert event1.getSeverity().equals(EventSeverity.DEBUG); - assert event1.getDetail().equals("[com.example.FooBar] run: IdleRemover notifying pools, interval: 450000"); - Event event2 = eventIterator.next(); - assert event2.getDetail().startsWith("[com.example.FooBar] a multi-line entry\n"); - assert event2.getDetail().endsWith("\twocka wocka"); - System.out.println("SUCCESS!"); - System.out.println("Testing with matching includes pattern..."); - processor.setIncludesPattern(Pattern.compile("wocka wocka")); - bufferedReader = new BufferedReader(new StringReader(TEST_LOG.toString())); - events = processor.processLines(bufferedReader); - assert events != null && events.size() == 1; - event1 = events.iterator().next(); - assert event1.getDetail().endsWith("\twocka wocka"); - System.out.println("SUCCESS!"); - System.out.println("Testing with non-matching includes pattern..."); - processor.setIncludesPattern(Pattern.compile("hubba bubba")); - bufferedReader = new BufferedReader(new StringReader(TEST_LOG.toString())); - events = processor.processLines(bufferedReader); - assert events != null && events.size() == 0; - System.out.println("SUCCESS!"); - } -} +/* + * RHQ Management Platform + * Copyright (C) 2005-2008 Red Hat, Inc. + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation, and/or the GNU Lesser + * General Public License, version 2.1, also as published by the Free + * Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License and the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * and the GNU Lesser General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.rhq.core.pluginapi.event.log; + +import java.io.BufferedReader; +import java.io.File; +import java.io.StringReader; +import java.util.Calendar; +import java.util.Iterator; +import java.util.Set; +import java.util.regex.Pattern; + +import org.testng.annotations.Test; + +import org.rhq.core.domain.event.Event; +import org.rhq.core.domain.event.EventSeverity; + +/** + * @author Ian Springer + */ +@Test +public class Log4JLogEntryProcessorTest { + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + private static final StringBuilder TEST_LOG = new StringBuilder(); + static { + TEST_LOG.append( + "2007-12-09 15:32:49,909 DEBUG [com.example.FooBar] run: IdleRemover notifying pools, interval: 450000") + .append(LINE_SEPARATOR); + TEST_LOG.append("2008-02-09 02:10:11,909 INFO [com.example.FooBar] a multi-line entry").append(LINE_SEPARATOR); + TEST_LOG.append("\tyada yada yada").append(LINE_SEPARATOR); + TEST_LOG.append("\twocka wocka").append(LINE_SEPARATOR); + } + + public void testProcessLine() throws Exception { + String eventType = "logEntry"; + File logFile = new File("C:/test.log"); + System.out.println("Testing with no includes pattern..."); + Log4JLogEntryProcessor processor = new Log4JLogEntryProcessor(eventType, logFile); + BufferedReader bufferedReader = new BufferedReader(new StringReader(TEST_LOG.toString())); + Set<Event> events = processor.processLines(bufferedReader); + assert events != null && events.size() == 2; + Iterator<Event> eventIterator = events.iterator(); + Event event1 = eventIterator.next(); + assert eventType.equals(event1.getType()); + assert new File(event1.getSourceLocation()).equals(logFile); + Calendar calendar = Calendar.getInstance(); + calendar.set(2007, 11, 9, 15, 32, 49); + calendar.set(Calendar.MILLISECOND, 909); + long expectedTimestamp = calendar.getTimeInMillis(); + assert event1.getTimestamp() == expectedTimestamp; + assert event1.getSeverity().equals(EventSeverity.DEBUG); + assert event1.getDetail().equals("[com.example.FooBar] run: IdleRemover notifying pools, interval: 450000"); + Event event2 = eventIterator.next(); + assert event2.getDetail().startsWith("[com.example.FooBar] a multi-line entry\n"); + assert event2.getDetail().endsWith("\twocka wocka"); + System.out.println("SUCCESS!"); + System.out.println("Testing with matching includes pattern..."); + processor.setIncludesPattern(Pattern.compile("wocka wocka")); + bufferedReader = new BufferedReader(new StringReader(TEST_LOG.toString())); + events = processor.processLines(bufferedReader); + assert events != null && events.size() == 1; + event1 = events.iterator().next(); + assert event1.getDetail().endsWith("\twocka wocka"); + System.out.println("SUCCESS!"); + System.out.println("Testing with non-matching includes pattern..."); + processor.setIncludesPattern(Pattern.compile("hubba bubba")); + bufferedReader = new BufferedReader(new StringReader(TEST_LOG.toString())); + events = processor.processLines(bufferedReader); + assert events != null && events.size() == 0; + System.out.println("SUCCESS!"); + } + + public void testProcessLineBracketDelimitedSeverity() throws Exception { + String eventType = "logEntry"; + File logFile = new File("C:/test.log"); + Log4JLogEntryProcessor processor = new Log4JLogEntryProcessor(eventType, logFile); + + String logEntry = "2007-12-09 15:32:49,909 [DEBUG] [com.example.FooBar] test message"; + BufferedReader bufferedReader = new BufferedReader(new StringReader(logEntry)); + Set<Event> events = processor.processLines(bufferedReader); + assert events != null && events.size() == 1; + Iterator<Event> eventIterator = events.iterator(); + Event event1 = eventIterator.next(); + assert eventType.equals(event1.getType()); + assert new File(event1.getSourceLocation()).equals(logFile); + Calendar calendar = Calendar.getInstance(); + calendar.set(2007, 11, 9, 15, 32, 49); + calendar.set(Calendar.MILLISECOND, 909); + long expectedTimestamp = calendar.getTimeInMillis(); + assert event1.getTimestamp() == expectedTimestamp; + assert event1.getSeverity().equals(EventSeverity.DEBUG); + assert event1.getDetail().equals("[com.example.FooBar] test message"); + } + + public void testProcessLineParenDelimitedSeverity() throws Exception { + String eventType = "logEntry"; + File logFile = new File("C:/test.log"); + Log4JLogEntryProcessor processor = new Log4JLogEntryProcessor(eventType, logFile); + + String logEntry = "2007-12-09 15:32:49,909 (DEBUG) [com.example.FooBar] test message"; + BufferedReader bufferedReader = new BufferedReader(new StringReader(logEntry)); + Set<Event> events = processor.processLines(bufferedReader); + assert events != null && events.size() == 1; + Iterator<Event> eventIterator = events.iterator(); + Event event1 = eventIterator.next(); + assert eventType.equals(event1.getType()); + assert new File(event1.getSourceLocation()).equals(logFile); + Calendar calendar = Calendar.getInstance(); + calendar.set(2007, 11, 9, 15, 32, 49); + calendar.set(Calendar.MILLISECOND, 909); + long expectedTimestamp = calendar.getTimeInMillis(); + assert event1.getTimestamp() == expectedTimestamp; + assert event1.getSeverity().equals(EventSeverity.DEBUG); + assert event1.getDetail().equals("[com.example.FooBar] test message"); + } + + public void testProcessLineSpacedDelimitedSeverity() throws Exception { + String eventType = "logEntry"; + File logFile = new File("C:/test.log"); + Log4JLogEntryProcessor processor = new Log4JLogEntryProcessor(eventType, logFile); + + String logEntry = "2007-12-09 15:32:49,909 [ DEBUG ) [com.example.FooBar] test message"; + BufferedReader bufferedReader = new BufferedReader(new StringReader(logEntry)); + Set<Event> events = processor.processLines(bufferedReader); + assert events != null && events.size() == 1; + Iterator<Event> eventIterator = events.iterator(); + Event event1 = eventIterator.next(); + assert eventType.equals(event1.getType()); + assert new File(event1.getSourceLocation()).equals(logFile); + Calendar calendar = Calendar.getInstance(); + calendar.set(2007, 11, 9, 15, 32, 49); + calendar.set(Calendar.MILLISECOND, 909); + long expectedTimestamp = calendar.getTimeInMillis(); + assert event1.getTimestamp() == expectedTimestamp; + assert event1.getSeverity().equals(EventSeverity.DEBUG); + assert event1.getDetail().equals("[com.example.FooBar] test message"); + } + +}
rhq-commits@lists.fedorahosted.org