Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrichardsz/87029eb1e1a4ca93cd744b55c2337ccc to your computer and use it in GitHub Desktop.
Save jrichardsz/87029eb1e1a4ca93cd744b55c2337ccc to your computer and use it in GitHub Desktop.
log controller web, log view, java
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