Websphere Liberty: How to specify log4j2 configuration location? - log4j2

I'm trying to tell Websphere Liberty where is located my log4j2.xmlfile, but it isn't working.
In my file jvm.options I configure:
-Dlog4j.configurationFile=file:///${server.config.dir}/log4j2.xml
but it looks like Liberty does not understand the variable ${server.config.dir} in the jvm.options file. The file is in the same directory of the server.xml file.
How would I specify the log4j2.xml location for Liberty?

I think it should be possible by creating a Library entry in your server.xml such as:
<library id="log4jLib">
<folder dir="/opt/log4j2/config"/>
</library>
where the directory specified contains the log4j2 properties or xml file.
Then specify a classloader for your application like this:
<application id="test" name="test" type="ear" location="test.ear">
<classloader commonLibraryRef="log4jLib" />
</application>

The ${server.config.dir} variable is one of the Liberty built-in server config variables, these only apply within the server.xml (and included configurations).
When you run a Liberty server, the user.dir gets set to the same thing as ${server.config.dir}, so you could just specify the relative path to your log4j2.xml file in jvm.options as:
-Dlog4j.configurationFile=log4j2.xml

For Liberty I do the following;
I have a jvm.options file containing:
-Dlog4j.configurationFile=log4j2.xml
I place both files (log4j2.xml and jvm.options) in the server config.
I.e where tour server.xml etc is placed:
usr/servers/<myserver>:
server.xml
jvm.options
log4j2.xml
That does the trick for me.

There are a few ways of configuring log4j in WebSphere. Aside from the library entries method mentioned by pseudonym, you can simply drop the log4j2 configuration file to the global library directory at
wlp/usr/servers//lib/global
If there are files present in above location at the time an application is started, and that application does not have a classloader element configured, the application uses these libraries. If a class loader configuration is present, these libraries are not used unless the global library is explicitly referenced.
You can find more details about the global libraries in this link
WebSphere Liberty Shared Libraries

Related

How to set log4j.property to .jar location

I'm Setting up Log4j2 in a Spring-boot application. I now want to create a /log directory exactly where the .jar file is located.
This is needed as we start the java application from a startup script and the configuration should work on both windows and unix developer machines as well as a server.
I already tried with:
<RollingFile name="FileAppender" fileName="./logs/mylog.log"
filePattern="logs/mylog-%d{yyyy-MM-dd}-%i.log">
which just creates a log folder at the directory where the jar gets started.
then I read i should use .\log/mylog.log as .\ points to the directory of the jar file.
But then it just creates a folder called .\log.
I also tried with configuration with jvm arguments and calling them at the log4j2.xml with: ${logFile}. Now a directory gets created called '${logFile}.
The only ${} command working is the directory of the log4j configuration file. But as this is inside the jar it just gets me a pretty useless folder structure
Thanks in Advance
EDIT: In the End what I did was setting up two configuration files, log4j2.xml and log4j2-prod.xml
The log4j2.xml took the system property as Vikas Sachdeva mentioned, while the prod.xml got the location hard coded.
Not really the solution I was looking for but made it work.
One solution is to pass log directory location through system properties.
Configuration file will look like -
<RollingFile name="FileAppender" fileName="${sys:basePath}/mylog.log"
filePattern="${sys:basePath}/mylog-%d{yyyy-MM-dd}-%i.log">
Now, pass VM argument basePath with absolute path of directory containing JAR file -
java -jar myapp.jar -DbasePath=/home/ubuntu/app

Grails standalone JAR as linux service configure memory parameters

I have a Grails application that I run as a Linux service. Basically I create a symlink from /etc/init.d/mygrailsservice to mygrailsservice.jar.
I want to increase the amount of OS memory allocated to the service.
How do I configure this?
Have a look at the spring-boot docs for executable jars and using them as system services - https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html
Specifically https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html#deployment-script-customization-when-it-runs
With the exception of JARFILE and APP_NAME, the above settings can be
configured using a .conf file. The file is expected next to the jar
file and have the same name but suffixed with .conf rather than .jar.
For example, a jar named /var/myapp/myapp.jar will use the
configuration file named /var/myapp/myapp.conf.
myapp.conf.
JAVA_OPTS=-Xmx1024M
LOG_FOLDER=/custom/log/folder

