I have apllication.properties files in java web application which contains these properties.
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:19095/test
spring.datasource.username=test
spring.datasource.password=test123
But I want to override these properties (except driverClassName) while starting tomcat server.
I'm trying to set these variables from command line, but it's not working.
tomcat version : 7.0.63
Why don't you use a property placeholder with config directory specified by a system parameter:
<context:property-placeholder location="file:${configLocation}/database.properties:defaultDatabase.properties" />
Then start the tomcat with:
-DconfigLocation=/opt/config
see http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html and also Loading property file from system properties with default path in Spring context
Related
I am trying to run a project on a UAT (Linux) server, but there is a problem, I need the logs to be written on the test server in a different path.
The project resources contain logback.xml, which specifies where to write logs:
<property name = "logging.path" value = "/logs"/>
<property name = "logging.file" value = "app-logger"/>
I cannot change this file, because in this case everything is correct, but for UAT I created another logback.xml and put it not in the resources but in another directory, and when I run the jar file I want to specify that I need to take this one logback.xml
On UAT I execute this command
java -Xmx512M -jar /home/user/app/program/program-0.1.jar --logging.config = app/program/config/logback.xml
I got this error:
ERROR in ch.qos.logback.core.rolling.RollingFileAppender [RollingFile] - openFile (/logs/app-logger.log,true) call failed. java.io.FileNotFoundException: /logs/app-logger.log (No such file or directory)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration (LogbackLoggingSystem.java:169)
So the second file logback.xml which I created is ignored and the one that lies in the resources is used. What am I doing wrong? How to put another logback.xml into Spring?
You can add in your required log directory to your environment specific properties file,
should be something like application.uat.properties. So you would add in something like LOG_DIR=path/to/directory
Once that is done add in '<property resource="application.${env}.properties"/>' to your logback.xml
And finally in your file appender , add '<file>${LOG_DIR}/serive.log</file>'
Is it possible to set the contents of Spring's bootstrap.properties at execution time?
I have a client application for Spring's cloud config which holds a line to point to the config server:
spring.cloud.config.url = http://localhost:8888
And I would like to set this config. I tried setting it like application.properties can be set:
java -jar ./config-client.jar --spring.cloud.config.url=http://localhost:8888
But it didn't work. So my question is, how can I set this property at execution time?
It seems to be caused by wrong properties name. url should be uri.
Try to change your property in bootstrap.properties like below.
spring.cloud.config.uri = http://localhost:8888
And then try again like below.
java -jar ./config-client.jar --spring.cloud.config.uri=<something else>
http://localhost:8888 is the default value of spring.cloud.config.uri. That's probably the reason why you didn't recognize the wrong property name.
I'm using java system logging in tomcat 7, but no logging statements get written to the log. I've added this file to my WEB-INF/classes. The log file "new-xyz-test" gets created (so I have at least some of the config right) but its empty - no log statements get printed to it.
handlers=java.util.logging.ConsoleHandler, org.apache.juli.FileHandler
org.apache.juli.FileHandler.level=ALL
org.apache.juli.FileHandler.directory=${catalina.base}/logs
org.apache.juli.FileHandler.prefix=new-xyz-test-
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
com.xyz.level=ALL
com.xyz.handlers=org.apache.juli.FileHandler
To configure JULI in the web applications you need have a logging.properties file in the WEB-INF/classes directory. If you use the default handlers, you may lose messages. You need to specify a prefix for the handler in your file.
handlers=1FILE.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
.handlers=java.util.logging.ConsoleHandler
1FILE.org.apache.juli.FileHandler.level=FINEST
1FILE.org.apache.juli.FileHandler.directory=/app-logs
1FILE.org.apache.juli.FileHandler.prefix=file-1
java.util.logging.ConsoleHandler.level=FINE
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
com.xyz.level=INFO
com.xyz.handlers=1FILE.org.apache.juli.FileHandler
com.abc.level=INFO
com.abc.handlers=java.util.logging.ConsoleHandler
A
handler prefix (e.g. 1FILE.) starts with a number, then has an arbitrary string, and ends with a period (.).
See more in Logging in Tomcat
Arguments in the JVM
If you are not running the Tomcat from the startup.sh or startup.bat, you need to specify:
The location of the general logging.properties for Tomcat (in the conf directory of Tomcat)
The manager org.apache.juli.ClassLoaderLogManager. This is important because allows you to configure
for each web application different loggin options. By default, a JVM process can only have a single configuration file.) ,
Similar to the next (I'm using eclipse):
-Djava.util.logging.config.file="C:\Users\Paul\workspaces\utils\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\logging.properties" -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
By default, java.util.logging read the file that is included in the JDK or JRE, e.g.:
"C:\Software\jdk1.7.0_17\jre\lib\logging.properties"
Setting Tomcat Heap Size (JVM Heap) in Eclipse, for how to add arguments in the VM
are you sure that you write to the correct logger , i.e. Logger.getLogger("com.xyz")?
I think that you may got wrong when you wrote in logging.properties:com.xyz.level=ALL com.xyz.handlers=org.apache.juli.FileHandler in the case that you actually write to the logger Logger.getLogger(com.xyz.YourClass.class), that because in the logging properties file you should write the logger name which is in your case com.xyz.YourClass
I have an application that listens on a specific port to do its task.
This application can run on multiple instances by specifying different
ports in the argument.
MyApp-1211.bat contains
java MyApp 1211
MyApp-1311.bat contains
java MyApp 1311
MyApp-1411.bat contains
java MyApp 1411
This application logs to a file. The problem is all three instances log
into a single file, myApp.log. Is there a way to tell log4j to use
different log files? like:
myApp-port1211.log
myApp-port1311.log
myApp-port1411.log
Of course you can. One way would be to create multiple configuration files (log4j.xml / log4j.properties) - one per port respectively process. Upon loading the configuration file you could select the correct one based on the current port number:
PropertyConfigurator.configure("log4j-" + port + ".properties");
Create the config files accordingly: log4j-1211.properties, log4j-1311.properties, ...
The alternative would be to configure the file logging at runtime via Java code:
String logFilename = "./myApp-port" + port + ".log";
Layout layout = new PatternLayout("%d{ISO8601} %-5p [%t] %c{1}: %m%n");
FileAppender fileAppender = new FileAppender(layout, logFilename, false);
fileAppender.setThreshold(Level.DEBUG);
Logger.getRootLogger().addAppender(fileAppender);
You can refer to system properties in log4j.xml as following:
<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
<!-- The active file to log to -->
<param name="file" value="mylog${MY_PARAM}.log" />
</appender>
Now you just have to insert your parameter into system properties either programmatically System.setProperty("MY_PARAM", args[0]) into your main() method or when you are running java:
java -DMY_PARAM=1234 MyApp 1234
Obiously you can avoid dupplication if you are running you application from bat or shell script like:
java -DMY_PARAM=%1 MyApp %1
Please see the following references for details:
http://wiki.apache.org/logging-log4j/Log4jXmlFormat
Using system environment variables in log4j xml configuration
If you go for loading the property configuration file yourself, as suggested by other answers, don't forget to disable the log4j default initialization by adding the env. variable definition to your program's command line:
-Dlog4j.defaultInitOverride=true
I'm struggling to set connect a Java program to MySQL using JPA/Hibernate.
I'm currently getting the following error when I try to call createEntityManagerFactory():
[main] ERROR org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource: java:jdbc/myDataDS
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
Googling seems to indicate that I need a jndi.properties file in META-INF in my classpath, but I can't seem to find any information about what that file should contain in my case.
Edit: I'm running this stand-alone, for the time being.
A jndi.properties file should be at the root of the classpath and typically contains the URL of the JNDI server and the initial context factory to use. For example, with JBoss:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
But, when using Hibernate, you should actually declare these properties in the hibernate.cfg.xml. For example, with WebLogic:
<property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
<property name="jndi.url">t3://127.0.0.1:7001</property>