Is it possible to put placeholders in log4j2.xml that can be read from application.yml? For example, I'd like to configure to/from addresses, as well as host and port in application.yml:
<SMTP name="Mail" subject="Error (${hostName})" to="${app.alerts.to}" from="${app.alerts.from}"
smtpHost="${mail.host}" smtpPort="${mail.port}" bufferSize="50">
<ThresholdFilter level="debug" onMatch="NEUTRAL" onMismatch="DENY" />
<PatternLayout>
<pattern>%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %m%n</pattern>
</PatternLayout>
</SMTP>
If it's possible to enable/disable this appender based on Spring Profiles (like this logback example), I'd love to hear about that too.
Is it possible to put placeholders in log4j2.xml that can be read from application.yml?
Not at the moment, anyway. Boot supports Log4j 2's default lookup locations. At first glance, it looks like it would be possible to add a new lookup that's backed by Spring's environment. Ideally, we'd maintain parity across all of the supported logging frameworks, i.e. it'd be nice if it worked with Logback and Log4j too.
If it's possible to enable/disable this appender based on Spring Profiles (like this logback example), I'd love to hear about that too.
AFAIK, unlike Logback, Log 4j 2 doesn't support conditional logic in its configuration.
I tried to create a ConfigurationProperties class that sets all the relevant values from application.yml as system properties.
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Properties;
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "logging")
@Data
public class LoggingConfig {
private MailSettings mail;
@Data
class MailSettings {
private String to;
private String from;
private String host;
private String port;
private boolean debug;
}
@PostConstruct
public void configure() {
// set values as system properties so they can be read in log4j2.xml
Properties props = new Properties();
props.put("logging.mail.to", mail.getTo());
props.put("logging.mail.from", mail.getFrom());
props.put("logging.mail.host", mail.getHost());
props.put("logging.mail.port", mail.getPort());
System.setProperties(props);
}
}
<SMTP name="Mail" subject="Error (${hostName})" to="${sys:logging.mail.to}"
from="${sys:logging.mail.from}" smtpHost="${sys:logging.mail.host}" bufferSize="20"
smtpPort="${sys:logging.mail.port}" smtpDebug="${sys:logging.mail.debug}">
However, Log4j2 seems to initialize before Spring, so I get an error on startup:
2014-10-16 13:56:35,538 ERROR Could not parse "${sys:logging.mail.port}" as an integer, using default value 0: java.lang.NumberFormatException: For input string: "${sys:logging.mail.port}" java.lang.NumberFormatException: For input string: "${sys:logging.mail.port}"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
Is there a workaround in the meantime or do I have to wait for a new lookup?
As you've seen, you can't use @ConfigurationProperties as the logging system is initialized too early. You might be able to do something with an ApplicationListener that listens to ApplicationEnvironmentPreparedEvent. You can access the Environment from the event and copy over the relevant properties. It would need to be ordered to run before LoggingApplicationListener.
Is it now possible to pass custom configs to log4j2 via application.properties ?
@deepika087 No, not at the moment (this issue is still open). It is possible if you use Logback (Boot's preferred logging framework).
Same requirement here as well:
I would like to re-use the (encrypted) data-source properties defined in my application.yml to create a JDBC appender through log4j2-spring.xml place-holders. This works fine for logback but I would prefer to use log4j2 because of its better support of asynchronous logging.
We've no plans to support this. Consider using logback or submitting a pull request.
@wilkinsona You mentioned earlier that there is an issue open for log4j2. Appreciate if you can point me that. Thanks
Are you referring to this comment? If so, the issue that I was referring to is this issue, not a Log4j issue.
Ok. Thanks
There seems to be a Epic open against log4j2 for supporting dependency management.
https://issues.apache.org/jira/browse/LOG4J2-1775
As of now, plugins are initialised by log4j2 itself & there is no straight forward way to inject into custom StrLookup class. Log4j2 expects the custom plugin that implements StrLookup to have no args constructor & there are no additional ways to refer to Environment, except using static variables to hold a reference to Environment when we receive ApplicationEnvironmentPreparedEvent & access this from our own StrLookup
@wilkinsona If we capture a a static reference to ConfigurableEnvironment during ApplicationEnvironmentPreparedEvent, can u help me with the following questions
ConfigurableEnvironment, when context is restarted via Spring boot actuator? If I have to, which API should I hook into to get a callback?ConfigurableEnvironment, when some properties are updated via Spring cloud bus/actuator? If I have to, which API should I hook into to get a callback?ConfigurableEnvironment during shut down, so that there will not be any memory leaks. If so, which API should I hook into to get a callback?Thanks again
Do we need to get fresh reference to ConfigurableEnvironment, when context is restarted via Spring boot actuator? If I have to, which API should I hook into to get a callback?
There's no actuator endpoint provided by Spring Boot to trigger a restart. Perhaps this is a Spring Cloud feature? Regardless, you'd get a new ApplicationEnvironmentPreparedEvent.
Do we need to get fresh reference to ConfigurableEnvironment, when some properties are updated via Spring cloud bus/actuator? If I have to, which API should I hook into to get a callback?
No I don't think so. The ConfigurableEnvironment is not itself replaced, only the PropertySources that it contains.
Dereference ConfigurableEnvironment during shut down, so that there will not be any memory leaks. If so, which API should I hook into to get a callback?
Shutdown might be tricky. You might be able to use ContextStoppedEvent, but if you just need to protect against memory leaks perhaps using a weak reference to the environment would be enough.
@philwebb Thanks. I'll give it a try
Please unsubscribe these mails
On Tuesday, 27 March, 2018, 2:09:10 PM IST, Ashok Koyi notifications@github.com wrote:
@philwebb Thanks. I'll give it a try
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
@RameshNaiduN Only you can do that. You need to unsubscribe from this issue and possibly unwatch this repository as well.