Could not read log4j.properties - log4j

We are having a module which contains log4j.properties and other files in it. And there is a separate module which is dependent on the 1st module(Realign). So we had made the 1st module as a jar file and placed it in the WEB-INF/lib folder of the second module(Reasign). We are running the modules in Liberty server. But still we are getting the File Not found exception as below,
log4j:ERROR Could not read configuration file
[file:/metlife/runtime/installed/wlp/usr/servers/bobr/apps/expanded/bobr.ear/BOBReassignmentWeb.war/WEB-INF/lib/Realignment.jar!/r_resources/log4j.properties].
[9/12/18 8:28:51:591 EDT] 000002de SystemErr R java.io.FileNotFoundException:
file:/metlife/runtime/installed/wlp/usr/servers/bobr/apps/expanded/bobr.ear/BOBReassignmentWeb.war/WEB-INF/lib/Realignment.jar!/r_resources/log4j.properties (No such file or directory)

It looks like your PropertiesConfigurator class is taking a file path (as a String). If you used a URL instead, I think that would work - that way, you would get a JAR URL which includes the path to the JAR (or WAR, EAR, etc.) archive and the path inside the JAR. If you have control over the PropertiesConfigurator code, then I would recommend changing it so that it loads the file via URL.
If that is not an option, then you could extract the properties files and put them on the file system directly. For example, you could create a directory in your server directory (for an example, we'll call it log4jProps). Then you could create a shared library in your server config (server.xml) like this:
<library id="log4j.props">
<fileset dir="${server.config.dir}/log4jProps" includes="r_resources/*properties"/>
</library>
then update your application configuration to use this library as a common shared library:
<application id="myApp" name="myApp" location="myApp.war"...>
<classloader commonLibraryRef ="log4j.props" />
</application>
For good measure, you should probably remove the properties file from your application archives - that way they won't be loaded from there, and then throw off the PropertiesConfigurator like it is now.
Hope this helps,
Andy

Related

How to hide Tomcat version from error messages when using embedded servers in Java

I have a java application where i'm using embedded Tomcat servers,
which looks like this
Tomcat tomcat = new Tomcat()
I'm creating an embedded tomcat server here.
Problem statement
whenever there's an error it displays information on which tomcat version i'm using,
how to hide this in java?
i have a little idea that i need to override ServerInfo.properties, but how do i do this?
I'm not sure how we can do this in java, but if you are using any build scripts like ant / gradle for distribution purpose, we can write a task to override / harden the jar file, and replace the ServerInfo.properties file with the customized value whatever we need.
the code for ant build scripts would look like
<target name="override.tomcat">
<jar destfile="path/to/tomcat-embed-core-9.0.62.jar" update="true">
<fileset dir="src/"> <!-- folder where you keep the directory/file to raplace-->
<include name="org/apache/catalina/util/ServerInfo.properties"/> <!-- file to replace within directory path in side the jar-->
</fileset>
</jar>
</target>
and in gradle
task overRideTomcat(type: Jar) {
from(zipTree(file("path/to/tomcat-embed-core-9.0.62.jar"))) {
exclude '**/org/apache/catalina/util/ServerInfo.properties'
}
from('src/') {
include('/org/apache/catalina/util/ServerInfo.properties')
}
archiveName "tomcat-embed-core-9.0.62.jar"
}
make sure you have the modified ServerInfo.properties file under src directory in the same path as you have mentioned in the include statement.

Configurate log4j for external jar

In my project (myProject) I use an external jar (external.jar). Both of them make logging with log4j.jar . With the help of log4j.properties file (located in myProject) I can configure logging from myProject. How can I configurate log levels of logging from the the external.jar without changing that jar file ?
Simpy adding package from external.jar ( let say org.external) in property file
log4j.logger.org.external=ERROR does not make any difference.
Here I have found the salution.

Transform external config in a web role

Can slowcheetah transform an external config file in an azure web role? e.g. I have logging info in log4net.config. But the transformed version does not get created when packaged.
I did not manage to get slowCheetah working in my Azure solution.
One alternative you can use is to create complete config files for each environment - e.g. :
log4net.debug.config
log4net.release.config
and copy the contents of these into the log4net.config at buildtime depending on the build configuration chosen.
This is done by adding a build target to your csproj file like so:
<Target Name="BeforeBuild">
<Delete Files="$(ProjectDir)log4net.config" />
<Copy SourceFiles="$(ProjectDir)log4net.$(Configuration).config"
DestinationFiles="$(ProjectDir)log4net.config" />
</Target>
(you may have to modify the paths in the script depending on where in the solution your config files are)
You can find more information on MSBuild and manipulating your .csproj file here and here

How to configure log4j and Selenium Grid?

When I launch a Grid hub using Ant, the logging is all by default on the console. I would like to know if there is a way wherein I can alter the build.xml file of Selenium Grid and include a log4j logger into it. I understand that I can specify a log file to Ant itself using the -logfile option. But this would cause the log file to be overwritten everytime the grid is launched. I want to ensure that the log files are automatically renamed after a threshold is reached for better maintenance. Any help and if possible some examples on how to do it would be greatly appreciated (I am new to using log4j which is why I am asking for some sample for this specific need).
You need to specify the location of the Log4j archive as well as its configuration file when starting up Selenium. In the following example, I've updated the classpath to load the log4j.hub.properties configuration file located in the /etc/selenium directory and the log4j.jar located in the /usr/lib/selenium directory. I've also setup some additional log files:
java -classpath /etc/selenium:/usr/lib/selenium/log4j.jar:/usr/lib/selenium/selenium-server-standalone.jar -Dlog4j.configuration=log4j.hub.properties org.openqa.grid.selenium.GridLauncher -role hub -log /var/log/selenium/hub.debug.log > /var/log/selenium/hub.output.log 2> /var/log/selenium/hub.error.log &
Then you can have something like the following to achieve what you are looking for:
log4j.rootLogger=ALL, file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=/var/log/selenium/hub.log
log4j.appender.file.Append=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} <%p> <%t> %m%n
You might want to look at this page for more information about how Selenium logging is working.

