Development Tools

Dependency pertama yang kita tambahkan adalah Spring Boot Development Tools dan Spring Boot Web. Spring Boot Development Tools membuat proses development menjadi lebih simple.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<scope>runtime</scope>
</dependency>

Development tools akan merestart aplikasi secara otomatis ketika kita menyimpan modifikasi yang kita lakukan. Kamu bisa mencobanya dengan menambahkan satu bari comment di main class:

package lateral.spring.boot.springreact;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringReactApplication {

	public static void main(String[] args) {
		//Tambahkan comment disini, aplikasi akan restart ketika perubahan disimpan
		SpringApplication.run(SpringReactApplication.class, args);
	}

}

Ketika disimpan, di console akan muncul log berikut:

2019-11-14 03:22:42.291  INFO 9156 --- [  restartedMain] l.s.b.s.SpringReactApplication           : Starting SpringReactApplication on popos with PID 9156 (/home/data/Projects/lateral/spring-react/spring-react/target/classes started by dianalamanda in /home/data/Projects/lateral/spring-react/spring-react)
2019-11-14 03:22:42.291  INFO 9156 --- [  restartedMain] l.s.b.s.SpringReactApplication           : No active profile set, falling back to default profiles: default
2019-11-14 03:22:42.500  WARN 9156 --- [  restartedMain] org.apache.tomcat.util.modeler.Registry  : The MBean registry cannot be disabled because it has already been initialised
2019-11-14 03:22:42.508  INFO 9156 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-11-14 03:22:42.509  INFO 9156 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-11-14 03:22:42.509  INFO 9156 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-14 03:22:42.515  INFO 9156 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-11-14 03:22:42.515  INFO 9156 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 221 ms
2019-11-14 03:22:42.566  INFO 9156 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-14 03:22:42.609  INFO 9156 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2019-11-14 03:22:42.640  INFO 9156 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-11-14 03:22:42.641  INFO 9156 --- [  restartedMain] l.s.b.s.SpringReactApplication           : Started SpringReactApplication in 0.368 seconds (JVM running for 29248.122)
2019-11-14 03:22:42.651  INFO 9156 --- [  restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged

Log dan Problem Solving

Spring Boot starter secara default menyediakan logging. Berikut contoh cara menggunakannya:

package lateral.spring.boot.springreact;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
 public class SpringReactApplication {
private static final Logger logger = LoggerFactory.getLogger(SpringReactApplication.class);

public static void main(String[] args) {
    SpringApplication.run(SpringReactApplication.class, args);
    logger.info("Percobaan menggunakan logger");
}
}

Kita lihat di console akan muncul log berikut:

2019-11-14 03:28:22.920  INFO 9156 --- [  restartedMain] l.s.b.s.SpringReactApplication           : Percobaan menggunakan logger

Ada tujuh level logging: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, dan OFF. Kita dapat mengkonfigurasinya secara global pada file application.properties di folder resource:

Tambahkan baris berikut dalam file application.properties:

logging.level.root=INFO

Perhatikan, di console, kita tidak lagi melihat log TRACE dan DEBUG lagi.


LEAVE A REPLY

Please enter your comment!
Please enter your name here