Skip to content

[LOG4J2-3404] Creates default layouts using the available Configuration #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ public String getName() {

public Layout<? extends Serializable> getOrCreateLayout() {
if (layout == null) {
return PatternLayout.createDefaultLayout();
return PatternLayout.createDefaultLayout(configuration);
}
return layout;
}

public Layout<? extends Serializable> getOrCreateLayout(final Charset charset) {
if (layout == null) {
return PatternLayout.newBuilder().withCharset(charset).build();
return PatternLayout.newBuilder().withCharset(charset).withConfiguration(configuration).build();
}
return layout;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ protected static StatusLogger logger() {
return StatusLogger.getLogger();
}

/**
* For testing purposes.
*/
protected static int getManagerCount() {
return MAP.size();
}

/**
* May be overridden by managers to perform processing while the manager is being released and the
* lock is held. A timeout is passed for implementors to use as they see fit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ public static class Builder<B extends Builder<B>> extends AbstractOutputStreamAp

@Override
public OutputStreamAppender build() {
final Layout<? extends Serializable> layout = getLayout();
final Layout<? extends Serializable> actualLayout = layout == null ? PatternLayout.createDefaultLayout()
: layout;
return new OutputStreamAppender(getName(), actualLayout, getFilter(), getManager(target, follow, actualLayout),
final Layout<? extends Serializable> layout = getOrCreateLayout();
return new OutputStreamAppender(getName(), layout, getFilter(), getManager(target, follow, layout),
ignoreExceptions, getPropertyArray());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
*/
package org.apache.logging.log4j.core.appender;

import java.io.Serializable;
import java.io.Writer;

import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.StringLayout;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.config.plugins.Plugin;
Expand All @@ -47,9 +49,13 @@ public static class Builder<B extends Builder<B>> extends AbstractAppender.Build

@Override
public WriterAppender build() {
final StringLayout layout = (StringLayout) getLayout();
final StringLayout actualLayout = layout != null ? layout : PatternLayout.createDefaultLayout();
return new WriterAppender(getName(), actualLayout, getFilter(), getManager(target, follow, actualLayout),
final Layout<? extends Serializable> layout = getOrCreateLayout();
if (!(layout instanceof StringLayout)) {
LOGGER.error("Layout must be a StringLayout to log to ServletContext");
return null;
}
final StringLayout stringLayout = (StringLayout) layout;
return new WriterAppender(getName(), stringLayout, getFilter(), getManager(target, follow, stringLayout),
isIgnoreExceptions(), getPropertyArray());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.appender;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.junit.jupiter.api.Test;

public class AbstractAppenderBuilderTest {

@Test
public void testDefaultLayoutLeak() {
int expected = AbstractManager.getManagerCount();
final Configuration configuration = new DefaultConfiguration();
ConsoleAppender appender = ConsoleAppender.newBuilder().setConfiguration(configuration).setName("test").build();
configuration.addAppender(appender);
configuration.start();
configuration.stop();
assertEquals(expected, AbstractManager.getManagerCount(), "No managers should be left after shutdown.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.StringLayout;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.web.WebLoggerContextUtils;

/**
Expand All @@ -55,10 +55,8 @@ public ServletAppender build() {
LOGGER.error("No servlet context is available");
return null;
}
Layout<? extends Serializable> layout = getLayout();
if (layout == null) {
layout = PatternLayout.createDefaultLayout();
} else if (!(layout instanceof AbstractStringLayout)) {
Layout<? extends Serializable> layout = getOrCreateLayout();
if (!(layout instanceof StringLayout)) {
LOGGER.error("Layout must be a StringLayout to log to ServletContext");
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.StringLayout;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.web.WebLoggerContextUtils;

/**
Expand All @@ -55,10 +55,8 @@ public ServletAppender build() {
LOGGER.error("No servlet context is available");
return null;
}
Layout<? extends Serializable> layout = getLayout();
if (layout == null) {
layout = PatternLayout.createDefaultLayout();
} else if (!(layout instanceof AbstractStringLayout)) {
final Layout<? extends Serializable> layout = getOrCreateLayout();
if (!(layout instanceof StringLayout)) {
LOGGER.error("Layout must be a StringLayout to log to ServletContext");
return null;
}
Expand Down