esoe 2 months ago
parent
commit
d846eab852
  1. 4
      logger/logs/teh.log
  2. 11
      logger/src/main/java/gsp/technologies/logger/config/StorageConfig.java
  3. 39
      logger/src/main/java/gsp/technologies/logger/controllers/Hello.java
  4. 19
      logger/src/main/java/gsp/technologies/logger/services/FileServiceImpl.java
  5. 2
      logger/src/main/resources/templates/hello.html

4
logger/logs/teh.log

@ -121,3 +121,7 @@
{"@timestamp":"2024-09-27T13:15:51.331494100Z","log.level":"INFO","process.pid":9092,"process.thread.name":"main","service.name":"logger","service.version":"1","service.environment":"Production","service.node.name":"Primary","log.logger":"gsp.technologies.logger.LoggerApplicationTests","message":"Starting LoggerApplicationTests using Java 17.0.7 with PID 9092 (started by devuser in C:\\Users\\devuser\\Documents\\code\\teh\\logger)","ecs.version":"8.11"} {"@timestamp":"2024-09-27T13:15:51.331494100Z","log.level":"INFO","process.pid":9092,"process.thread.name":"main","service.name":"logger","service.version":"1","service.environment":"Production","service.node.name":"Primary","log.logger":"gsp.technologies.logger.LoggerApplicationTests","message":"Starting LoggerApplicationTests using Java 17.0.7 with PID 9092 (started by devuser in C:\\Users\\devuser\\Documents\\code\\teh\\logger)","ecs.version":"8.11"}
{"@timestamp":"2024-09-27T13:15:51.333502400Z","log.level":"INFO","process.pid":9092,"process.thread.name":"main","service.name":"logger","service.version":"1","service.environment":"Production","service.node.name":"Primary","log.logger":"gsp.technologies.logger.LoggerApplicationTests","message":"No active profile set, falling back to 1 default profile: \"default\"","ecs.version":"8.11"} {"@timestamp":"2024-09-27T13:15:51.333502400Z","log.level":"INFO","process.pid":9092,"process.thread.name":"main","service.name":"logger","service.version":"1","service.environment":"Production","service.node.name":"Primary","log.logger":"gsp.technologies.logger.LoggerApplicationTests","message":"No active profile set, falling back to 1 default profile: \"default\"","ecs.version":"8.11"}
{"@timestamp":"2024-09-27T13:15:52.630944700Z","log.level":"INFO","process.pid":9092,"process.thread.name":"main","service.name":"logger","service.version":"1","service.environment":"Production","service.node.name":"Primary","log.logger":"gsp.technologies.logger.LoggerApplicationTests","message":"Started LoggerApplicationTests in 1.757 seconds (process running for 2.751)","ecs.version":"8.11"} {"@timestamp":"2024-09-27T13:15:52.630944700Z","log.level":"INFO","process.pid":9092,"process.thread.name":"main","service.name":"logger","service.version":"1","service.environment":"Production","service.node.name":"Primary","log.logger":"gsp.technologies.logger.LoggerApplicationTests","message":"Started LoggerApplicationTests in 1.757 seconds (process running for 2.751)","ecs.version":"8.11"}
{"@timestamp":"2024-09-27T13:57:17.389583800Z","log.level":"WARN","process.pid":22996,"process.thread.name":"main","service.name":"logger","service.version":"1","service.environment":"Production","service.node.name":"Primary","log.logger":"org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer","message":"\r\n\r\nFound multiple occurrences of org.json.JSONObject on the class path:\r\n\r\n\tjar:file:\/C:\/Users\/devuser\/.m2\/repository\/org\/json\/json\/20240303\/json-20240303.jar!\/org\/json\/JSONObject.class\r\n\tjar:file:\/C:\/Users\/devuser\/.m2\/repository\/com\/vaadin\/external\/google\/android-json\/0.0.20131108.vaadin1\/android-json-0.0.20131108.vaadin1.jar!\/org\/json\/JSONObject.class\r\n\r\nYou may wish to exclude one of them to ensure predictable runtime behavior\r\n","ecs.version":"8.11"}
{"@timestamp":"2024-09-27T13:57:17.421197100Z","log.level":"INFO","process.pid":22996,"process.thread.name":"main","service.name":"logger","service.version":"1","service.environment":"Production","service.node.name":"Primary","log.logger":"gsp.technologies.logger.LoggerApplicationTests","message":"Starting LoggerApplicationTests using Java 17.0.7 with PID 22996 (started by devuser in C:\\Users\\devuser\\Documents\\code\\teh\\logger)","ecs.version":"8.11"}
{"@timestamp":"2024-09-27T13:57:17.423166200Z","log.level":"INFO","process.pid":22996,"process.thread.name":"main","service.name":"logger","service.version":"1","service.environment":"Production","service.node.name":"Primary","log.logger":"gsp.technologies.logger.LoggerApplicationTests","message":"No active profile set, falling back to 1 default profile: \"default\"","ecs.version":"8.11"}
{"@timestamp":"2024-09-27T13:57:18.678243900Z","log.level":"INFO","process.pid":22996,"process.thread.name":"main","service.name":"logger","service.version":"1","service.environment":"Production","service.node.name":"Primary","log.logger":"gsp.technologies.logger.LoggerApplicationTests","message":"Started LoggerApplicationTests in 1.748 seconds (process running for 2.709)","ecs.version":"8.11"}

