Created
July 1, 2022 22:08
-
-
Save jrichardsz/87029eb1e1a4ca93cd744b55c2337ccc to your computer and use it in GitHub Desktop.
log controller web, log view, java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardOpenOption; | |
import java.util.Map; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.ResponseBody; | |
import org.springframework.web.bind.annotation.RestController; | |
import com.google.common.collect.ImmutableMap; | |
import edu.usil.ldap.common.ApiResponseCodes; | |
import io.swagger.annotations.Api; | |
import io.swagger.annotations.ApiOperation; | |
@RestController | |
@RequestMapping("/v1/meta") | |
@Api(tags = "meta") | |
public class LogController { | |
@Value(value = "${META_LOG_PATH}") | |
private String logPath; | |
@RequestMapping(method = RequestMethod.GET, path = "/log/view", produces = "text/plain") | |
@ApiOperation(nickname = "log", value = "log", notes = "log") | |
@ResponseBody | |
public String simpleLogViewer() { | |
try { | |
System.out.println(logPath); | |
String content = new String(Files.readAllBytes(Paths.get(logPath)), "UTF-8"); | |
System.out.println(content); | |
return content; | |
} catch (Exception e) { | |
return "Is not possible to read the log file"; | |
} | |
} | |
@RequestMapping(method = RequestMethod.GET, path = "/log/truncate", produces = "application/json") | |
@ApiOperation(nickname = "log", value = "log", notes = "log") | |
public Map<String, Object> simpleHealth() { | |
try { | |
Files.write(Paths.get(logPath), new byte[0], StandardOpenOption.TRUNCATE_EXISTING); | |
return ImmutableMap.<String, Object>builder().put("code", 200000).put("message", "success") | |
.build(); | |
} catch (IOException e) { | |
return ImmutableMap.<String, Object>builder() | |
.put("code", ApiResponseCodes.UNCATEGORIZED_ERROR.value()) | |
.put("message", "File cannot be truncated").build(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment