Author: rec Date: Wed May 1 13:42:10 2013 New Revision: 1477999 URL: http://svn.apache.org/r1477999 Log: [UIMA-2555] Logging messaged from ExtendedLogger always carry wrong source info - Use new logging function in UIMA 2.4.1-SNAPSHOT - Added tests
Modified: uima/sandbox/uimafit/trunk/uimafit/pom.xml uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/util/ExtendedLogger.java uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/LoggingTest.java Modified: uima/sandbox/uimafit/trunk/uimafit/pom.xml URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit/pom.xml?rev=1477999&r1=1477998&r2=1477999&view=diff ============================================================================== --- uima/sandbox/uimafit/trunk/uimafit/pom.xml (original) +++ uima/sandbox/uimafit/trunk/uimafit/pom.xml Wed May 1 13:42:10 2013 @@ -65,6 +65,30 @@ <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + <version>1.2.15</version> + <scope>test</scope> + <exclusions> + <exclusion> + <artifactId>jmxtools</artifactId> + <groupId>com.sun.jdmk</groupId> + </exclusion> + <exclusion> + <artifactId>jmxri</artifactId> + <groupId>com.sun.jmx</groupId> + </exclusion> + <exclusion> + <artifactId>jms</artifactId> + <groupId>javax.jms</groupId> + </exclusion> + <exclusion> + <artifactId>mail</artifactId> + <groupId>javax.mail</groupId> + </exclusion> + </exclusions> + </dependency> </dependencies> <licenses> <license> Modified: uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/util/ExtendedLogger.java URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/util/ExtendedLogger.java?rev=1477999&r1=1477998&r2=1477999&view=diff ============================================================================== --- uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/util/ExtendedLogger.java (original) +++ uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/util/ExtendedLogger.java Wed May 1 13:42:10 2013 @@ -20,16 +20,15 @@ package org.apache.uima.fit.util; import java.io.OutputStream; import java.io.PrintStream; - import org.apache.uima.UimaContext; import org.apache.uima.resource.ResourceManager; import org.apache.uima.util.Level; import org.apache.uima.util.Logger; /** - * INTERNAL API - Wrapper for the UIMA {@link Logger} offering a more convenient API similar to that of the Apache - * Commons Logging interface {@link org.apache.commons.logging.Log Log} or to that of Log4J's - * {@code Category} and SLF4J's {@code Logger}, using the names {@code error}, {@code warn}, + * INTERNAL API - Wrapper for the UIMA {@link Logger} offering a more convenient API similar to that + * of the Apache Commons Logging interface {@link org.apache.commons.logging.Log Log} or to that of + * Log4J's {@code Category} and SLF4J's {@code Logger}, using the names {@code error}, {@code warn}, * {@code info}, {@code debug} and {@code trace} and mapping these to UIMA logging levels. * */ @@ -167,6 +166,15 @@ public class ExtendedLogger implements L } } + public void log(String wrapperFQCN, Level level, String message, Throwable thrown) { + if (context != null) { + Logger logger = context.getLogger(); + if (logger != null) { + context.getLogger().log(wrapperFQCN, level, message, thrown); + } + } + } + public boolean isLoggable(Level level) { if (context != null) { Logger logger = context.getLogger(); @@ -195,7 +203,8 @@ public class ExtendedLogger implements L if (context != null) { Logger logger = context.getLogger(); if (logger != null) { - context.getLogger().log(Level.FINE, String.valueOf(paramObject)); + context.getLogger() + .log(getClass().getName(), Level.FINE, String.valueOf(paramObject), null); } } } @@ -212,7 +221,8 @@ public class ExtendedLogger implements L if (context != null) { Logger logger = context.getLogger(); if (logger != null) { - context.getLogger().log(Level.FINE, String.valueOf(paramObject), paramThrowable); + context.getLogger().log(getClass().getName(), Level.FINE, String.valueOf(paramObject), + paramThrowable); } } } @@ -227,7 +237,8 @@ public class ExtendedLogger implements L if (context != null) { Logger logger = context.getLogger(); if (logger != null) { - context.getLogger().log(Level.SEVERE, String.valueOf(paramObject)); + context.getLogger().log(getClass().getName(), Level.SEVERE, String.valueOf(paramObject), + null); } } } @@ -244,7 +255,8 @@ public class ExtendedLogger implements L if (context != null) { Logger logger = context.getLogger(); if (logger != null) { - context.getLogger().log(Level.SEVERE, String.valueOf(paramObject), paramThrowable); + context.getLogger().log(getClass().getName(), Level.SEVERE, String.valueOf(paramObject), + paramThrowable); } } } @@ -259,7 +271,8 @@ public class ExtendedLogger implements L if (context != null) { Logger logger = context.getLogger(); if (logger != null) { - context.getLogger().log(Level.INFO, String.valueOf(paramObject)); + context.getLogger() + .log(getClass().getName(), Level.INFO, String.valueOf(paramObject), null); } } } @@ -276,7 +289,8 @@ public class ExtendedLogger implements L if (context != null) { Logger logger = context.getLogger(); if (logger != null) { - context.getLogger().log(Level.INFO, String.valueOf(paramObject), paramThrowable); + context.getLogger().log(getClass().getName(), Level.INFO, String.valueOf(paramObject), + paramThrowable); } } } @@ -341,7 +355,8 @@ public class ExtendedLogger implements L if (context != null) { Logger logger = context.getLogger(); if (logger != null) { - context.getLogger().log(Level.FINER, String.valueOf(paramObject)); + context.getLogger().log(getClass().getName(), Level.FINER, String.valueOf(paramObject), + null); } } } @@ -358,7 +373,8 @@ public class ExtendedLogger implements L if (context != null) { Logger logger = context.getLogger(); if (logger != null) { - context.getLogger().log(Level.FINER, String.valueOf(paramObject), paramThrowable); + context.getLogger().log(getClass().getName(), Level.FINER, String.valueOf(paramObject), + paramThrowable); } } } @@ -373,7 +389,8 @@ public class ExtendedLogger implements L if (context != null) { Logger logger = context.getLogger(); if (logger != null) { - context.getLogger().log(Level.WARNING, String.valueOf(paramObject)); + context.getLogger().log(getClass().getName(), Level.WARNING, String.valueOf(paramObject), + null); } } } @@ -390,7 +407,8 @@ public class ExtendedLogger implements L if (context != null) { Logger logger = context.getLogger(); if (logger != null) { - context.getLogger().log(Level.WARNING, String.valueOf(paramObject), paramThrowable); + context.getLogger().log(getClass().getName(), Level.WARNING, String.valueOf(paramObject), + paramThrowable); } } } Modified: uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/LoggingTest.java URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/LoggingTest.java?rev=1477999&r1=1477998&r2=1477999&view=diff ============================================================================== --- uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/LoggingTest.java (original) +++ uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/LoggingTest.java Wed May 1 13:42:10 2013 @@ -32,6 +32,12 @@ import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.LogRecord; +import org.apache.log4j.Appender; +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.Logger; +import org.apache.log4j.spi.LocationInfo; +import org.apache.log4j.spi.LoggingEvent; +import org.apache.uima.UimaContextAdmin; import org.apache.uima.analysis_engine.AnalysisEngineProcessException; import org.apache.uima.cas.AbstractCas; import org.apache.uima.cas.CAS; @@ -47,18 +53,20 @@ import org.apache.uima.fit.component.JCa import org.apache.uima.fit.component.JCasFlowController_ImplBase; import org.apache.uima.fit.component.JCasMultiplier_ImplBase; import org.apache.uima.fit.component.Resource_ImplBase; +import org.apache.uima.fit.util.ExtendedLogger; import org.apache.uima.flow.Flow; +import org.apache.uima.impl.RootUimaContext_impl; import org.apache.uima.jcas.JCas; import org.apache.uima.resource.ResourceInitializationException; import org.apache.uima.resource.ResourceSpecifier; import org.apache.uima.util.Progress; +import org.apache.uima.util.impl.JSR47Logger_impl; +import org.apache.uima.util.impl.Log4jLogger_impl; import org.junit.Test; -/** - */ public class LoggingTest { @Test - public void testLogger() throws Exception { + public void testJSR47Logger() throws Exception { final List<LogRecord> records = new ArrayList<LogRecord>(); // Tell the logger to log everything @@ -70,13 +78,20 @@ public class LoggingTest { handler.setFilter(new Filter() { public boolean isLoggable(LogRecord record) { records.add(record); + System.out.printf("[%s] %s%n", record.getSourceClassName(), record.getMessage()); return false; } }); try { - JCas jcas = JCasFactory.createJCas(); - createPrimitive(LoggingCasConsumerChristmasTree.class).process(jcas.getCas()); + UimaContextAdmin ctx = new RootUimaContext_impl(); + ctx.setLogger(JSR47Logger_impl.getInstance()); + ExtendedLogger logger = new ExtendedLogger(ctx); + + logger.setLevel(org.apache.uima.util.Level.ALL); + trigger(logger); + logger.setLevel(org.apache.uima.util.Level.OFF); + trigger(logger); assertEquals(10, records.size()); assertEquals(Level.FINER, records.get(0).getLevel()); @@ -98,6 +113,80 @@ public class LoggingTest { } @Test + public void testLog4JLogger() throws Exception { + final List<LoggingEvent> records = new ArrayList<LoggingEvent>(); + + BasicConfigurator.configure(); + + // Tell the logger to log everything + Logger rootLogger = org.apache.log4j.LogManager.getRootLogger(); + org.apache.log4j.Level oldLevel = rootLogger.getLevel(); + rootLogger.setLevel(org.apache.log4j.Level.ALL); + Appender appender = (Appender) rootLogger.getAllAppenders().nextElement(); + // Capture the logging output without actually logging it + appender.addFilter(new org.apache.log4j.spi.Filter() { + @Override + public int decide(LoggingEvent event) { + records.add(event); + LocationInfo l = event.getLocationInformation(); + System.out.printf("[%s:%s] %s%n", l.getFileName(), l.getLineNumber(), event.getMessage()); + return org.apache.log4j.spi.Filter.DENY; + } + }); + + try { + UimaContextAdmin ctx = new RootUimaContext_impl(); + ctx.setLogger(Log4jLogger_impl.getInstance()); + ExtendedLogger logger = new ExtendedLogger(ctx); + + logger.setLevel(org.apache.uima.util.Level.ALL); + trigger(logger); + logger.setLevel(org.apache.uima.util.Level.OFF); + trigger(logger); + + assertEquals(10, records.size()); + assertEquals(org.apache.log4j.Level.ALL, records.get(0).getLevel()); + assertEquals(org.apache.log4j.Level.ALL, records.get(1).getLevel()); + assertEquals(org.apache.log4j.Level.DEBUG, records.get(2).getLevel()); + assertEquals(org.apache.log4j.Level.DEBUG, records.get(3).getLevel()); + assertEquals(org.apache.log4j.Level.INFO, records.get(4).getLevel()); + assertEquals(org.apache.log4j.Level.INFO, records.get(5).getLevel()); + assertEquals(org.apache.log4j.Level.WARN, records.get(6).getLevel()); + assertEquals(org.apache.log4j.Level.WARN, records.get(7).getLevel()); + assertEquals(org.apache.log4j.Level.ERROR, records.get(8).getLevel()); + assertEquals(org.apache.log4j.Level.ERROR, records.get(9).getLevel()); + } finally { + if (oldLevel != null) { + rootLogger.setLevel(oldLevel); + appender.clearFilters(); + } + } + } + + private void trigger(ExtendedLogger aLogger) { + if (aLogger.isTraceEnabled()) { + aLogger.trace("Logging: " + getClass().getName()); + aLogger.trace("Logging: " + getClass().getName(), new IllegalArgumentException()); + } + if (aLogger.isDebugEnabled()) { + aLogger.debug("Logging: " + getClass().getName()); + aLogger.debug("Logging: " + getClass().getName(), new IllegalArgumentException()); + } + if (aLogger.isInfoEnabled()) { + aLogger.info("Logging: " + getClass().getName()); + aLogger.info("Logging: " + getClass().getName(), new IllegalArgumentException()); + } + if (aLogger.isWarnEnabled()) { + aLogger.warn("Logging: " + getClass().getName()); + aLogger.warn("Logging: " + getClass().getName(), new IllegalArgumentException()); + } + if (aLogger.isErrorEnabled()) { + aLogger.error("Logging: " + getClass().getName()); + aLogger.error("Logging: " + getClass().getName(), new IllegalArgumentException()); + } + } + + @Test public void testAllKindsOfComponents() throws Exception { final List<LogRecord> records = new ArrayList<LogRecord>(); @@ -156,39 +245,6 @@ public class LoggingTest { records.clear(); } - public static class LoggingCasConsumerChristmasTree extends CasConsumer_ImplBase { - @Override - public void process(CAS aCAS) throws AnalysisEngineProcessException { - getLogger().setLevel(org.apache.uima.util.Level.ALL); - trigger(); - getLogger().setLevel(org.apache.uima.util.Level.OFF); - trigger(); - } - - private void trigger() { - if (getLogger().isTraceEnabled()) { - getLogger().trace("Logging: " + getClass().getName()); - getLogger().trace("Logging: " + getClass().getName(), new IllegalArgumentException()); - } - if (getLogger().isDebugEnabled()) { - getLogger().debug("Logging: " + getClass().getName()); - getLogger().debug("Logging: " + getClass().getName(), new IllegalArgumentException()); - } - if (getLogger().isInfoEnabled()) { - getLogger().info("Logging: " + getClass().getName()); - getLogger().info("Logging: " + getClass().getName(), new IllegalArgumentException()); - } - if (getLogger().isWarnEnabled()) { - getLogger().warn("Logging: " + getClass().getName()); - getLogger().warn("Logging: " + getClass().getName(), new IllegalArgumentException()); - } - if (getLogger().isErrorEnabled()) { - getLogger().error("Logging: " + getClass().getName()); - getLogger().error("Logging: " + getClass().getName(), new IllegalArgumentException()); - } - } - } - public static class LoggingCasMultiplier extends CasMultiplier_ImplBase { public boolean hasNext() throws AnalysisEngineProcessException {