Common Facelets files in shared library JAR outside /WEB-INF/lib

I have a common shared Library (that is setup as a Shared Library in Websphere Application server).
The folder structure of that jar is:
UtilityJAR
----src
-com
-test
-TestClass.java
---- META-INF
-resources
-template.xhtml
-css
-style.css
In my web Project, I have a template client file called User.xhtml that uses the template file from the above Shared Library using
ui:composition template="/template.xhtml"
When I have the above jar file in the WEB-INF/lib folder of the Web application, the application works fine without any issues (template.xhtml is recognized). When I remove the jar from the Lib folder of this application and put it as a Shared Library in Websphere (because I need this jar file from more than 4 applications and I don't want to copy this jar in all the 4 applications), I get the following error message.
[9/24/14 14:09:17:936 EDT] 00000113 ServletWrappe E com.ibm.ws.webcontainer.servlet.ServletWrapper service SRVE0014E: Uncaught service() exception root cause Faces Servlet: java.io.FileNotFoundException: /template.xhtml Not Found in ExternalContext as a Resource
The Utility jar has faces-config in it and has #ManagedBean annotations that work when the jar is inside the application's WEB-INF/lib folder.
Anybody faced this problem before? thanks for your help.
Web fragment JARs containing webapp resources MUST be placed in webapp's /WEB-INF/lib.
From Servlet 3.0 specification page 37 (emphasis mine):
4.6 Resources
...
The getResource and getResourceAsStream methods take a String with a leading
“/” as an argument that gives the path of the resource relative to the root of the
context or relative to the META-INF/resources directory of a JAR file inside the
web application’s WEB-INF/lib directory. These methods will first search the root
of the web application context for the requested resource before looking at any of the
JAR files in the WEB-INF/lib directory. The order in which the JAR files in the
WEB-INF/lib directory are scanned is undefined. This hierarchy of documents may
exist in the server’s file system, in a Web application archive file, on a remote server,
or at some other location.
...
If you really want to place them elsewhere (bad idea!), then you'd need to homegrow a custom Facelets resource resolver. You can find a kickoff example here: How to create a modular JSF 2.0 application?
See also:
JSF facelets template packaging

JSF with External Resource Bundle

I'm doing a JSF 2.0 application on Tomcat 6.x. I have a resource bundle in different languages, is it possible to externalize the properties files outside the webapp?
For the moment I have this in my faces-config.xml:
<locale-config>
<default-locale>fr</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>es</supported-locale>
</locale-config>
<resource-bundle>
<base-name>front</base-name>
<var>messages</var>
</resource-bundle>
What should I do?
Thanks.
Yes, that's definitely possible. To the point, just put the files in the classpath. You can do that by placing it in any of existing paths covered by the webapp's runtime classpath, or by adding the new path to the webapp's runtime classpath.
You could add a new path to the classpath by specifying it in shared.loader of Tomcat's /conf/catalina.properties. Assuming that you've placed front*.properties files in /var/webapp/conf folder, then you need to specify the shared.loader as follows:
shared.loader = /var/webapp/conf
May be because of the spaces in the folder name; try to put the resource bundle file in another place, eg.: c:/temp/resources/

External properties file with JSF2

I'd like to NOT put my properties file in the war file's classes directory. Can I do this and what would I specify in faces-config.xml for it to use the correct resource-bundle ?
Thanks
Binh Nguyen
Put it in an external folder and add its path to the runtime classpath. Then you can access it from the classpath the usual way as if it's in /WEB-INF/classes (which is just by default part of the classpath).
Adding the path to an external folder to the classpath is best to be configured at the webserver level. In Tomcat for example, you can specify it in the shared.loader or common.loader property of Tomcat's /conf/catalina.properties file.
shared.loader = /path/to/propertiesfiles

Resources