2

I'm trying to show the contents of a logger file in the browser screen , so that when the application is running in production in external server, i don't need to login every time into the server to fetch the logs. I'm trying to achieve this using Spring Boot Actuator. I have configured the log file path and log info level in my properties file, and logs are being written in that file, but how to stream the contents of the file in browser window. below is my properties file contents

  management.security.enabled=false
  endpoints.env.enabled=false
  endpoints.configprops.enabled=false
  endpoints.autoconfig.enabled=false
  endpoints.beans.enabled=false
  endpoints.dump.enabled=true
  endpoints.heapdump.enabled=true
  logging.level.root=info
  logging.file=target/app.log

Thanks for the help in advance !!!!

Pradeep Anand
  • 145
  • 3
  • 14

3 Answers3

1

You can use Spring Boot Admin: https://github.com/codecentric/spring-boot-admin

The logs appear like this:

enter image description here

You can use https://start.spring.io/ to include de Admin Client and Server in your project. Check the tutorial here: http://codecentric.github.io/spring-boot-admin/current/#getting-started

voliveira89
  • 1,134
  • 2
  • 9
  • 22
1

By default, the following Spring Boot Actuator endpoints are enabled (JMX/WEB):

https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-endpoints-exposing-endpoints

To enable specific endpoints write the following in Spring Boot application.properties file:

management.endpoints.web.exposure.include = info, health, logfile

or to disable write:

management.endpoints.web.exposure.exclude = env,beans
Šime Tokić
  • 700
  • 1
  • 9
  • 22
0

Logs in the browser:

One way to expose the logs from a Spring Boot application in the browser is by using the built-in Actuator endpoints.

Actuator is a tool that provides production-ready features to help you monitor and manage your Spring Boot application. It includes a variety of endpoints, including one for viewing the application logs.

To enable the Actuator endpoints, you need to add the spring-boot-starter-actuator dependency to your project's pom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Once the dependency is added, you can access the log endpoint at http://localhost:8080/actuator/logfile.

To expose the logs in the browser, you can create a custom endpoint that reads the log file and returns its contents. Here's an example:

@RestController
public class LogController {

    @GetMapping("/logs")
    public ResponseEntity<String> getLogs() throws IOException {
        Path logFile = Paths.get(System.getProperty("logging.file.name"));
        String logs = new String(Files.readAllBytes(logFile));
        return ResponseEntity.ok(logs);
    }
}

Make sure the application is configured to write logs into a file, by adding the following to the application.properties or application.yml file:

logging.file.name=mylog.log
logging.level.root=INFO

This code creates a new endpoint at http://localhost:8080/logs that returns the contents of the log file as a string.

Note that this approach may not be suitable for production environments as it exposes potentially sensitive information. It is recommended to restrict access to the log endpoint using appropriate security measures.

Nacho
  • 119
  • 1
  • 3