Logging using spring boot sl4j - spring-boot

In my application properties i have written the below for logging
logging.level.com.intro.dmp=INFO
logging.level.org.springframework.web=ERROR
logging.level.com.intro.dmp=ERROR
logging.file=application.log
and my Application is below , but it is not creating any log file rather displaying in the console . What is that i am missing here , it is reading application properties
#SpringBootApplication(scanBasePackages = "com.intro")
#PropertySource("file:src/main/resources/application.properties")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
logger.debug("--Application Started--");
logger.error("Check the main Articles");
logger.info("Checking files ");
}
}

Two changes will resolve your issue:
In application.properties file, you need to provide quotes to the file name. i.e.:logging.file='application.log'
You can remove #PropertySource annotation. Spring Boot will, by default, look for application.properties file at src/main/resources/.

Related

How to use system property variable in log4j2.xml in spring boot using lombok log?

I am having a CommandLineRunner Spring Boot applicatio nwith Sytem property log_prefix.
I am currently using a log24j.xml for setting the log configurations. It is taking the filename etc from the <properites> tag in the log24j.xml. Is there a way to change the log fileName to log_prefix_LogFileName ?
My Application.class
#SpringBootApplication
public class Application implements CommandLineRunner {
static Logger log = Logger.getLogger(Application.class.getName());
public static void main(String[] args) {
System.setProperty("log_prefix",args[0]);// Tried hardcoding a string value
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.run(args);
}
I tried setting runID in Application.class and using ${log_prefix} in log42j.xml but not working.
Pom.xml log4j2 dependency.
<log4j2.version>2.15.0</log4j2.version>
I am using #Slf4j lombok annotaion over the class
import lombok.extern.slf4j.Slf4j;
#Slf4j
As Piotr P Karwaz pointed out my log config file had ${log_prefix} , to get system properties in spring boot log configuration log4j2.xml file we need to prefix sys: the property name in curly braces. ${sys:log_prefix} fixed the issue.
note this is specific to Spring boot, for normal java application you can refer this answer

location of Bootstrap.properties in Spring Cloud Config application

I was trying to develop Spring Cloud Config server with some properties. So on that site it was mentioned that replace the application.properties file into bootstrap.properties file.
Code is given as below,
#SpringBootApplication
#EnableConfigServer
public class ConfigserverApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
}
}
and properties file ,
server.port = 8888
spring.cloud.config.server.native.searchLocations=file:///C:/configprop/
SPRING_PROFILES_ACTIVE=native
So where exactly this bootstrap.properties file resides in ? Is that complete replacement for application.properties or both exists in config server?

Spring Boot : PropertySourcesPlaceholderConfigurer is not loading system properties

I have a Spring Boot application as follows:
#SpringBootApplication
#PropertySource(ignoreResourceNotFound=true,value={"classpath:application.properties","classpath:util-${spring.profiles.active}.properties"})
#ComponentScan("com.jmarts")
#EnableTransactionManagement
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
protected SpringApplicationBuilderconfigure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
I'm making use of spring profiles and based on active profile, a the correct environment specific file is loaded: utils-local.properties, utils-dev.properties, etc...
When profile is set through application.properties (spring), e.g. spring.profiles.active=local all works great, correct file (utils-local.properties)is loaded.
Providing profile through -D (gradle bootRun -Dspring.profiles.active=local) doesn't load profile. I was able to verify that the system properties is passed (print systemProperties)
I assume spring boot will register a PropertySourcesPlaceholderConfigurer if none is configured.
spring boot officially supports profile-specific properties using the naming convention application-{profile}.properties.
so you can remove "classpath:util-${spring.profiles.active}.properties" and add application-local.properties, application-dev.properties and so on in the classpath.

Loading applicationcontext.xml when using SpringApplication

Could anyone provide an example of a SpringApplication that loads an applicationContext.xml file?
I'm attempting to move my GWT RPC application to a RESTful web service by using a Spring's Example (Gradle based). I have an applicationContext.xml but I do not see how to get SpringApplication to load it. Loading manually via
ApplicationContext context = new ClassPathXmlApplicationContext(args);
results in an empty context. ...and even if that worked it would be separate from the one returned from
SpringApplication.run(Application.class, args);
Or is there a way to get external beans into the app context created by SpringApplication.run?
If you'd like to use file from your classpath, you can always do this:
#SpringBootApplication
#ImportResource("classpath:applicationContext.xml")
public class ExampleApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ExampleApplication.class, args);
}
}
Notice the classpath string in #ImportResource annotation.
You can use #ImportResource to import an XML configuration file into your Spring Boot application. For example:
#SpringBootApplication
#ImportResource("applicationContext.xml")
public class ExampleApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ExampleApplication.class, args);
}
}
The annotation does not have to be (on the class) that (has the main method) that (has this below call):
SpringApplication.run(Application.class, args);
(in your case, what I am saying is that #ImportResource does NOT have to be on your class)
public class ExampleApplication {}
.........
You can have a different class
#Configuration
#ImportResource({"classpath*:applicationContext.xml"})
public class XmlConfiguration {
}
or for clarity
#Configuration
#ImportResource({"classpath*:applicationContext.xml"})
public class MyWhateverClassToProveTheImportResourceAnnotationCanBeElsewhere {
}
The above is mentioned in this article
http://www.springboottutorial.com/spring-boot-java-xml-context-configuration
.........
BONUS:
And just in case you may have thought "SpringApplication.run" was a void method.....that is NOT the case.
You can also do this:
public static void main(String[] args) {
org.springframework.context.ConfigurableApplicationContext applicationContext = SpringApplication.run(ExampleApplication.class, args);
String[] beanNames = applicationContext.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String name : beanNames) {
System.out.println(name);
}
This will also subtly clue you in to all the many, many, many (did I mention "many"?)....dependencies that spring boot is bringing in. Depending to whom you speak, this is a good thing (somebody else did all the nice figuring out for me) or an evil thing (whoah, that's a lot of dependencies that I don't control).
hashtag:sometimesLookBehindTheCurtain
Thanks Andy, that made it very concise. However, my main problem turned out to be getting applicationContext.xml into the classpath.
Apparently, putting files into src/main/resources is required to get them into the classpath (by placing them into the jar). I was attempting to set CLASSPATH which was just ignored. In my example above, the load seemed to fail silently. Using #ImportResource caused it to fail verbosely (which helped me track down the real cause).

Spring Boot WS application cannot load external property

In my Spring-Boot Web Service application, I want to load a property named appName with value defined in application.properties.
#Endpoint
public class RasEndpoint {
private static final String NAMESPACE_URI = "http://www.mycompany.com/schema/ras/ras-request/V1";
#Value("${appName}")
private String appName;
#PayloadRoot(namespace = NAMESPACE_URI, localPart = "getProductRequest")
#ResponsePayload
public GetProductResponse getProduct(#RequestPayload GetProductRequest request) {
System.out.println("appName: " + appName);
GetProductResponse response = generateStubbedOkResponse();
return response;
}
application.properties has the following entry
appName=ras-otc
I get the application started via the main Application class as shown below
#Configuration
#ComponentScan
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
However, when I run the app, I get the following error
Caused by: java.lang.IllegalArgumentException: Could not resolve
placeholder 'appName' in string value "${appName}"
Do you guys know what I'm doing wrong?
Appreciate any help.
As Dave mentioned in the comment above, the properties file was not loaded into the classpath.
The properties file was located in /src/main/resources folder, which was added to source, under build path in Eclipse IDE, however had an exclusion rule applied which prevented the properties file from being loaded into the classpath. By removing the exclusion, I was able to load the properties correctly.

Resources