Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -214,11 +214,20 @@ public Filter getFilter() {
return filter;
}

/**
* @deprecated Use {@link #withFilter(Filter)} instead which performs the same function.
*/
@Deprecated
public B withtFilter(Filter filter) {
this.filter = filter;
return asBuilder();
}

public B withFilter(Filter filter) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have mixed feelings about this change: yes, it would be nice to have a correctly spelled method, but the other one was already released and it is API.

Don't change the name of the method, deprecate it and add another one with the correct spelling.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deprecated the misspelled method and created a new one with the correct spelling as suggested.

Copy link
Author

@phreed phreed Mar 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were two such cases.
What is the rule for naming methods?
When should the name be setFilter and when withFilter?
I just noticed that the appender was changed from withFilter to setFilter.
Should the same be done here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have mixed feelings about this change: yes, it would be nice to have a correctly spelled method, but the other one was already released and it is API.

Don't change the name of the method, deprecate it and add another one with the correct spelling.

Just add a new method with the correct name and deprecate the bogus one. Simple ;-)

this.filter = filter;
return asBuilder();
}

@Override
public LoggerConfig build() {
final String name = loggerName.equals(ROOT) ? Strings.EMPTY : loggerName;
Expand Down Expand Up @@ -884,11 +893,20 @@ public Filter getFilter() {
return filter;
}

/**
* @deprecated Use {@link #withFilter(Filter)} instead which performs the same function.
*/
@Deprecated
public B withtFilter(Filter filter) {
this.filter = filter;
return asBuilder();
}

public B withFilter(Filter filter) {
this.filter = filter;
return asBuilder();
}

@Override
public LoggerConfig build() {
LevelAndRefs container = LoggerConfig.getLevelAndRefs(level, refs, levelAndRefs, config);
Expand Down
65 changes: 47 additions & 18 deletions src/site/xdoc/manual/customconfig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,17 @@ LoggerContext ctx = Configurator.initialize(builder.build());
</p>
<p>
The easiest way to achieve this is to extend one of the standard Configuration classes
(XMLConfiguration, JSONConfiguration) and then create a new ConfigurationFactory for the extended class.
(XmlConfiguration, JSONConfiguration) and then create a new ConfigurationFactory for the extended class.
After the standard configuration completes the custom configuration can be added to it.
</p>
<p>
The example below shows how to extend XMLConfiguration to manually add an Appender and a LoggerConfig
The example below shows how to extend XmlConfiguration to manually add an Appender and a LoggerConfig
to the configuration.
</p>
<pre class="prettyprint linenums">
@Plugin(name = "MyXMLConfigurationFactory", category = "ConfigurationFactory")
@Plugin(name = "MyXmlConfigurationFactory", category = "ConfigurationFactory")
@Order(10)
public class MyXMLConfigurationFactory extends ConfigurationFactory {
public class MyXmlConfigurationFactory extends ConfigurationFactory {

/**
* Valid file extensions for XML files.
Expand All @@ -245,8 +245,9 @@ public class MyXMLConfigurationFactory extends ConfigurationFactory {
* @param source The InputSource.
* @return The Configuration.
*/
public Configuration getConfiguration(InputSource source) {
return new MyXMLConfiguration(source, configFile);
@Override
public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) {
return new MyXmlConfiguration(loggerContext, source);
}

/**
Expand All @@ -256,29 +257,57 @@ public class MyXMLConfigurationFactory extends ConfigurationFactory {
public String[] getSupportedTypes() {
return SUFFIXES;
}

}
</pre>

<pre class="prettyprint linenums">
public class MyXmlConfiguration extends XmlConfiguration {

public class MyXMLConfiguration extends XMLConfiguration {
public MyXMLConfiguration(final ConfigurationFactory.ConfigurationSource configSource) {
super(configSource);
public MyXmlConfiguration(LoggerContext loggerContext, ConfigurationSource configSource) {
super(loggerContext, configSource);
}

@Override
protected void doConfigure() {
super.doConfigure();
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = context.getConfiguration();
final Configuration config = ctx.getConfiguration();
final Layout layout = PatternLayout.createDefaultLayout(config);
final Appender appender = FileAppender.createAppender("target/test.log", "false", "false", "File", "true",
"false", "false", "4000", layout, null, "false", null, config);
appender.start();
addAppender(appender);
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", "info", "org.apache.logging.log4j",
"true", refs, null, config, null );
loggerConfig.addAppender(appender, null, null);
final FileAppender fileAppender = FileAppender.newBuilder()
.withFileName("target/test.log")
.withAppend(false)
.withLocking(false)
.setName("File")
.setImmediateFlush(true)
.setIgnoreExceptions(false)
.setBufferedIo(false)
.setBufferSize(4000)
.setLayout(layout)
.setFilter(null)
.withAdvertise(false)
.withAdvertiseUri(null)
.setConfiguration(config)
.build();
fileAppender.start();
addAppender(fileAppender);

AppenderRef[] refs = new AppenderRef[] {AppenderRef.createAppenderRef("File", null, null)};
LoggerConfig loggerConfig = LoggerConfig.newBuilder()
.withAdditivity(false)
.withLevel(Level.INFO)
.withIncludeLocation("true")
.withRefs(refs)
.withProperties(null)
.withConfig(config)
.withFilter(null)
.build();

loggerConfig.addAppender(fileAppender, null, null);
addLogger("org.apache.logging.log4j", loggerConfig);
}
}</pre>
}
</pre>
</subsection>
<a name="AddingToCurrent"/>
<subsection name="Programmatically Modifying the Current Configuration after Initialization">
Expand Down