logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1433581 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/appender/ core/src/main/java/org/apache/logging/log4j/core/net/ core/src/test/java/org/apache/logging/log4j/core/appender/ flume-ng/ samples/flume-c...
Date Tue, 15 Jan 2013 19:03:51 GMT
Author: rgoers
Date: Tue Jan 15 19:03:50 2013
New Revision: 1433581

URL: http://svn.apache.org/viewvc?rev=1433581&view=rev
Log:
LOG4J2-149 - SMTPAppender will only cache filtered events.

Added:
    logging/log4j/log4j2/trunk/samples/flume-common/src/main/java/org/apache/logging/log4j/samples/app/LoggingApp.java
      - copied, changed from r1431786, logging/log4j/log4j2/trunk/samples/flume-common/src/main/java/org/apache/logging/log4j/samples/app/LoggingController.java
Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/SMTPAppender.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/SMTPManager.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SMTPAppenderTest.java
    logging/log4j/log4j2/trunk/flume-ng/pom.xml
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/SMTPAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/SMTPAppender.java?rev=1433581&r1=1433580&r2=1433581&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/SMTPAppender.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/SMTPAppender.java
Tue Jan 15 19:03:50 2013
@@ -146,8 +146,11 @@ public class SMTPAppender extends Abstra
      */
     @Override
     public boolean isFiltered(final LogEvent event) {
-        manager.add(event);
-        return super.isFiltered(event);
+        final boolean filtered = super.isFiltered(event);
+        if(filtered) {
+            manager.add(event);
+        }
+		return filtered;
     }
 
     /**
@@ -156,6 +159,6 @@ public class SMTPAppender extends Abstra
      * sent.
      */
     public void append(final LogEvent event) {
-        manager.sendEvents(getLayout());
+        manager.sendEvents(getLayout(), event);
     }
 }
\ No newline at end of file

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/SMTPManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/SMTPManager.java?rev=1433581&r1=1433580&r2=1433581&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/SMTPManager.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/net/SMTPManager.java
Tue Jan 15 19:03:50 2013
@@ -123,13 +123,15 @@ public class SMTPManager extends Abstrac
 
     /**
      * Send the contents of the cyclic buffer as an e-mail message.
+     * @param appendEvent 
      */
-    public void sendEvents(final Layout<?> layout) {
+    public void sendEvents(final Layout<?> layout, final LogEvent appendEvent) {
         if (message == null) {
             connect();
         }
         try {
-            final byte[] rawBytes = formatContentToBytes(buffer, layout);
+        	final LogEvent[] priorEvents = buffer.removeAll();
+            final byte[] rawBytes = formatContentToBytes(priorEvents, appendEvent, layout);
 
             final String contentType = layout.getContentType();
             final String encoding = getEncoding(rawBytes, contentType);
@@ -151,16 +153,16 @@ public class SMTPManager extends Abstrac
         }
     }
 
-    protected byte[] formatContentToBytes(final CyclicBuffer<LogEvent> cb, final Layout<?>
layout) throws IOException {
+    protected byte[] formatContentToBytes(final LogEvent[] priorEvents, final LogEvent appendEvent,
final Layout<?> layout) throws IOException {
         final ByteArrayOutputStream raw = new ByteArrayOutputStream();
-        writeContent(cb, layout, raw);
+        writeContent(priorEvents, appendEvent, layout, raw);
         return raw.toByteArray();
     }
 
-    private void writeContent(final CyclicBuffer<LogEvent> cb, final Layout<?>
layout, final ByteArrayOutputStream out)
+    private void writeContent(final LogEvent[] priorEvents, final LogEvent appendEvent, final
Layout<?> layout, final ByteArrayOutputStream out)
         throws IOException {
         writeHeader(layout, out);
-        writeBuffer(cb, layout, out);
+        writeBuffer(priorEvents, appendEvent, layout, out);
         writeFooter(layout, out);
     }
 
@@ -171,12 +173,14 @@ public class SMTPManager extends Abstrac
         }
     }
 
