port number of server in log file name - log4j2 - spring-boot

Is there any way to configure log-file to be appended with the port number of application-server.
Example: if my spring boot web service is running on 5001 port, then the log file name must be appended with the port number.
Port number is not configured in application.yml file.
Also, I tried using lookup ${spring:server.port} from application.yml file, but it does not seem to work.
I found a post on stack overflow where it says, log4j2 lookup works with Spring cloud config. I don't want to use cloud config.
Another post says to use higher version of log4j2 ie at least 2.13. But this also didn't work.

You can set your port no in application.properties file and after that u can read that using Environment Object.
In application.properties :- server.port= 5001
In Log4j implementation class :-
#Autowired
Environment env;
String keyVal = env.getProperty("server.port");
And append this port to your Log4j file name.

Related

Want to access Spring Boot Service without port in URL

For example
https://192.168.1.136:8081/ works fine
But if I try to access
https://192.168.1.136/ Error - The site can't be reached.
What are the ways to access it without a port in URL?
Assign port 80 instead of 8081 will work.
Update application.properties in /src/main/resources/ with
server.port=80
Alternatively you may also set server.port as system property using command line option to jvm -Dserver.port=80

Changing Default Ports in Moqui

Moqui Framework Version : 2.1.3
The Framework runs on the default port 8080 just fine, i would like to change the default ports and i did read https://www.moqui.org/m/docs/framework/Run+and+Deploy#a2.RuntimeDirectoryandMoquiConfigurationXMLFile
which states > "Each of these can be system environment variables (with underscores) or Java properties (with underscores or dots) using the -D command-line argument.
i did find the webapp_ variables are referenced in MoquiDefaultConf.xml as mentioned in the above material and tried using the below start command >
$sudo nohup java -Dwebapp_http_host=localhost -Dwebapp_http_port=9080 -Dwebapp_https_port=9443 -jar moqui.war conf=conf/MoquiDevConf.xml &
However the above command does not seem to change the port, Moqui is still running on default port 8080, What could i be missing?
I also tried the solution to update the webapp tag in MoquiDevConf.xml as mentioned in Running Moqui on Tomcat over SSL (setting http-port and htts-port) - return code 302 with no joy,
Appreciate any pointers, i'm really stuck
The environment variables or Java properties you mention are for setting the ports to use when building URLs. These are the external ports used for accessing your server and if a load balancer or reverse proxy is used may be different from the ports the servlet container is running on. For more information see:
https://moqui.org/m/docs/framework/Run+and+Deploy#EnvironmentVariables
If you are running Moqui with the embedded Jetty server you can specify the port it listens on using the port argument as described in the Executable WAR File section of the Run and Deploy document:
https://moqui.org/m/docs/framework/Run+and+Deploy#a3.ExecutableWARFile
Note that the embedded Jetty server can be used in production but it does not support https and is meant to be used behind a reverse proxy like nginx or Apache httpd that forwards requests to the embedded Jetty server.
If you deploy the WAR file by dropping it in a Servlet Container (ie as an actual WAR file, not treating it as an executable JAR file) then the port configuration would be done with the Servlet Container (Tomcat, Jetty, etc).

Where to specify a specific port number in spring boot service file in systemd?

Need to know where can I specify the spring boot port aa when I check my service status I get this:
"Serverkt.log started at server-name with process id"
But I don't get the port number.
Also wanted to know where I can find the spring logs in the server
spring boot has its configuration file. I guess that you have it included in your jar/war file. So basically unzip that file and look inside it (try to search for application.properties or application.yaml|yml).
property server.port defines the port on which application is running. It defaults to 8080.
If you are using spring-boot 2 then with property logging.path you can change the path where output file will be placed. However I don't know if this works when you have logback/log4j/... configuration.
if you run your application you can override those properties specified in appplication.properties|yaml by providing command line properties. For example you can change the port with command java -jar your-boot.jar --server.port=9090
Since you're starting spring boot from a service file, you can set the port using command line arguments e.g.port 8083 would look like this:
ExecStart=/usr/bin/java -jar /opt/corda/spring.jar --server.port=8083

Spring Boot and Logback logging.config file with spring properties placeholders

I've a set of applications installed on a server that I want to start sending logs thru syslog to a remote Logstash server. For that, I've created a single external Logback configuration file containing a SyslogAppender pointing to the desired remote server.
As multiple applications will log to the same server, I've changed the log pattern to be the following:
<suffixPattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [${server_instance}] [${application_name}] %p ${PID:- } --- [%15.15t] %logger : %m%n</suffixPattern>
Where server_instance and application_name are command-line option arguments provided at startup.
Now I just simply set the logging.config property of all my apps pointing to the same Logback config file, and all the applications start sending the logs using the specified pattern to the desired server. That part works like a charm.
But the only problem I've is that Logback isn't able to determine the server_instance and the application_name properties and they appear as [server_instance_IS_NOT_DEFINED] and [application_name_IS_NOT_DEFINED] respectively.
Can I achieve this somehow using a single external configuration file?
Logback isn't able to see command-line arguments; those are seen by Spring alone.
Moved those command-line options arguments to system properties (i.e. "-Dserver_instance" and "-Dapplication_name" instead of "--server_instance" and "--application_name") and now everything's working as expected.

spring boot failed to start due to "The port may already be in use"

I tried to start my spring boot application on centos server with this command line.
SPRING_PROFILES_ACTIVE=prd java -jar ./build/libs/port_switcher-0.0.1-SNAPSHOT.jar
But I got an error message that the port is already in used.
then looked up process list which is used in the port number using netstat
but there is no process that uses the port number
and then I rerun my spring boot application without SPRING_PROFILES_ACTIVE variable. (it was an mistake that forget that variable)
at that time spring boot application run successfully.
I really want to know what problem is in my application properties
this command occur error
SPRING_PROFILES_ACTIVE=prd java -jar ./build/libs/port_switcher-0.0.1-SNAPSHOT.jar
this command works fine
java -jar ./build/libs/port_switcher-0.0.1-SNAPSHOT.jar
application-prd.properties
#Server
server.address=xxx.xxx.xxx.xxx
server.port=9090
#Devtools
spring.devtools.livereload.enabled=true
spring.devtools.restart.enabled=true
# Logging
logging.level.org.springframework.web=INFO
logging.level.com.kakao.portswitcher=INFO
logging.path=logs
server.tomcat.basedir=./
server.tomcat.accesslog.enabled=true
#server.tomcat.accesslog.file-date-format=.yyyy-MM-dd
server.tomcat.accesslog.rotate=true
server.tomcat.accesslog.suffix=.log
server.tomcat.accesslog.prefix=access
server.tomcat.accesslog.directory=logs
server.tomcat.accesslog.buffered=true
#Jackson
spring.jackson.property-naming-strategy=SNAKE_CASE
# Status check file
status.file=./port_switcher_status
Can you try this :
java -jar -Dspring.profiles.active=prd ./build/libs/port_switcher-0.0.1-SNAPSHOT.jar

Resources