diff --git a/logger/logs/teh.log b/logger/logs/teh.log index 5878254..802ecf4 100644 --- a/logger/logs/teh.log +++ b/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.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: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"} diff --git a/logger/src/main/java/gsp/technologies/logger/config/StorageConfig.java b/logger/src/main/java/gsp/technologies/logger/config/StorageConfig.java index 8bc55c5..912624f 100644 --- a/logger/src/main/java/gsp/technologies/logger/config/StorageConfig.java +++ b/logger/src/main/java/gsp/technologies/logger/config/StorageConfig.java @@ -8,6 +8,9 @@ import org.springframework.context.annotation.Configuration; public class StorageConfig { private String location; + private String name; + + public String getLocation() { return location; } @@ -15,4 +18,12 @@ public class StorageConfig { this.location = location; } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } diff --git a/logger/src/main/java/gsp/technologies/logger/controllers/Hello.java b/logger/src/main/java/gsp/technologies/logger/controllers/Hello.java index 38269ce..d394c01 100644 --- a/logger/src/main/java/gsp/technologies/logger/controllers/Hello.java +++ b/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.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; @@ -15,43 +16,23 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import gsp.technologies.logger.services.FileService; + @Controller public class Hello { + private final FileService fs; 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") public String hello(Model model) { - System.out.println("logger.Hello#hello() ---> OK"); - 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"); - } + String msg = fs.content(); System.out.println("msg: " + msg.toString()); model.addAttribute("msg", msg.toString()); return "hello"; diff --git a/logger/src/main/java/gsp/technologies/logger/services/FileServiceImpl.java b/logger/src/main/java/gsp/technologies/logger/services/FileServiceImpl.java index 9dbfe00..4613ce4 100644 --- a/logger/src/main/java/gsp/technologies/logger/services/FileServiceImpl.java +++ b/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.InputStream; import java.net.MalformedURLException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -16,6 +17,7 @@ import java.util.List; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; import org.springframework.stereotype.Service; +import org.springframework.util.StreamUtils; import org.springframework.web.multipart.MultipartFile; import gsp.technologies.logger.config.StorageConfig; @@ -24,9 +26,12 @@ import jakarta.annotation.PostConstruct; @Service public class FileServiceImpl implements FileService { private final Path storageLocation; + private final String logName; + public FileServiceImpl(StorageConfig config) { this.storageLocation = Paths.get(config.getLocation()) .toAbsolutePath().normalize(); + this.logName = config.getName(); } /** @@ -254,9 +259,17 @@ public class FileServiceImpl implements FileService { @Override public String content() { - - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'content'"); + String filename = this.logName; + Resource resource = loadAsResource(filename); + if (resource == null) { + return null; + } + try { + return StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8); + } + catch (IOException e) { + return null; + } } } \ No newline at end of file diff --git a/logger/src/main/resources/templates/hello.html b/logger/src/main/resources/templates/hello.html index df4e080..8ac170b 100644 --- a/logger/src/main/resources/templates/hello.html +++ b/logger/src/main/resources/templates/hello.html @@ -6,6 +6,6 @@