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
Related
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
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
I have a problem concerning log4j2 which does not load the log4j2.xml configuration file in a project.
The project is bundled into an uber jar file. When running the application using java -jar jarfile.jar the application starts but log4j prints the following error to the console:
ERROR StatusLogger No log4j2 configuration file found. Using default
configuration: logging only errors to the console.
I checked the jar and it definitely contains a log4j2.xml file in the root location.
Because I could not figure out why this does not work I debugged to log4j2 bootstrap code. I found out that log4j never tries to read the log4j2.xml. This should happen in org.apache.logging.log4j.core.config.ConfigurationFactory.Factory#getConfiguration.
Unfortunately the list of factories used in this method is empty thus the method always returns null.
Any ideas on this?
If you want to check this clone https://github.com/cryptomator/cryptomator, cd to the main directory in the cloned repo and run mvn clean install -DskipTests -P uber-jar afterwards you will find the jar file in question under main/uber-jar/target.
I suspect this is the same issue as Log4j2 configuration not found when running standalone application builded by shade plugin since it sounds like you are building an uber jar.
I have a similar issue, but not using shade.
I have a jar file, with dependencies in /lib
I have a log4j2.xml file in the same location as the main jar file.
I can (obviously) run the jar file by calling:
java -Dlog4j2.configurationFile=.\log4j2.xml -jar myjar.jar
However, in Windows, it's possible simply to double-click the jar file to load it. Everything loads and works, except that it doesn't find the log4j2.xml file - so no logfile is written.
What I would like to be able to do is have a simple jar file I can hand to someone and have it run on their machine, with the ability to configure logging in the event they run into issues.
EDIT:
To do that, you need to amend your code thus:
public class MyClass
{
static
{
System.setProperty("log4j.configurationFile", "log4j2.xml");
}
private final static Logger LOG = LogManager.getLogger();
//OTHER STUFF HERE
}
Thanks to Load Log4j2 configuration file programmatically for the answer.
I'm trying to launch:
java -jar selenium-server-standalone-2.14.0.jar -role hub
from my Command Prompt but output was as below:
C:\Program Files (x86)>java -jar selenium-server-standalone-2.14.0.jar -role hub
Unable to access jarfile selenium-server-standalone-2.14.0.jar
C:\Program Files (x86) is where the jar file is located.
I've put C:\Program Files (x86) in my PATH and CLASSPATH and it still won't work.
Your filename must be wrong. Check whether you have a file named -selenium-server-standalone-2.14.0.jar. Chances are you won't be. :)
I encountered the same issue.
The solution is that the naming convention matters.
if you have a selenium server standalone.jar file, you can rename it first
to make it look simple(example abc.jar).
1) If the jar file in your system is encountered with the .jar extension,
then after renaming, give the .jar extension.(eg abc.jar)
2) If the jar file in your system is not having the .jar extension,
then after renaming, dont provide the .jar extension(eg abc)
3)Start the hub now:
java -jar abc.jar -role hub
Regards,
Nikhil Kanojia
Unable to access the jarfile is considered as Common Error.
This error can occur when starting up either a hub or node. This means Java cannot find the selenium-server jar file. Either run the command from the directory where the selenium-server-XXXX.jar file is stored, or specify an explicit path to the jar.
Go for details here
1.goto root mode
2. install $apt install mlocate
3. locate your jar file
4. check the correct jar file name and again try to open it with specific command
5 java -jar ./selenium-server-standalone-2.14.0.jar
I had the same issue with ubuntu. Try following steps..
Go to the directory where the jar file located.
Then execute the .jar file in the directory using,
java -jar ./selenium-server-standalone-2.14.0.jar
Go to desired location in command prompt and enter the below command.
java -jar ./selenium-server-standalone-3.141.59.jar
That means if you save the .jar file in "C:\Eclipse\jar" location then the command should be:
C:\Eclipse\jar>java -jar ./selenium-server-standalone-3.141.59.jar
I have in my main config something like:
grails.config.locations = ["file:grails-app/config/Jawr.groovy"].
When running the application with grails run-app, everything is OK.
But, on deployment (creating the war archive) this does not work anymore, as the file "Jawr.groovy" is not kept anymore on the filesystem (it should be only in the war).
Do you have a solution for that? Hw do you include external files into the grails main configuration file?
Thanks.
Okay, a few things here.
First, because you don't have a leading slash on your config path, that is a path relative to who knows where. I played with this in Tomcat, and that path ends up being relative to the working directory you were in when starting the Tomcat server. If you start Tomcat, shut it down, change directories, then start it again, you are going to get two different config paths.
Second, the grails-app directory only exists within the source tree of your Grails project. The structure of an unpacked WAR file is more like the web-app folder of your Grails source tree, with folders like WEB-INF, META-INF, js, images, etc.
Third, you probably want to avoid putting your externalized config file inside the folder of your webapp. The next time you deploy your app, that configuration is going to get wiped away with the old version of the app. One of the points of the externalized config is so that you can redeploy without having to reconfigure.
A simple, but less than ideal, solution would be to use a static, fully qualified path, like /etc/yourApp/conf.groovy, then put that in the documentation. There is also a plug-in that handles this.
http://www.grails.org/plugin/external-config
I haven't used it, but the description makes it sound like it does sensible things.
see this: https://stackoverflow.com/questions/6341117/is-it-possible-that-grails-xxconfig-groovy-as-a-script-no-compile
Then I put it into /shared, and
modify:
//Config.groovy
grails.config.locations =
["file:shared/TZLibConfig.groovy"]
//BuildConfig.groovy
grails.war.resources = { stagingDir, args ->
copy(todir: "${stagingDir}/WEB-INF/shared"){
fileset(dir:"shared",includes:"**")
}
}
In my work, our team often use a system properties to save the path to the config file (often in home folder of the user running the app - for privilege sake). Then we manually copy the config file into that path
To identify that it's the production environment, we use the following code in Config.groovy:
if (System.properties["${appName}.config.location"]) {
grails.config.locations = ["file:" + System.properties["${appName}.config.location"]]
}
This article suggests allowing the user to specify the location of the config file as an environment variable or as a java property --- meaning you can simply specify it with -D on the command-line. This can be used in addition to all the other methods.