I am trying to load a configuration file (.yaml) to configure log4j2 in a SpringBoot application. Using documentation provided by log4j2 team I am trying to execute a ConfigurationFactory. But nothing is executed. I have to do it when applications starts because there are some properties not defined until this point.
This is the code I have:
#Plugin(name = "LoggingConfigurationFactory", category = "ConfigurationFactory")
#Order(10)
public class LoggingConfigurationFactory extends ConfigurationFactory {
/**
* Valid file extensions for XML files.
*/
protected static final String[] SUFFIXES = new String[] {".yaml", "*"};
/**
* Return the Configuration.
* #param source The InputSource.
* #return The Configuration.
*/
#Override
public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
return new NuarYamlConfiguration(loggerContext, source);
}
/**
* Returns the file suffixes for XML files.
* #return An array of File extensions.
*/
public String[] getSupportedTypes() {
return SUFFIXES;
}
}
If anyone knows how to do it, I would appreciate it
Thanks in advance
Peter
Related
I'm using a Spring Boot application. For now, the use of the plugins is very simple. I'm just following the tutorial. My plugin is started, I'm trying to find the extensions like this:
final List<MyExtensionPoint> sections = pluginManager.getExtensions(MyExtensionPoint.class);
but Pf4j doesn't return the extensions.
When I'm following the code execution, I can see this code in the AbstractExtensionFinder:
if (type.isAssignableFrom(extensionClass)) {
ExtensionWrapper extensionWrapper = createExtensionWrapper(extensionClass);
result.add(extensionWrapper);
log.debug("Added extension '{}' with ordinal {}", className, extensionWrapper.getOrdinal());
} else {
log.trace("'{}' is not an extension for extension point '{}'", className, type.getName());
if (RuntimeMode.DEVELOPMENT.equals(pluginManager.getRuntimeMode())) {
checkDifferentClassLoaders(type, extensionClass);
}
}
I can understand the program is not entering inside the condition because I have 2 different classloaders: PluginClassLoader (for the extension) and RestartClassLoader (from Spring for the interface of the extension point).
I don't understand why it will be a problem because I think to instanciate the extension class, the PluginClassloader will use the parent class loader (RestartClassLoader) to find the interface.
Where is my mistake ? How to fix it ?
Thank you.
I extended the DevelopmentPluginLoader to pass the Spring classloader.
public class MyDevelopmentPluginLoader extends DevelopmentPluginLoader {
private ClassLoader parentClassLoader;
/**
* #param pluginManager
*/
public MyDevelopmentPluginLoader(final PluginManager pluginManager, final ClassLoader parentClassLoader) {
super(pluginManager);
this.parentClassLoader = parentClassLoader;
}
#Override
protected PluginClassLoader createPluginClassLoader(final Path pluginPath, final PluginDescriptor pluginDescriptor) {
return new PluginClassLoader(pluginManager, pluginDescriptor, parentClassLoader);
}
}
and in my custom PluginManager, I created and instance of my PluginLoader:
protected PluginLoader createPluginLoader() {
final CompoundPluginLoader compoundPluginLoader = new CompoundPluginLoader();
final PluginLoader developmentPluginLoader = new MyDevelopmentPluginLoader(this, getClass().getClassLoader());
final PluginLoader jarPluginLoader = new JarPluginLoader(this);
final PluginLoader defaultPluginLoader = new DefaultPluginLoader(this);
// #formatter:off
return compoundPluginLoader.
add(developmentPluginLoader, this::isDevelopment).
add(jarPluginLoader, this::isNotDevelopment).
add(defaultPluginLoader, this::isNotDevelopment);
// #formatter:on
}
I am writing a custom Info Contributor. I have a properties file that is generated during the build process in target/classes folder.
How Can I use this generated file in the custom info contributor.
I checked the below code for GitInfoContributor
public class GitInfoContributor extends InfoPropertiesInfoContributor<GitProperties> {
public GitInfoContributor(GitProperties properties) {
this(properties, Mode.SIMPLE);
}
public GitInfoContributor(GitProperties properties, Mode mode) {
super(properties, mode);
}
#Override
public void contribute(Info.Builder builder) {
builder.withDetail("git", generateContent());
}
#Override
protected PropertySource<?> toSimplePropertySource() {
Properties props = new Properties();
copyIfSet(props, "branch");
String commitId = getProperties().getShortCommitId();
if (commitId != null) {
props.put("commit.id", commitId);
}
copyIfSet(props, "commit.time");
return new PropertiesPropertySource("git", props);
}
/**
* Post-process the content to expose. By default, well known keys representing dates
* are converted to {#link Instant} instances.
* #param content the content to expose
*/
#Override
protected void postProcessContent(Map<String, Object> content) {
replaceValue(getNestedMap(content, "commit"), "time", getProperties().getCommitTime());
replaceValue(getNestedMap(content, "build"), "time", getProperties().getInstant("build.time"));
}
}
I am not able to figure out how the git properties are being injected to GitProperties class here ?
I need to do the same for my custom info contributor using my properties file.
In addition to a git contributor, Spring boot also comes with a EnvironmentInfoContributor. This contributor automatically includes all application properties starting with info.*.
So the easiest solution is to make sure your custom properties all start with info.* and to treat your custom properties file as an additional application properties file. This can be done by providing the spring.config.additional-location VM argument. For example:
java -jar -Dspring.config.additional-location=classpath:custom-properties myApp.jar
Now you only have to enable the environment info contributor by setting:
management.info.env.enabled=true
If this isn't possible, you can indeed write your own contributor. One way of doing so is by programmatically reading your properties and converting it to a Map<String, String>. Once that's done, you can call the Info.Builder.withDetails(..) method with your Map (see the source code of EnvironmentInfoContributor).
I have the following code and I migrated my project from spring 3 to spring 4.
But ExpressionEvaluationUtils is obsolete actually.
Do you have any idea to replace it ?
/**
* Set quote escaping for this tag, as boolean value. Default is "true".
*/
public void setQuoteEscape(String quoteEscape) throws JspException {
this.quoteEscape = ExpressionEvaluationUtils.evaluateBoolean("quoteEscape", quoteEscape, pageContext);
}
/**
* Set breakLine escaping for this tag, as boolean value. Default is "true".
*/
public void setBackslashEscape(String backslashEscape) throws JspException {
this.backslashEscape = ExpressionEvaluationUtils.evaluateBoolean("backslashEscape", backslashEscape, pageContext);
}
Do you have any idea to replace it?
I am working on an existing system that uses log4j, I want to update to log4j2.
There is a custom spring bean that loads the configuration from a file. I need to keep this approach. I cannot use the "log4j.configurationFile" system property.
We have a properties file where the path to the current log4j.xml is specified ( NFS share )
The spring bean has this code ...
public class Log4jConfigurationBean implements ResourceLoaderAware,
InitializingBean {
private ResourceLoader resourceLoader;
private boolean enabled;
private String location;
/**
* Default, no argument constructor.
*/
public Log4jConfigurationBean() {
enabled = true;
}
/**
* Sets whether or not this bean should load an external configuration
* defined by {#link #setLocation(Resource)}. If <code>false</code>, this
* bean does nothing.
*
* <p>
* Default value is <code>true</code>.
* </p>
*
* #param enabled
* <code>false</code> causes this bean to do nothing
*/
public void setEnabled(final boolean enabled) {
this.enabled = enabled;
}
/**
* Sets the location of the external log4j configuration (xml or properties)
* to be loaded.
*
* #param location
* the location of the external configuration to be loaded.
* #throws IllegalStateException
* if there is a problem resolving the location resource
* #throws NullPointerException
* if <code>resource</code> is <code>null</code>
*/
public void setLocation(final String location) {
this.location = StringUtils.trimToNull(location);
}
#Override
public void setResourceLoader(final ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
/**
* #throws IllegalStateException
* if enabled and no location has be set, or if the external
* configuration is neither xml or properties.
*/
#Override
public void afterPropertiesSet() throws Exception {
URL configURL = null;
if (null != location) {
try {
final Resource resource = resourceLoader.getResource(location);
if (null != resource) {
configURL = resource.getURL();
}
} catch (IOException e) {
throw new IllegalArgumentException(
"Could not resolve configuration location due to error: ",
e);
}
}
if (enabled && null == configURL) {
throw new IllegalStateException(
"Log4j configuration enabled, but configuration location is not set.");
}
if (enabled) {
if (configURL.getFile().toLowerCase().endsWith(".xml")) {
DOMConfigurator.configure(configURL);
} else if (configURL.getFile().toLowerCase()
.endsWith(".properties")) {
PropertyConfigurator.configure(configURL);
} else {
throw new IllegalStateException(
"Configuration must be properties or xml: "
+ configURL.getFile());
}
}
}
}
In log4j2 there is no PropertyConfigurator.
How can I load the log4j2.xml file the same way.
The file path to the log4j2.xml file is specified in a spring property file.
The goal is to have the war files contain a log4j2.xml file in the classpath. This will be used when developing on your local box.
When the web apps are deployed to a qa environment, there is a property file containing the following key/value pair...
# Should an external file be used for log4j configuration
log4j.enabled=true
log4j.location=file:/paht to log4j2.xml
A spring bean is using these values to decide if an external log4j2.xml file should be used instead of the one on the classpath.
I tried with a spring bean like this... the code is executed, but it still uses the configuration file on the classpath.
public class Log4j2ConfigurationBean implements ResourceLoaderAware, InitializingBean {
private static final Logger log = LoggerFactory.getLogger(Log4j2ConfigurationBean.class);
private ResourceLoader resourceLoader;
private boolean enabled;
private String location;
/**
* Default, no argument constructor.
*/
public Log4j2ConfigurationBean() {
enabled = true;
}
/**
* Sets whether or not this bean should load an external configuration defined by {#link #setLocation(Resource)}. If <code>false</code>, this bean does nothing.
*
* <p>
* Default value is <code>true</code>.
* </p>
*
* #param enabled
* <code>false</code> causes this bean to do nothing
*/
public void setEnabled(final boolean enabled) {
this.enabled = enabled;
}
/**
* Sets the location of the external log4j configuration (xml or properties) to be loaded.
*
* #param location
* the location of the external configuration to be loaded.
* #throws IllegalStateException
* if there is a problem resolving the location resource
* #throws NullPointerException
* if <code>resource</code> is <code>null</code>
*/
public void setLocation(final String location) {
this.location = StringUtils.trimToNull(location);
}
#Override
public void setResourceLoader(final ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
/**
* #throws IllegalStateException
* if enabled and no location has be set, or if the external configuration is neither xml or properties.
*/
#Override
public void afterPropertiesSet() throws Exception {
URL configURL = null;
if (enabled) {
if (StringUtils.isBlank(location)) {
throw new IllegalStateException("Log4j2 configuration enabled, but configuration location is not set.");
}
try {
System.out.println(this.getClass().getName() + " : Loading log4j2 configuration with " + location);
final Resource resource = resourceLoader.getResource(location);
if (null != resource) {
configURL = resource.getURL();
}
} catch (IOException e) {
throw new IllegalArgumentException("Could not resolve configuration location due to error: ", e);
}
if (configURL.getFile().toLowerCase().endsWith(".xml")) {
try {
System.setProperty("Log4jContextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
System.setProperty("AsyncLogger.RingBufferSize", "8192");
ConfigurationFactory configurationFactory = XmlConfigurationFactory.getInstance();
ConfigurationSource configurationSource = new ConfigurationSource(configURL.openStream(), configURL);
Configuration configuration = configurationFactory.getConfiguration(configurationSource);
configuration.start();
log.info("Log4j2 configured with {}", location);
log.info("System property Log4jContextSelector set to {}", System.getProperty("Log4jContextSelector"));
log.info("System property AsyncLogger.RingBufferSize set to {}", System.getProperty("AsyncLogger.RingBufferSize"));
} catch (Exception e) {
System.out.println(this.getClass().getName() + " : Could not initialize log4j2 with resource " + location);
System.out.println(e.getStackTrace());
}
} else {
throw new IllegalStateException("Configuration must be xml: " + configURL.getFile());
}
} else {
System.out.println(this.getClass().getName() + " : External log4j2 configuration not configured.");
}
}
}
Thanks.
Check out the How do I configure log4j2 in code without a configuration file? section here - http://logging.apache.org/log4j/2.x/faq.html
I am using JBoss 5.1 with Hibernate 3.6, JPA 2.0 and Spring 3.0.5.
I use maven to build the EAR file which looks like :
AutoTrader.ear
-------> META-INF
--------------> application.xml
--------------> jboss-app.xml
--------------> MANIFEST.MF
-------> AutoTrader.war
if I deploy this ear file in JBoss 5.1, i get the error
org.springframework.dao.InvalidDataAccessApiUsageException: Not an entity: class uk.co.aol.shipmanager.domain.Manager; nested exception is ja
va.lang.IllegalArgumentException: Not an entity: class uk.co.aol.shipmanager.domain.Subscription
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:286) ~[at_war-1.0.war:3
.0.5.RELEASE]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:104) ~[at_war-1.0.war:3.0.5.RELEASE
]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:368) ~[at_war-1.
0.war:3.0.5.RELEASE]
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58
) ~[at_war-1.0.war:3.0.5.RELEASE]
However, if I deploy the war file exploded, it works fine.
Any suggestions are welcome.
Thanks,
Adi
UPDATE:
I have added a ResourceScanner which extends NativeScanner:
public class ResourceScanner extends NativeScanner {
#Override
public Set<Class<?>> getClassesInJar(final URL jarToScan,
final Set<Class<? extends Annotation>> annotationsToLookFor) {
return super.getClassesInJar(patchUrl(jarToScan), annotationsToLookFor);
}
#Override
public Set<NamedInputStream> getFilesInJar(final URL jarToScan, final Set<String> filePatterns) {
return super.getFilesInJar(patchUrl(jarToScan), filePatterns);
}
#Override
public Set<Package> getPackagesInJar(final URL jarToScan,
final Set<Class<? extends Annotation>> annotationsToLookFor) {
return super.getPackagesInJar(patchUrl(jarToScan), annotationsToLookFor);
}
#Override
public String getUnqualifiedJarName(final URL jarToScan) {
return super.getUnqualifiedJarName(patchUrl(jarToScan));
}
/**
* Patch the VFS URL to a FILE protocol URL.
*
* #param url
* original URL.
* #return either the original, either the corresponding FILE protocol of given VFS URL.
*/
protected URL patchUrl(final URL url) {
String protocol = url.getProtocol();
if (protocol.equals("vfs")) {
try {
File file = new File(url.getFile());
return file.toURI().toURL();
} catch (final MalformedURLException e) {
return url;
} catch (IOException e) {
e.printStackTrace();
return url;
}
}
return url;
}
}
and, in spring-persistence.xml,
<property name="hibernate.ejb.resource_scanner" value="uk.co.aol.shipmanager.ResourceScanner"/>
This again works in the exploded war file.
But in case of a EAR file, the protocol is vfszip not vfs.
Please tell what to do???
did you tried to to use the following system parameter and see if it helped resolved the issue?
-Dorg.jboss.net.protocol.file.useURI=false