-    protected void writeBuffer(final CyclicBuffer<LogEvent> cb, final Layout<?>
layout, final OutputStream out) throws IOException {
-        final LogEvent[] events = cb.removeAll();
-        for (final LogEvent event : events) {
-            final byte[] bytes = layout.toByteArray(event);
+    protected void writeBuffer(final LogEvent[] priorEvents, final LogEvent appendEvent,
final Layout<?> layout, final OutputStream out) throws IOException {
+        for (final LogEvent priorEvent : priorEvents) {
+            final byte[] bytes = layout.toByteArray(priorEvent);
             out.write(bytes);
         }
+        
+        final byte[] bytes = layout.toByteArray(appendEvent);
+        out.write(bytes);
     }
 
     protected void writeFooter(final Layout<?> layout, final OutputStream out) throws
IOException {

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SMTPAppenderTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SMTPAppenderTest.java?rev=1433581&r1=1433580&r2=1433581&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SMTPAppenderTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/SMTPAppenderTest.java
Tue Jan 15 19:03:50 2013
@@ -144,6 +144,7 @@ public class SMTPAppenderTest {
         root.debug("Debug message #1");
         root.debug("Debug message #2");
         root.debug("Debug message #3");
+        root.debug("Debug message #4");
         root.error("Error with exception", new RuntimeException("Exception message"));
 
         server.stop();
@@ -162,6 +163,7 @@ public class SMTPAppenderTest {
         assertFalse(body.contains("Debug message #1"));
         assertTrue(body.contains("Debug message #2"));
         assertTrue(body.contains("Debug message #3"));
+        assertTrue(body.contains("Debug message #4"));
         assertTrue(body.contains("Error with exception"));
         assertTrue(body.contains("RuntimeException"));
         assertTrue(body.contains("Exception message"));

Modified: logging/log4j/log4j2/trunk/flume-ng/pom.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/flume-ng/pom.xml?rev=1433581&r1=1433580&r2=1433581&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/flume-ng/pom.xml (original)
+++ logging/log4j/log4j2/trunk/flume-ng/pom.xml Tue Jan 15 19:03:50 2013
@@ -32,7 +32,7 @@
     <log4jParentDir>${basedir}/..</log4jParentDir>
     <docLabel>Flume Documentation</docLabel>
     <projectDir>/flume-ng</projectDir>
-    <flumeVersion>1.2.0</flumeVersion>
+    <flumeVersion>1.3.1</flumeVersion>
   </properties>
   <dependencies>
     <dependency>

Copied: logging/log4j/log4j2/trunk/samples/flume-common/src/main/java/org/apache/logging/log4j/samples/app/LoggingApp.java
(from r1431786, logging/log4j/log4j2/trunk/samples/flume-common/src/main/java/org/apache/logging/log4j/samples/app/LoggingController.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/samples/flume-common/src/main/java/org/apache/logging/log4j/samples/app/LoggingApp.java?p2=logging/log4j/log4j2/trunk/samples/flume-common/src/main/java/org/apache/logging/log4j/samples/app/LoggingApp.java&p1=logging/log4j/log4j2/trunk/samples/flume-common/src/main/java/org/apache/logging/log4j/samples/app/LoggingController.java&r1=1431786&r2=1433581&rev=1433581&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/samples/flume-common/src/main/java/org/apache/logging/log4j/samples/app/LoggingController.java
(original)
+++ logging/log4j/log4j2/trunk/samples/flume-common/src/main/java/org/apache/logging/log4j/samples/app/LoggingApp.java
Tue Jan 15 19:03:50 2013
@@ -16,41 +16,41 @@
  */
 package org.apache.logging.log4j.samples.app;
 
-import java.util.List;
-import java.util.Random;
-
-import javax.servlet.http.HttpServletRequest;
-
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.ThreadContext;
 import org.apache.logging.log4j.samples.dto.AuditEvent;
 import org.apache.logging.log4j.samples.dto.RequestContext;
 
-import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.servlet.ModelAndView;
+import java.util.List;
+import java.util.Random;
 
 
 /**
- * The Class LoggingController.
+ * The Class LoggingApp.
  */
-@Controller
-public class LoggingController {
+public class LoggingApp {
 
     /**
      * The logger.
      */
-    private static Logger logger = LogManager.getLogger(LoggingController.class);
+    private static Logger logger = LogManager.getLogger(LoggingApp.class);
 
-    private volatile boolean generateLog = false;
     private Random ran = new Random();
 
     private List<AuditEvent> events;
 
-    public LoggingController() {
+    public static void main(String[] args) {
+        String member = "fakemember";
+        if (args.length == 1) {
+            member = args[0];
+        }
+        LoggingApp app = new LoggingApp(member);
+        app.runApp(member);
+        System.out.println("Job ended");
+    }
+
+    public LoggingApp(String member) {
 
         ThreadContext.clear();
 
@@ -61,64 +61,82 @@ public class LoggingController {
         RequestContext.setProductVersion("4.18.1");
         RequestContext.setLocale("en_US");
         RequestContext.setRegion("prod");
-    }
-
-    @RequestMapping(value = "/start.do", method = RequestMethod.GET)
-    public ModelAndView startLogging(
-        @RequestParam(value = "member", required = false, defaultValue = "fakemember") final
String member,
-                      HttpServletRequest servletRequest) {
-
-        System.out.println("STARTING..................");
 
         if (events == null) {
             events = MockEventsSupplier.getAllEvents(member);
         }
+    }
 
-        generateLog = true;
+    public void runApp(String member) {
+        Worker worker = new Worker(member);
+        worker.start();
+        sleep(30000);
+        worker.shutdown();
+        sleep(5000);
+    }
 
-        (new Thread() {
+    private void sleep(long millis) {
+        try {
+            Thread.sleep(millis);
+        } catch (InterruptedException ie) {
+            //
+        }
+    }
 
-            public void run() {
-                while (generateLog) {
-                    // Generate rand number between 1 to 10
-                    int rand = ran.nextInt(9) + 1;
-
-                    // Sleep for rand seconds
-                    try {
-                        Thread.sleep(rand * 1000);
-                    } catch (InterruptedException e) {
-                        logger.warn("WARN", e);
-                    }
 
-                    // Write rand number of logs
-                    for (int i = 0; i < rand; i++) {
-                        int eventIndex = (Math.abs(ran.nextInt())) % events.size();
-                        AuditEvent event = events.get(eventIndex);
-                        RequestContext.setUserId(member);
-                        event.logEvent();
-
-                        if ((rand % 4) == 1) {
-                            logger.debug("DEBUG level logging.....");
-                        } else if ((rand % 4) == 2) {
-                            logger.info("INFO level logging.....");
-                        } else if ((rand % 4) == 3) {
-                            logger.warn("WARN level logging.....");
-                        } else {
-                            logger.error("ERROR level logging.....");
-                        }
-                    }
+    public class Worker extends Thread {
+
+        private final String member;
+
+        private boolean shutdown = false;
+
+        public Worker(String member) {
+            this.member = member;
+        }
+
+        public void run() {
+            System.out.println("STARTING..................");
 
+            while (!shutdown) {
+                // Generate rand number between 1 to 10
+                int rand = ran.nextInt(9) + 1;
+
+                // Sleep for rand seconds
+                try {
+                    Thread.sleep(rand * 1000);
+                } catch (InterruptedException e) {
+                    logger.warn("WARN", e);
                 }
-            }
-        }).start();
 
-        return new ModelAndView("start.jsp");
-    }
+                // Write rand number of logs
+                for (int i = 0; i < rand; i++) {
+                    int eventIndex = (Math.abs(ran.nextInt())) % events.size();
+                    AuditEvent event = events.get(eventIndex);
+                    RequestContext.setUserId(member);
+                    event.logEvent();
+
+                    if ((rand % 4) == 1) {
+                        logger.debug("DEBUG level logging.....");
+                    } else if ((rand % 4) == 2) {
+                        logger.info("INFO level logging.....");
+                    } else if ((rand % 4) == 3) {
+                        logger.warn("WARN level logging.....");
+                    } else {
+                        logger.error("ERROR level logging.....");
+                    }
+                }
 
-    @RequestMapping(value = "/stop.do", method = RequestMethod.GET)
-    public ModelAndView stopLogging(HttpServletRequest servletRequest) {
-        generateLog = false;
-        return new ModelAndView("stop.jsp");
-    }
+            }
+        }
 
+        public void shutdown() {
+            this.shutdown = true;
+            try {
+                this.join();
+            } catch (InterruptedException ie) {
+                //
+            }
+            System.out.println("SHUTDOWN.......................");
+        }
+    }
 }

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1433581&r1=1433580&r2=1433581&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Tue Jan 15 19:03:50 2013
@@ -23,6 +23,9 @@
 
   <body>
     <release version="2.0-beta4" date="TBD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-149" dev="rgoers" type="fix">
+        SMTPAppender will only cache filtered events.
+      </action>
       <action issue="LOG4J2-145" dev="ggregory" type="fix">
         Add missing serial version IDs.
       </action>



Mime
View raw message