You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2014/05/25 06:52:07 UTC
svn commit: r1597387 - in /logging/log4j/log4j2/trunk/log4j-core/src:
main/java/org/apache/logging/log4j/core/appender/
main/java/org/apache/logging/log4j/core/layout/
test/java/org/apache/logging/log4j/core/layout/
Author: mattsicker
Date: Sun May 25 04:52:07 2014
New Revision: 1597387
URL: http://svn.apache.org/r1597387
Log:
Migrate HtmlLayout to use typed parameters in factory method.
- Also added createDefaultLayout method for convenience.
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/SmtpAppender.java
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HtmlLayout.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/HtmlLayoutTest.java
Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/SmtpAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/SmtpAppender.java?rev=1597387&r1=1597386&r2=1597387&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/SmtpAppender.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/SmtpAppender.java Sun May 25 04:52:07 2014
@@ -132,7 +132,7 @@ public final class SmtpAppender extends
final int bufferSize = bufferSizeStr == null ? DEFAULT_BUFFER_SIZE : Integer.parseInt(bufferSizeStr);
if (layout == null) {
- layout = HtmlLayout.createLayout(null, null, null, null, null, null);
+ layout = HtmlLayout.createDefaultLayout();
}
if (filter == null) {
filter = ThresholdFilter.createFilter(null, null, null);
Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HtmlLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HtmlLayout.java?rev=1597387&r1=1597386&r2=1597387&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HtmlLayout.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HtmlLayout.java Sun May 25 04:52:07 2014
@@ -32,6 +32,7 @@ import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginDefault;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.util.Charsets;
import org.apache.logging.log4j.core.util.Constants;
@@ -56,6 +57,8 @@ public final class HtmlLayout extends Ab
private static final String DEFAULT_CONTENT_TYPE = "text/html";
+ public static final String DEFAULT_FONT_FAMILY = "arial,sans-serif";
+
private final long jvmStartTime = ManagementFactory.getRuntimeMXBean().getStartTime();
// Print no location info by default
@@ -103,12 +106,19 @@ public final class HtmlLayout extends Ab
super(charset);
this.locationInfo = locationInfo;
this.title = title;
- this.contentType = contentType;
+ this.contentType = addCharsetToContentType(contentType);
this.font = font;
this.fontSize = fontSize;
this.headerSize = headerSize;
}
+ private String addCharsetToContentType(final String contentType) {
+ if (contentType == null) {
+ return DEFAULT_CONTENT_TYPE + "; charset=" + getCharset();
+ }
+ return contentType.contains("charset") ? contentType : contentType + "; charset=" + getCharset();
+ }
+
/**
* Format as a String.
*
@@ -306,33 +316,35 @@ public final class HtmlLayout extends Ab
* @param locationInfo If "true", location information will be included. The default is false.
* @param title The title to include in the file header. If none is specified the default title will be used.
* @param contentType The content type. Defaults to "text/html".
- * @param charsetName The character set to use. If not specified, the default will be used.
+ * @param charset The character set to use. If not specified, the default will be used.
* @param fontSize The font size of the text.
* @param font The font to use for the text.
* @return An HTML Layout.
*/
@PluginFactory
public static HtmlLayout createLayout(
- @PluginAttribute("locationInfo") final String locationInfo,
- @PluginAttribute("title") String title,
- @PluginAttribute("contentType") String contentType,
- @PluginAttribute("charset") final String charsetName,
+ @PluginAttribute("locationInfo") @PluginDefault("false") final boolean locationInfo,
+ @PluginAttribute("title") @PluginDefault(DEFAULT_TITLE) final String title,
+ @PluginAttribute("contentType") @PluginDefault(DEFAULT_CONTENT_TYPE) String contentType,
+ @PluginAttribute("charset") @PluginDefault("UTF-8") final Charset charset,
@PluginAttribute("fontSize") String fontSize,
- @PluginAttribute("fontName") String font) {
- final Charset charset = Charsets.getSupportedCharset(charsetName, Charsets.UTF_8);
- if (font == null) {
- font = "arial,sans-serif";
- }
+ @PluginAttribute("fontName") @PluginDefault(DEFAULT_FONT_FAMILY) final String font) {
final FontSize fs = FontSize.getFontSize(fontSize);
fontSize = fs.getFontSize();
final String headerSize = fs.larger().getFontSize();
- final boolean info = Boolean.parseBoolean(locationInfo);
- if (title == null) {
- title = DEFAULT_TITLE;
- }
if (contentType == null) {
contentType = DEFAULT_CONTENT_TYPE + "; charset=" + charset;
}
- return new HtmlLayout(info, title, contentType, charset, font, fontSize, headerSize);
+ return new HtmlLayout(locationInfo, title, contentType, charset, font, fontSize, headerSize);
+ }
+
+ /**
+ * Creates an HTML Layout using the default settings.
+ *
+ * @return an HTML Layout.
+ */
+ public static HtmlLayout createDefaultLayout() {
+ return new HtmlLayout(false, DEFAULT_TITLE, DEFAULT_CONTENT_TYPE + "; charset=UTF-8", Charsets.UTF_8,
+ DEFAULT_FONT_FAMILY, FontSize.SMALL.getFontSize(), FontSize.SMALL.larger().getFontSize());
}
}
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/HtmlLayoutTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/HtmlLayoutTest.java?rev=1597387&r1=1597386&r2=1597387&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/HtmlLayoutTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/HtmlLayoutTest.java Sun May 25 04:52:07 2014
@@ -27,6 +27,12 @@ import org.apache.logging.log4j.core.Bas
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.config.DefaultConfiguration;
+import org.apache.logging.log4j.core.config.Node;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
+import org.apache.logging.log4j.core.config.plugins.util.PluginType;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.util.Charsets;
import org.apache.logging.log4j.test.appender.ListAppender;
import org.junit.AfterClass;
@@ -64,20 +70,20 @@ public class HtmlLayoutTest {
@Test
public void testDefaultContentType() {
- final HtmlLayout layout = HtmlLayout.createLayout("true", null, null, null, "small", null);
+ final HtmlLayout layout = HtmlLayout.createDefaultLayout();
assertEquals("text/html; charset=UTF-8", layout.getContentType());
}
@Test
public void testContentType() {
- final HtmlLayout layout = HtmlLayout.createLayout("true", null, "text/html; charset=UTF-16", null, "small",
+ final HtmlLayout layout = HtmlLayout.createLayout(true, null, "text/html; charset=UTF-16", null, "small",
null);
assertEquals("text/html; charset=UTF-16", layout.getContentType());
}
@Test
public void testDefaultCharset() {
- final HtmlLayout layout = HtmlLayout.createLayout("true", null, null, null, "small", null);
+ final HtmlLayout layout = HtmlLayout.createLayout(true, null, null, null, "small", null);
assertEquals(Charsets.UTF_8, layout.getCharset());
}
@@ -85,22 +91,33 @@ public class HtmlLayoutTest {
* Test case for MDC conversion pattern.
*/
@Test
- public void testLayoutIncludeLocationNo() {
+ public void testLayoutIncludeLocationNo() throws Exception {
testLayout(false);
}
@Test
- public void testLayoutIncludeLocationYes() {
+ public void testLayoutIncludeLocationYes() throws Exception {
testLayout(true);
}
- private void testLayout(boolean includeLocation) {
+ private void testLayout(final boolean includeLocation) throws Exception {
final Map<String, Appender> appenders = root.getAppenders();
for (Appender appender : appenders.values()) {
root.removeAppender(appender);
}
// set up appender
- final HtmlLayout layout = HtmlLayout.createLayout("" + includeLocation, null, null, null, "small", null);
+ // note: yes, this is a bit of a roundabout way to constructing the layout, but without builder classes, this
+ // is the most reliable way to get the default values for everything
+ // TODO: this could probably be tested more easily using a config file
+ final Node node = new Node();
+ node.getAttributes().put("locationInfo", Boolean.toString(includeLocation));
+ final HtmlLayout layout =
+ new PluginBuilder<HtmlLayout>(new PluginType<HtmlLayout>(HtmlLayout.class, "HtmlLayout", false, false))
+ .withFactoryMethodAnnotatedBy(PluginFactory.class)
+ .withConfiguration(new DefaultConfiguration())
+ .withConfigurationNode(node)
+ .forLogEvent(new Log4jLogEvent())
+ .build();
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();