Java Code Examples for org.apache.log4j.Layout#format()

The following examples show how to use org.apache.log4j.Layout#format() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XMLLayoutTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
   * Tests CDATA element within NDC content.  See bug 37560.
   */
 public void testNDCWithCDATA() throws Exception {
     Logger logger = Logger.getLogger("com.example.bar");
     Level level = Level.INFO;
     String ndcMessage ="<envelope><faultstring><![CDATA[The EffectiveDate]]></faultstring><envelope>";
     NDC.push(ndcMessage);
     LoggingEvent event =
       new LoggingEvent(
         "com.example.bar", logger, level, "Hello, World", null);
     Layout layout = createLayout();
     String result = layout.format(event);
     NDC.clear();
     Element parsedResult = parse(result);
     NodeList ndcs = parsedResult.getElementsByTagName("log4j:NDC");
     assertEquals(1, ndcs.getLength());
     StringBuffer buf = new StringBuffer();
     for(Node child = ndcs.item(0).getFirstChild();
             child != null;
             child = child.getNextSibling()) {
         buf.append(child.getNodeValue());
     }
     assertEquals(ndcMessage, buf.toString());
}
 
Example 2
Source File: XMLLayoutTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
   * Tests CDATA element within exception.  See bug 37560.
   */
 public void testExceptionWithCDATA() throws Exception {
     Logger logger = Logger.getLogger("com.example.bar");
     Level level = Level.INFO;
     String exceptionMessage ="<envelope><faultstring><![CDATA[The EffectiveDate]]></faultstring><envelope>";
     LoggingEvent event =
       new LoggingEvent(
         "com.example.bar", logger, level, "Hello, World", new Exception(exceptionMessage));
     Layout layout = createLayout();
     String result = layout.format(event);
     Element parsedResult = parse(result);
     NodeList throwables = parsedResult.getElementsByTagName("log4j:throwable");
     assertEquals(1, throwables.getLength());
     StringBuffer buf = new StringBuffer();
     for(Node child = throwables.item(0).getFirstChild();
             child != null;
             child = child.getNextSibling()) {
         buf.append(child.getNodeValue());
     }
     assertTrue(buf.toString().indexOf(exceptionMessage) != -1);
}
 
Example 3
Source File: SystemTestAppender.java    From JVoiceXML with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String getContents(final Layout layout) {
        final StringBuilder content = new StringBuilder();
        synchronized (events) {
            for (LoggingEvent event : events) {
                final String message = layout.format(event);
                content.append(message);
//                final String[] throwable = event.getThrowableStrRep();
//                final String lf = System.getProperty("line.separator");
//                if (throwable != null) {
//                    for (String str : throwable) {
//                        content.append(str);
//                        content.append(lf);
//                    }
//                }
            }
        }
        return content.toString();
    }
 
Example 4
Source File: SystemTestAppender.java    From JVoiceXML with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
     * Writes the logs to the specified filename using the given layout.
     * @param layout the layout to use.
     * @param file the file where to write the messages.
     * @throws IOException
     *         error writing
     */
    public void writeToFile(final Layout layout, final File file)
            throws IOException {
        final FileWriter writer = new FileWriter(file);
        try {
            synchronized (events) {
                for (LoggingEvent event : events) {
                    final String message = layout.format(event);
                    writer.write(message);
//                    final String[] throwable = event.getThrowableStrRep();
//                    final String lf = System.getProperty("line.separator");
//                    if (throwable != null) {
//                        for (String str : throwable) {
//                            writer.write(str);
//                            writer.write(lf);
//                        }
//                    }
                }
            }
        } finally {
            writer.close();
        }
    }
 
Example 5
Source File: TestRollingFileAppenderExt.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void testRollingFileAppenderExt(@Injectable LoggingEvent event,
    @Injectable Layout layout) throws Exception {
  new Expectations() {
    {
      layout.format(event);
      result = "test";
    }
  };
  File cur = new File(System.getProperty("user.dir"));
  File temp = new File(cur, "temptestfile.log");
  if (temp.exists()) {
    temp.delete();
  }
  RollingFileAppenderExt ext = new RollingFileAppenderExt();
  ext.setLayout(layout);
  ext.setLogPermission("rw-------");
  ext.setFile(temp.getAbsolutePath());
  ext.setFile(temp.getAbsolutePath(), false, false, 300000);
  Assert.assertEquals(ext.getLogPermission(), "rw-------");
  Assert.assertTrue(temp.exists());

  temp.delete();
  ext.subAppend(event);
  Assert.assertTrue(temp.exists());

  ext.close();
  temp.delete();
  Assert.assertFalse(temp.exists());
}