11
logger/src/main/java/gsp/technologies/logger/config/StorageConfig.java

@ -8,6 +8,9 @@ import org.springframework.context.annotation.Configuration;
public class StorageConfig { public class StorageConfig {
private String location; private String location;
private String name;
public String getLocation() { public String getLocation() {
return location; return location;
} }
@ -15,4 +18,12 @@ public class StorageConfig {
this.location = location; this.location = location;
} }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} }

39
logger/src/main/java/gsp/technologies/logger/controllers/Hello.java

@ -7,6 +7,7 @@ import java.nio.file.Paths;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@ -15,43 +16,23 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import gsp.technologies.logger.services.FileService;
@Controller @Controller
public class Hello { public class Hello {
private final FileService fs;
private static final Logger logger = LoggerFactory.getLogger(Hello.class); private static final Logger logger = LoggerFactory.getLogger(Hello.class);
@Value("${logs.location}")
private String p; @Autowired
public Hello(FileService fs) {
this.fs = fs;
}
@GetMapping("/hello") @GetMapping("/hello")
public String hello(Model model) { public String hello(Model model) {
System.out.println("logger.Hello#hello() ---> OK"); String msg = fs.content();
ResourceLoader resourceLoader = new DefaultResourceLoader();
// String p = "\\";
Path path = Paths.get(p).toAbsolutePath().normalize();
Resource resource = resourceLoader.getResource(path.toString());
StringBuilder msg = new StringBuilder();
if (resource.exists()) {
try {
File file = resource.getFile();
if (file.isDirectory()){
File[] files = file.listFiles();
if (files != null) {
for (File f : files) {
msg.append(f.getName()).append("\n");
logger.info(f.getName());
}
}
} else {
msg.append(file.getName());
logger.info(file.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}else {
msg.append("path: " + p + " --> File not found");
}
System.out.println("msg: " + msg.toString()); System.out.println("msg: " + msg.toString());
model.addAttribute("msg", msg.toString()); model.addAttribute("msg", msg.toString());
return "hello"; return "hello";

19
logger/src/main/java/gsp/technologies/logger/services/FileServiceImpl.java

@ -5,6 +5,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -16,6 +17,7 @@ import java.util.List;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource; import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import gsp.technologies.logger.config.StorageConfig; import gsp.technologies.logger.config.StorageConfig;
@ -24,9 +26,12 @@ import jakarta.annotation.PostConstruct;
@Service @Service
public class FileServiceImpl implements FileService { public class FileServiceImpl implements FileService {
private final Path storageLocation; private final Path storageLocation;
private final String logName;
public FileServiceImpl(StorageConfig config) { public FileServiceImpl(StorageConfig config) {
this.storageLocation = Paths.get(config.getLocation()) this.storageLocation = Paths.get(config.getLocation())
.toAbsolutePath().normalize(); .toAbsolutePath().normalize();
this.logName = config.getName();
} }
/** /**
@ -254,9 +259,17 @@ public class FileServiceImpl implements FileService {
@Override @Override
public String content() { public String content() {
String filename = this.logName;
// TODO Auto-generated method stub Resource resource = loadAsResource(filename);
throw new UnsupportedOperationException("Unimplemented method 'content'"); if (resource == null) {
return null;
}
try {
return StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
}
catch (IOException e) {
return null;
}
} }
} }

2
logger/src/main/resources/templates/hello.html

@ -6,6 +6,6 @@
<title>logger</title> <title>logger</title>
</head> </head>
<body> <body>
<h1 th:text="${msg}"></h1> <span th:text="${msg}"></span>
</body> </body>
</html> </html>
Loading…
Cancel
Save