Warbler config.java_classes and log4j.properties

I'm packaging up a rails app with warbler and I want app specific logging. I've added the log4j and commons-loggin jar to the WEB-INF/lib directory, and I want to add log4j.properties to the WEB-INF/classes directory. The problem is, I also want environment specific logging, so my staging/production use different properties (ie. INFO instead of DEBUG) than my devel. I can't just do a:
config.java_classes = FileList["lib/log4j-#{RAILS_ENV}.properties"]
because Tomcat seems to look for the specific file log4j.properties. Is there any way to get warbler to rename this file to just log4j.properties? Or is there a better mechanism for app specific, environment specific logging?
And for the final answer. RAILS_ENV doesn't seem to work in warbler, but looking through the docs on warble config, there's a webxml attribute that contains rails.env, modifying my code to pull the file like:
config.java_classes = FileList["lib/properties/log4j.properties.#{config.webxml.rails.env}"]
Worked like a charm!
Guess I should just read further down in the warble file itself. You can configure pathmaps for the java_classes. Here's what I used:
config.java_classes = FileList["lib/properties/log4j.properties.#{RAILS_ENV}"]
config.pathmaps.java_classes << "%n"
The only problem I've found is that this doesn't actually put the log4j.properties in the WEB-INF/classes directory anymore. It now puts it in the Root. Seems odd that it specifically says in the docs:
One or more pathmaps defining how the java classes should be copied into WEB-INF/classes
I wouldn't think I'd have to add in that WEB-INF/classes path manually but I did. So finally then, this worked:
config.java_classes = FileList["lib/properties/log4j.properties.#{RAILS_ENV}"]
config.pathmaps.java_classes << "WEB-INF/classes/%n"
using the files log4j.properties.#{RAILS_ENV} in the lib/properties directory

Resources