logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1487753 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/config/ core/src/main/java/org/apache/logging/log4j/core/helpers/ core/src/main/java/org/apache/logging/log4j/core/impl/ core/src/test/java/org/apac...
Date Thu, 30 May 2013 05:35:05 GMT
Author: rgoers
Date: Thu May 30 05:35:05 2013
New Revision: 1487753

URL: http://svn.apache.org/r1487753
Log:
LOG4J2-243 - Allow customr LogEventFactories

Added:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/DefaultLogEventFactory.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LogEventFactoryTest.java
      - copied, changed from r1487271, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/AppenderRefLevelTest.java
Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Constants.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml
    logging/log4j/log4j2/trunk/src/site/xdoc/manual/extending.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java?rev=1487753&r1=1487752&r2=1487753&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
Thu May 30 05:35:05 2013
@@ -32,11 +32,13 @@ import org.apache.logging.log4j.core.con
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
 import org.apache.logging.log4j.core.filter.AbstractFilterable;
 import org.apache.logging.log4j.core.helpers.Constants;
-import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.helpers.Loader;
+import org.apache.logging.log4j.core.impl.DefaultLogEventFactory;
 import org.apache.logging.log4j.core.impl.LogEventFactory;
 import org.apache.logging.log4j.core.lookup.StrSubstitutor;
 import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.PropertiesUtil;
 
 import java.io.Serializable;
 import java.util.ArrayList;
@@ -54,11 +56,12 @@ import java.util.concurrent.atomic.Atomi
  * Logger object that is created via configuration.
  */
 @Plugin(name = "logger", category = "Core", printObject = true)
