How to create Spring Boot logs in standalone project deployed on windows? - spring-boot

I am using following to execute spring and creating logs
java -jar AhMachine-0.0.1-SNAPSHOT.jar > c:\log.txt
but this creates a single log for lifetime.
Is there any way to split this log via commandline or spring boot?

java -jar AhMachine-0.0.1-SNAPSHOT.jar > c:\log.txt will pipe the console output to a file
java -jar -Dlogging-file=c:\log.txt AhMachine-0.0.1-SNAPSHOT.jar will output the internal configurated file appenders to a file
java -jar -Dlogging-file=c:\log.txt -Dlogging.file.max-size=4KB AhMachine-0.0.1-SNAPSHOT.jar will output the internal configurated file appenders to a file will create log.txt files of max 4KB in size, a .gz file is created of the older files
For more complex configuration I would advise using a config-file like spring-logback.xml, you can easily specify the location of that file via the CLI -Dlogging.config=c:/log/config/spring-logback.xml

Not via command line,
this can all be done inside the jar via rolling file appender:
https://www.baeldung.com/java-logging-rolling-file-appenders
If you cannot access the Jar no matter what see this comment:
https://stackoverflow.com/a/53395247/3942132
I haven't tested it but there should be a rotatelogs.exe process for windows as well.

Related

Spring Boot - Externalize Database Settings

I have a JAVA project developed with Spring Boot.
The database settings are in the default resources/application.properties file.
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:#//172.********:1521/BDHML
spring.datasource.username=********
spring.datasource.password=********
The application will run through the command:
java -jar **CONFIGURAÇÃO** app-cosolidar.jar
I need to put the database settings in a .properties file external to the project.
How can I do this?
What should the .properties file look like?
Should I change any .java files?
What setting should I put in java -jar?
Regards, Diego
You can provide command line arguments while running jar.
java -jar app.jar --spring.config.location=file://<path>/application.properties
You can also pass a folder location where the application will search for the files.
java -jar app.jar --spring.config.name=application,jdbc --spring.config.location=file://<path to config folder>
Refer this link for more understanding. https://www.baeldung.com/spring-properties-file-outside-jar
You would be able to launch your spring boot application with the external properties file path as follows:
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config

how to set profile-specific applicaiton.properties(spring boot) outside war(jboss)?

I have spring boot application that I am running in jboss(instead of tomcat).
I am using spring profile for loading environment specific application.properties.
Profile specific application{env}.properties is working fine when placed under "src/main/resources/" however, it is not working when placed externally.
I tried setting property in jboss standalone.xml but application fails to start in jboss.
<property name="spring.config.location" value="C:/Dev/config/rt" />
Please suggest how to load the environment specific application.properties files that are not placed inside the war file.
I was expecting spring to pick the profile specific file from the folder but looks like that's not the case.
It appears that spring.config.location needs to have the absolute file location instead of folder location. so, updated spring.config.location in standalone.xml and it worked :
<property name="spring.config.location" value="C:/Dev/config/rt/application-dev.properties" />
You can provide externalized configuration file using below command when you are initially starting the application,
java -jar <your-jar-name.jar> --spring.config.location=<path><external property>
example :
java -jar mySample.jar --spring.config.location=./application-external.properties
read more
Generally it was picked very easily when it's placed under the 'src/main/resources' folder. suppose you have to different files for profiles like - application-dev.properties and application-prod.properties, you need to set only the current working profile in the application.properties like
spring.profiles.active=dev
and it will be picked easily. If it's doesn't you need to create a workaround by creating a new bat or sh file like run.bat and run.sh in the bin folder of the jboss and pass the file location like
--spring.config.location=
The complete command to be added in the bat/sh file will be
java -jar appName.jar --spring.config.name=application-dev --spring.config.location=c:/Dev/application-dev.properties

Logback external configuration on Tomcat

I have Spring Boot application deployed as WAR file on standalone Apache Tomcat.
In order to configure Logback on Windows machine I've created setenv.bat file with the following content:
set logging.config=<absolute path to logback.xml>
I was trying the same for Tomcat deployed on Mac, but the following setenv.sh doesn't work:
logging.config=<absolute path to logback.xml>
I've also tried set logging.config variable using export command:
export logging.config=<absolute path to logback.xml>
but result is the same - log files aren't created.
Any ideas what is wrong with my configuration?
I hope you already have found your answer somewhere. But what I have done is that in conf/catalina.properties, point to the logback config file as follows:
logging.config=${catalina.base}/conf/logback-spring.xml
I don't do anything in setenv.bat.

Spring boot starts even if start with nonexistent profile

I have spring boot application - server.jar with next structure:
-resources
-application.yaml
After the build, I create a folder with a name source. And put my jar to this folder. Also, I create start .bat file
-source
-server.jar
-start.bat
In start.but the file I write next:
java -Dspring.profiles.active="foofoofoo" -jar server.jar
pause
When I run start.bat my server starts with log:
The following profiles are active: foofoofoo
And use properties from application.yaml. I have not profile with name foofoofoo and I have not apllication-foofoofoo.yaml. Why? Why spring writes that it loads foofoofoo profile, load application.yaml and work?
It must crash because I start the application with the nonexistent profile!
Instead, I see a running application with another property! How can I process this?
When you start the application application.yml file in your resources directory is getting included anyway.
If you also have application-yourProfileName.yml in resources directory and add
--spring.profile.active=yourProfileName parameter,
then both property files are getting included, and NOTE that in this case application-yourProfileName.yml override the same properties in application.yml.
The profile can be used in more ways than just application's properties. It does not matter if you do not have application-your-profile.{properties, yml}. Loading profile does not mean loading properties file.

Spring Boot : log4j2.xml next to app.jar not read in production environment

I'm new to Spring Boot.
Under 'resources' there are 2 files :
-- resources
-- application.properties
-- log4j2.xml
In development environment everything works fine.
In production environment, I copy both files and put them next to the app.jar :
-- app_folder
-- my-app.jar
-- application.properties
-- log4j2.xml
When I start the my-app.jar, :
application.properties is read from app_folder, as intended
log4j2.xml is read from 'resources', the one under app_folder is ignored
Shouldn't it work this way out of the box ? What am I doing wrong ?
All I had to do was putting a file named
name-of-my-spring-boot-jar-file.conf
in the same directory as the jar file itself.
Content of conf file :
JAVA_OPTS="-Dlog4j.configurationFile=/home/<user>/log4j2.xml"
Why do you assume it works that way?
It is true that according to its documentation Spring Boot will detect application.properties correctly if it is placed in the same directory as the jar file.
That being said log4j2.xml is not read by Spring Boot but by Log4J2 Framework and according to its documentation that framework oly looks for files on the classpath.
If the file is elsewhere you need to specify the path like this:
java -Dlog4j.configurationFile=path/to/log4j2.xml -jar my-app.jar
Edit:
my-app.jar is an executable, so I don't invoke the 'java' command when starting my-app.
Yes you do. Even if you're invoking it from a GUI (eg. double clicking in Explorer in Windows) it still runs java -jar my-app.jar under the hood.
Isn't the same folder the app.jar resides in considered classpath ?
Again, why would you assume that it is?
In the Java documentation (here for Java 8) in section Folders and Archive Files it clearly says that if the classes are stored in a jar, then the classpath includes only stuff from the jar (although in case of Spring Boot due to the custom classloader it also includes jars embedded in the jar, whch would normally not be the case - see Executable Jar Format).
You really should read the documentation (or at least relevant parts of it) before attempting to use any framework/library/programming language you have not used before - it will save you a lot of time in the long run.

Resources