How to change logging level with the MVC endpoint – new Actuator Loggers Endpoint | Spring Boot

Spring Boot 1.5.1 provides some new features. Among them, we have a new actuator loggers endpoint, which helps us to view and change logging level of application with the MVC endpoint.

This tutorial shows you the way to do this with http GET/POST request.

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE

II. Overview
1. Dependency

A Spring Boot Starter Dependency for Actuator should be present in pom.xml:

		
			org.springframework.boot
			spring-boot-starter-actuator
		
2. Loggers Endpoint

Actuator endpoints help us to monitor and interact with our application. Spring Boot includes a number of built-in endpoints including Loggers Endpoint. It shows current active level for logger and how we config. In JSON format:

{
     "configuredLevel":"...",
     "effectiveLevel":"..."
}

‘ could be: TRACE, DEBUG, INFO, WARN, ERROR

3. Customize endpoint

We can change how this endpoint exposes by add some code to application.properties, in this example:

management.port=8081
management.security.enabled=false
management.address=127.0.0.1
management.context-path=/actuator

endpoints.loggers.enabled=true

management.port is used to change the HTTP port.
– Because the management port is often protected by a firewall, so we need to disable management security like this: management.security.enabled=false
management.address=127.0.0.1 indicates that we only allow to access by this IP address, in this case, it is localhost.
management.context-path can be used to set a prefix for management endpoint.
endpoints.loggers.enabled (true/false) is to enable/disable endpoint by name, in the example, we enable loggers endpoint.

So the url to be called should be: http://localhost:8081/actuator/loggers/[package]

4. View Logging Level

Just make a HTTP GET request to /loggers/[our qualified name of package]. The JSON String result will be:

{
     "configuredLevel":"[Logging Level to be set]",
     "effectiveLevel":"[current active Logging Level]"
}
5. Change Logging Level

We can issue a HTTP POST request to /loggers/[our qualified name of package] with the following JSON:

{
     "configuredLevel": "[Logging Level]"
}
III. Practice
1. Add dependencies

Open pom.xml, add those dependencies:

		
			org.springframework.boot
			spring-boot-starter-web
		

		
			com.google.code.gson
			gson
		

		
			commons-io
			commons-io
			2.4
		

		
			org.springframework.boot
			spring-boot-starter-actuator
		
2. Add configuration for Endpoint

Open application.properties and add:

management.port=8081
management.security.enabled=false
management.address=127.0.0.1
management.context-path=/actuator

endpoints.loggers.enabled=true
3. Create LogInfo class

Under package com.javasampleapproach.loggerendpoint, create package logger and add LogInfo class, this POJO class is used for creating correlative JsonString.

package com.javasampleapproach.loggerendpoint.logger;

public class LogInfo {

	private String configuredLevel;

	public LogInfo() {
	}

	public LogInfo(String configuredLevel) {
		this.configuredLevel = configuredLevel;
	}

	public String getConfiguredLevel() {
		return configuredLevel;
	}

	public void setConfiguredLevel(String configuredLevel) {
		this.configuredLevel = configuredLevel;
	}

}
4. Create code for testing

In your main class which is annotated by @SpringBootApplication, use the code below:

package com.javasampleapproach.loggerendpoint;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.google.gson.Gson;
import com.javasampleapproach.loggerendpoint.logger.LogInfo;

@SpringBootApplication
public class SpringLoggerEndpointApplication {

	static Logger logger = LoggerFactory.getLogger(SpringLoggerEndpointApplication.class);

	public static void main(String[] args) throws IOException {
		SpringApplication.run(SpringLoggerEndpointApplication.class, args);

		viewLoggerLevel();
		
		System.out.println("=== Set Logger Level TRACE ===");
		setLoggerLevel("TRACE");
		viewLoggerLevel();
		printLog();

		System.out.println("=== Change Logger Level to INFO ===");
		setLoggerLevel("INFO");
		viewLoggerLevel();
		printLog();

		System.out.println("=== Change Logger Level to WARN ===");
		setLoggerLevel("WARN");
		viewLoggerLevel();
		printLog();
		
	}

	private static void printLog() {
		logger.trace("Java Sample Approach");
		logger.debug("Java Sample Approach");
		logger.info("Java Sample Approach");
		logger.warn("Java Sample Approach");
		logger.error("Java Sample Approach");
	}
	
	private static void viewLoggerLevel() throws IOException {
		String query = "http://localhost:8081/actuator/loggers/com.javasampleapproach.loggerendpoint";

		URL url = new URL(query);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setDoInput(true);
		connection.setRequestMethod("GET");
		
		InputStream in = new BufferedInputStream(connection.getInputStream());
        String result = IOUtils.toString(in, "UTF-8");
        in.close();
        
        System.out.println("current Logging Level: " + result);
        
		int code = connection.getResponseCode();
		System.out.println("HTTP response code: " + code);
		
        connection.disconnect();
	}

	private static void setLoggerLevel(String configuredLevel) throws IOException {
		LogInfo logInfo = new LogInfo(configuredLevel);

		Gson gson = new Gson();
		String jsonString = gson.toJson(logInfo);

		System.out.println("JSON to send: " + jsonString);

		String query = "http://localhost:8081/actuator/loggers/com.javasampleapproach.loggerendpoint";

		URL url = new URL(query);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setConnectTimeout(5000);
		connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
		connection.setDoOutput(true);
		connection.setRequestMethod("POST");

		OutputStream os = connection.getOutputStream();
		os.write(jsonString.getBytes("UTF-8"));
		os.close();

		int code = connection.getResponseCode();
		System.out.println("HTTP response code: " + code);

		connection.disconnect();
	}
}
5. Check results

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results in Console Window:

IV. Source Code

SpringLoggerEndpoint



By grokonez | February 16, 2017.

Last updated on June 4, 2017.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*