-public class LoggerConfig extends AbstractFilterable implements LogEventFactory {
+public class LoggerConfig extends AbstractFilterable {
 
     private static final Logger LOGGER = StatusLogger.getLogger();
     private static final int MAX_RETRIES = 3;
     private static final long WAIT_TIME = 1000;
+    private static LogEventFactory LOG_EVENT_FACTORY = null;
 
     private List<AppenderRef> appenderRefs = new ArrayList<AppenderRef>();
     private final Map<String, AppenderControl<?>> appenders = new ConcurrentHashMap<String,
AppenderControl<?>>();
@@ -73,11 +76,28 @@ public class LoggerConfig extends Abstra
     private final Map<Property, Boolean> properties;
     private final Configuration config;
 
+    static {
+        final String factory = PropertiesUtil.getProperties().getStringProperty(Constants.LOG4J_LOG_EVENT_FACTORY);
+        if (factory != null) {
+            try {
+                final Class<?> clazz = Loader.loadClass(factory);
+                if (clazz != null && LogEventFactory.class.isAssignableFrom(clazz))
{
+                    LOG_EVENT_FACTORY = (LogEventFactory) clazz.newInstance();
+                }
+            } catch (final Exception ex) {
+                LOGGER.error("Unable to create LogEventFactory " + factory, ex);
+            }
+        }
+        if (LOG_EVENT_FACTORY == null) {
+            LOG_EVENT_FACTORY = new DefaultLogEventFactory();
+        }
+    }
+
     /**
      * Default constructor.
      */
     public LoggerConfig() {
-        this.logEventFactory = this;
+        this.logEventFactory = LOG_EVENT_FACTORY;
         this.level = Level.ERROR;
         this.name = "";
         this.properties = null;
@@ -93,7 +113,7 @@ public class LoggerConfig extends Abstra
      */
     public LoggerConfig(final String name, final Level level,
             final boolean additive) {
-        this.logEventFactory = this;
+        this.logEventFactory = LOG_EVENT_FACTORY;
         this.name = name;
         this.level = level;
         this.additive = additive;
@@ -107,7 +127,7 @@ public class LoggerConfig extends Abstra
             final Property[] properties, final Configuration config,
             final boolean includeLocation) {
         super(filter);
-        this.logEventFactory = this;
+        this.logEventFactory = LOG_EVENT_FACTORY;
         this.name = name;
         this.appenderRefs = appenders;
         this.level = level;
@@ -405,25 +425,6 @@ public class LoggerConfig extends Abstra
         }
     }
 
-    /**
-     * Creates a log event.
-     *
-     * @param loggerName The name of the Logger.
-     * @param marker An optional Marker.
-     * @param fqcn The fully qualified class name of the caller.
-     * @param level The event Level.
-     * @param data The Message.
-     * @param properties Properties to be added to the log event.
-     * @param t An optional Throwable.
-     * @return The LogEvent.
-     */
-    @Override
-    public LogEvent createEvent(final String loggerName, final Marker marker,
-            final String fqcn, final Level level, final Message data,
-            final List<Property> properties, final Throwable t) {
-        return new Log4jLogEvent(loggerName, marker, fqcn, level, data,
-                properties, t);
-    }
 
     @Override
     public String toString() {
@@ -478,7 +479,7 @@ public class LoggerConfig extends Abstra
 
     // Note: for asynchronous loggers, includeLocation default is FALSE,
     // for synchronous loggers, includeLocation default is TRUE.
-    private static boolean includeLocation(String includeLocationConfigValue) {
+    protected static boolean includeLocation(String includeLocationConfigValue) {
         if (includeLocationConfigValue == null) {
             final boolean sync = !AsyncLoggerContextSelector.class.getName()
                     .equals(System.getProperty(Constants.LOG4J_CONTEXT_SELECTOR));

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Constants.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Constants.java?rev=1487753&r1=1487752&r2=1487753&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Constants.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Constants.java
Thu May 30 05:35:05 2013
@@ -23,6 +23,7 @@ import org.apache.logging.log4j.util.Pro
  */
 public final class Constants {
 
+    public static final String LOG4J_LOG_EVENT_FACTORY = "Log4jLogEventFactory";
     /**
      * Name of the system property to use to identify the ContextSelector Class.
      */

Added: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/DefaultLogEventFactory.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/DefaultLogEventFactory.java?rev=1487753&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/DefaultLogEventFactory.java
(added)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/DefaultLogEventFactory.java
Thu May 30 05:35:05 2013
@@ -0,0 +1,51 @@
+/*
+ * 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.logging.log4j.core.impl;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.Marker;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.Property;
+import org.apache.logging.log4j.message.Message;
+
+import java.util.List;
+
+/**
+ *
+ */
+public class DefaultLogEventFactory implements LogEventFactory {
+
+
+    /**
+     * Creates a log event.
+     *
+     * @param loggerName The name of the Logger.
+     * @param marker An optional Marker.
+     * @param fqcn The fully qualified class name of the caller.
+     * @param level The event Level.
+     * @param data The Message.
+     * @param properties Properties to be added to the log event.
+     * @param t An optional Throwable.
+     * @return The LogEvent.
+     */
+    @Override
+    public LogEvent createEvent(final String loggerName, final Marker marker,
+                                final String fqcn, final Level level, final Message data,
+                                final List<Property> properties, final Throwable t)
{
+        return new Log4jLogEvent(loggerName, marker, fqcn, level, data, properties, t);
+    }
+}

Copied: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LogEventFactoryTest.java
(from r1487271, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/AppenderRefLevelTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LogEventFactoryTest.java?p2=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LogEventFactoryTest.java&p1=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/AppenderRefLevelTest.java&r1=1487271&r2=1487753&rev=1487753&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/AppenderRefLevelTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LogEventFactoryTest.java
Thu May 30 05:35:05 2013
@@ -16,11 +16,16 @@
  */
 package org.apache.logging.log4j.core;
 
+import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Marker;
-import org.apache.logging.log4j.MarkerManager;
 import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.Property;
 import org.apache.logging.log4j.core.config.XMLConfigurationFactory;
+import org.apache.logging.log4j.core.helpers.Constants;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.impl.LogEventFactory;
+import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.status.StatusLogger;
 import org.apache.logging.log4j.test.appender.ListAppender;
 import org.junit.AfterClass;
@@ -32,31 +37,35 @@ import java.util.List;
 import java.util.Map;
 
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
 
 /**
  *
  */
-public class AppenderRefLevelTest {
+public class LogEventFactoryTest {
 
-    private static final String CONFIG = "log4j-reference-level.xml";
+    private static final String CONFIG = "log4j2-config.xml";
     private static Configuration config;
-    private static ListAppender<LogEvent> app1;
-    private static ListAppender<LogEvent> app2;
+    private static ListAppender<LogEvent> app;
     private static LoggerContext ctx;
 
     @BeforeClass
     @SuppressWarnings("unchecked")
     public static void setupClass() {
+        System.setProperty(Constants.LOG4J_LOG_EVENT_FACTORY, TestLogEventFactory.class.getName());
         System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
         ctx = (LoggerContext) LogManager.getContext(false);
         config = ctx.getConfiguration();
         for (final Map.Entry<String, Appender<?>> entry : config.getAppenders().entrySet())
{
-            if (entry.getKey().equals("LIST1")) {
-                app1 = (ListAppender<LogEvent>) entry.getValue();
-            } else if (entry.getKey().equals("LIST2")) {
-                app2 = (ListAppender<LogEvent>) entry.getValue();
+            if (entry.getKey().equals("List")) {
+                app = (ListAppender<LogEvent>) entry.getValue();
+                break;
             }
         }
+        if (app == null) {
+            fail("No List Appender could be found");
+        }
     }
 
     @AfterClass
@@ -66,55 +75,29 @@ public class AppenderRefLevelTest {
         StatusLogger.getLogger().reset();
     }
 
-    org.apache.logging.log4j.Logger logger1 = LogManager.getLogger("org.apache.logging.log4j.test1");
-    org.apache.logging.log4j.Logger logger2 = LogManager.getLogger("org.apache.logging.log4j.test2");
-    org.apache.logging.log4j.Logger logger3 = LogManager.getLogger("org.apache.logging.log4j.test3");
-    Marker testMarker = MarkerManager.getMarker("TEST");
-
     @Before
     public void before() {
-        app1.clear();
-        app2.clear();
+        app.clear();
     }
 
     @Test
-    public void logger1() {
-        logger1.entry();
-        logger1.debug("debug message");
-        logger1.error("Test Message");
-        logger1.info("Info Message");
-        logger1.warn("warn Message");
-        logger1.exit();
-        List<LogEvent> events = app1.getEvents();
-        assertTrue("Incorrect number of events. Expected 6, actual " + events.size(), events.size()
== 6);
-        events = app2.getEvents();
+    public void testEvent() {
+        org.apache.logging.log4j.Logger logger = LogManager.getLogger("org.apache.test.LogEventFactory");
+        logger.error("error message");
+        List<LogEvent> events = app.getEvents();
+        assertNotNull("No events", events);
         assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size()
== 1);
+        LogEvent event = events.get(0);
+        assertTrue("Test LogEventFactory wasn't used", event.getLoggerName().equals("Test"));
     }
 
-    @Test
-    public void logger2() {
-        logger2.entry();
-        logger2.debug("debug message");
-        logger2.error("Test Message");
-        logger2.info("Info Message");
-        logger2.warn("warn Message");
-        logger2.exit();
-        List<LogEvent> events = app1.getEvents();
-        assertTrue("Incorrect number of events. Expected 2, actual " + events.size(), events.size()
== 2);
-        events = app2.getEvents();
-        assertTrue("Incorrect number of events. Expected 4, actual " + events.size(), events.size()
== 4);
-    }
+    public static class TestLogEventFactory implements LogEventFactory {
 
-    @Test
-    public void logger3() {
-        logger3.entry();
-        logger3.debug(testMarker, "debug message");
-        logger3.error("Test Message");
-        logger3.info(testMarker, "Info Message");
-        logger3.warn("warn Message");
-        logger3.exit();
-        final List<LogEvent> events = app1.getEvents();
-        assertTrue("Incorrect number of events. Expected 4, actual " + events.size(), events.size()
== 4);
+        public LogEvent createEvent(final String loggerName, final Marker marker,
+                                    final String fqcn, final Level level, final Message data,
+                                    final List<Property> properties, final Throwable
t) {
+            return new Log4jLogEvent("Test", marker, fqcn, level, data, properties, t);
+        }
     }
 }
 

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1487753&r1=1487752&r2=1487753&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Thu May 30 05:35:05 2013
@@ -22,6 +22,9 @@
   </properties>
   <body>
     <release version="2.0-beta7" date="2013-??-??" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-243" dev="rgoers" type="update">
+        Allow custom LogEventFactories.
+      </action>
       <action issue="LOG4J2-262" dev="rgoers" type="update" due-to="Edward Sargisson">
         Add support for interceptors in the embedded Flume Appender.
       </action>

Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/extending.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/extending.xml?rev=1487753&r1=1487752&r2=1487753&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/extending.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/extending.xml Thu May 30 05:35:05 2013
@@ -159,6 +159,11 @@ public static class RootLogger extends L
     }
 }]]></pre>
           </subsection>
+          <subsection name="LogEventFactory">
+            <p>A LogEventFactory is used to generate LogEvents. Applications may replace
the standard LogEventFactory
+              by setting the value of the system property Log4jLogEventFactory to the name
of the custom
+              LogEventFactory class. </p>
+          </subsection>
           <subsection name="Lookups">
             <p>
               Lookups are the means in which parameter substitution is performed. During
Configuration initialization



Mime
View raw message