package de.rwth.idsg.bikeman.web.rest;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import com.codahale.metrics.annotation.Timed;
import de.rwth.idsg.bikeman.web.rest.dto.LoggerDTO;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Controller for view and managing Log Level at runtime.
 */
@RestController
@RequestMapping("/api")
public class LogsResource {

    @RequestMapping(value = "/logs",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public List<LoggerDTO> getList() {
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        return context.getLoggerList()
            .stream()
            .map(LoggerDTO::new)
            .collect(Collectors.toList());
        
    }

    @RequestMapping(value = "/logs",
            method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    @Timed
    public void changeLevel(@RequestBody LoggerDTO jsonLogger) {
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        context.getLogger(jsonLogger.getName()).setLevel(Level.valueOf(jsonLogger.getLevel()));
    }
}

@@ -193,30 +193,110 @@
 man.
-web.rest.dto.LoggerDTO
+config.Constants;%0Aimport de.rwth.idsg.bikeman.web.rest.dto.LoggerDTO;%0Aimport lombok.extern.slf4j.Slf4j
 ;%0Aim
@@ -325,16 +325,65 @@
 actory;%0A
+import org.springframework.core.env.Environment;%0A
 import o
@@ -512,11 +512,567 @@
 ion.
-*;%0A
+RequestBody;%0Aimport org.springframework.web.bind.annotation.RequestMapping;%0Aimport org.springframework.web.bind.annotation.RequestMethod;%0Aimport org.springframework.web.bind.annotation.ResponseStatus;%0Aimport org.springframework.web.bind.annotation.RestController;%0A%0Aimport javax.annotation.PostConstruct;%0Aimport javax.inject.Inject;%0Aimport javax.servlet.http.HttpServletResponse;%0Aimport java.io.IOException;%0Aimport java.io.PrintWriter;%0Aimport java.nio.charset.StandardCharsets;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;
 %0Aimp
@@ -1194,16 +1194,23 @@
 me.%0A */%0A
+@Slf4j%0A
 @RestCon
@@ -1279,39 +1279,277 @@
    @
-RequestMapping(value = %22/logs%22,
+Inject private Environment env;%0A%0A    private Path logPath;%0A%0A    @PostConstruct%0A    private void init() %7B%0A        if (env.acceptsProfiles(Constants.SPRING_PROFILE_PRODUCTION)) %7B%0A            logPath = Paths.get(System.getProperty(%22catalina.base%22), %22logs%22, %22bikeman.log%22);
 %0A   
@@ -1545,35 +1545,88 @@
 .log%22);%0A        
+%7D%0A
    
+ %7D%0A%0A    @Timed%0A    @RequestMapping(value = %22/logs%22,
  method = Reques
@@ -1637,28 +1637,16 @@
 hod.GET,
-%0A           
  produce
@@ -1683,27 +1683,16 @@
 _VALUE)%0A
-    @Timed%0A
     publ
@@ -1856,16 +1856,26 @@
         
+        
+  
 .stream(
@@ -1876,16 +1876,26 @@
 tream()%0A
+          
         
@@ -1931,16 +1931,26 @@
         
+          
 .collect
@@ -1980,19 +1980,21 @@
     
-  
-  
+%7D%0A
 %0A    
-%7D%0A
+@Timed
 %0A   
@@ -2026,28 +2026,16 @@
 %22/logs%22,
-%0A           
  method 
@@ -2098,27 +2098,16 @@
 ONTENT)%0A
-    @Timed%0A
     publ
@@ -2347,11 +2347,731 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Prints the application logs%0A     */%0A    @RequestMapping(value = %22/logs/bikeman%22, method = RequestMethod.GET)%0A    public void log(HttpServletResponse response) %7B%0A        response.setContentType(%22text/plain%22);%0A%0A        try (PrintWriter writer = response.getWriter()) %7B%0A            if (logPath == null) %7B%0A                writer.write(%22Not available, because BikeMan is not running in '%22%0A                        + Constants.SPRING_PROFILE_PRODUCTION + %22' profile%22);%0A            %7D else %7B%0A                Files.lines(logPath, StandardCharsets.UTF_8)%0A                     .forEach(writer::println);%0A            %7D%0A        %7D catch (IOException e) %7B%0A            log.error(%22Exception happened%22, e);%0A        %7D%0A    %7D
 %0A%7D%0A

make application logs accessible via web