Doracoin a révisé ce gist . Aller à la révision
1 file changed, 12 insertions, 6 deletions
AdvancedStartupLogger.java
| @@ -32,7 +32,13 @@ public class AdvancedStartupLogger implements ApplicationListener<WebServerIniti | |||
| 32 | 32 | @Override | |
| 33 | 33 | public void onApplicationEvent(WebServerInitializedEvent event) { | |
| 34 | 34 | ||
| 35 | - | int port = event.getWebServer().getPort(); | |
| 35 | + | // int port = event.getWebServer().getPort(); | |
| 36 | + | String serverPort = env.getProperty("local.server.port"); | |
| 37 | + | String managementPort = env.getProperty("local.management.port"); | |
| 38 | + | ||
| 39 | + | if (managementPort == null) { | |
| 40 | + | managementPort = serverPort; | |
| 41 | + | } | |
| 36 | 42 | String protocol = resolveProtocol(); | |
| 37 | 43 | String contextPath = resolveContextPath(); | |
| 38 | 44 | String appName = env.getProperty("spring.application.name", "application"); | |
| @@ -73,15 +79,15 @@ public class AdvancedStartupLogger implements ApplicationListener<WebServerIniti | |||
| 73 | 79 | startupMs, | |
| 74 | 80 | maxHeapMb, | |
| 75 | 81 | protocol, | |
| 76 | - | port, | |
| 82 | + | serverPort, | |
| 77 | 83 | contextPath, | |
| 78 | 84 | protocol, | |
| 79 | 85 | outboundIp, | |
| 80 | - | port, | |
| 86 | + | serverPort, | |
| 81 | 87 | contextPath, | |
| 82 | 88 | protocol, | |
| 83 | 89 | outboundIp, | |
| 84 | - | port, | |
| 90 | + | managementPort, | |
| 85 | 91 | contextPath, | |
| 86 | 92 | actuatorPath, | |
| 87 | 93 | allIps, | |
| @@ -89,7 +95,7 @@ public class AdvancedStartupLogger implements ApplicationListener<WebServerIniti | |||
| 89 | 95 | k8s | |
| 90 | 96 | ); | |
| 91 | 97 | ||
| 92 | - | logStructuredJson(appName, port, outboundIp, startupMs); | |
| 98 | + | logStructuredJson(appName, serverPort, outboundIp, startupMs); | |
| 93 | 99 | } | |
| 94 | 100 | ||
| 95 | 101 | private String resolveProtocol() { | |
| @@ -142,7 +148,7 @@ public class AdvancedStartupLogger implements ApplicationListener<WebServerIniti | |||
| 142 | 148 | return ips; | |
| 143 | 149 | } | |
| 144 | 150 | ||
| 145 | - | private void logStructuredJson(String appName, int port, String ip, long startupMs) { | |
| 151 | + | private void logStructuredJson(String appName, String port, String ip, long startupMs) { | |
| 146 | 152 | log.info("STARTUP_JSON: {{\"app\":\"{}\",\"port\":{},\"ip\":\"{}\",\"startupMs\":{}}}", | |
| 147 | 153 | appName, port, ip, startupMs); | |
| 148 | 154 | } | |
doracoin a révisé ce gist . Aller à la révision
1 file changed, 149 insertions
AdvancedStartupLogger.java(fichier créé)
| @@ -0,0 +1,149 @@ | |||
| 1 | + | package com.example.startup; | |
| 2 | + | ||
| 3 | + | import lombok.extern.slf4j.Slf4j; | |
| 4 | + | import org.springframework.boot.web.context.WebServerInitializedEvent; | |
| 5 | + | import org.springframework.context.ApplicationListener; | |
| 6 | + | import org.springframework.core.env.Environment; | |
| 7 | + | import org.springframework.stereotype.Component; | |
| 8 | + | ||
| 9 | + | import java.lang.management.ManagementFactory; | |
| 10 | + | import java.net.DatagramSocket; | |
| 11 | + | import java.net.Inet4Address; | |
| 12 | + | import java.net.InetAddress; | |
| 13 | + | import java.net.NetworkInterface; | |
| 14 | + | import java.time.Duration; | |
| 15 | + | import java.time.Instant; | |
| 16 | + | import java.util.ArrayList; | |
| 17 | + | import java.util.Arrays; | |
| 18 | + | import java.util.Enumeration; | |
| 19 | + | import java.util.List; | |
| 20 | + | ||
| 21 | + | @Slf4j | |
| 22 | + | @Component | |
| 23 | + | public class AdvancedStartupLogger implements ApplicationListener<WebServerInitializedEvent> { | |
| 24 | + | ||
| 25 | + | private final Environment env; | |
| 26 | + | private static final Instant START_TIME = Instant.now(); | |
| 27 | + | ||
| 28 | + | public AdvancedStartupLogger(Environment env) { | |
| 29 | + | this.env = env; | |
| 30 | + | } | |
| 31 | + | ||
| 32 | + | @Override | |
| 33 | + | public void onApplicationEvent(WebServerInitializedEvent event) { | |
| 34 | + | ||
| 35 | + | int port = event.getWebServer().getPort(); | |
| 36 | + | String protocol = resolveProtocol(); | |
| 37 | + | String contextPath = resolveContextPath(); | |
| 38 | + | String appName = env.getProperty("spring.application.name", "application"); | |
| 39 | + | String profiles = Arrays.toString(env.getActiveProfiles()); | |
| 40 | + | String pid = getPid(); | |
| 41 | + | String javaVersion = System.getProperty("java.version"); | |
| 42 | + | String outboundIp = getOutboundIp(); | |
| 43 | + | boolean docker = isDocker(); | |
| 44 | + | boolean k8s = isK8s(); | |
| 45 | + | ||
| 46 | + | long startupMs = Duration.between(START_TIME, Instant.now()).toMillis(); | |
| 47 | + | ||
| 48 | + | List<String> allIps = getAllNetworkIps(); | |
| 49 | + | long maxHeapMb = Runtime.getRuntime().maxMemory() / 1024 / 1024; | |
| 50 | + | ||
| 51 | + | String actuatorPath = env.getProperty("management.endpoints.web.base-path", "actuator"); | |
| 52 | + | ||
| 53 | + | log.info("\n" + | |
| 54 | + | "================= 🚀 SERVICE STARTED =================\n" + | |
| 55 | + | "App Name : {}\n" + | |
| 56 | + | "PID : {}\n" + | |
| 57 | + | "Profiles : {}\n" + | |
| 58 | + | "Java Version : {}\n" + | |
| 59 | + | "Startup Time : {} ms\n" + | |
| 60 | + | "Max Heap : {} MB\n\n" + | |
| 61 | + | "Access URLs:\n" + | |
| 62 | + | "Local : {}://localhost:{}{}\n" + | |
| 63 | + | "External : {}://{}:{}{}\n" + | |
| 64 | + | "Actuator : {}://{}:{}{}{}\n\n" + | |
| 65 | + | "All Network IPs : {}\n" + | |
| 66 | + | "Docker : {}\n" + | |
| 67 | + | "Kubernetes : {}\n" + | |
| 68 | + | "=======================================================", | |
| 69 | + | appName, | |
| 70 | + | pid, | |
| 71 | + | profiles, | |
| 72 | + | javaVersion, | |
| 73 | + | startupMs, | |
| 74 | + | maxHeapMb, | |
| 75 | + | protocol, | |
| 76 | + | port, | |
| 77 | + | contextPath, | |
| 78 | + | protocol, | |
| 79 | + | outboundIp, | |
| 80 | + | port, | |
| 81 | + | contextPath, | |
| 82 | + | protocol, | |
| 83 | + | outboundIp, | |
| 84 | + | port, | |
| 85 | + | contextPath, | |
| 86 | + | actuatorPath, | |
| 87 | + | allIps, | |
| 88 | + | docker, | |
| 89 | + | k8s | |
| 90 | + | ); | |
| 91 | + | ||
| 92 | + | logStructuredJson(appName, port, outboundIp, startupMs); | |
| 93 | + | } | |
| 94 | + | ||
| 95 | + | private String resolveProtocol() { | |
| 96 | + | return env.getProperty("server.ssl.key-store") != null ? "https" : "http"; | |
| 97 | + | } | |
| 98 | + | ||
| 99 | + | private String resolveContextPath() { | |
| 100 | + | return env.getProperty("server.servlet.context-path", ""); | |
| 101 | + | } | |
| 102 | + | ||
| 103 | + | private String getPid() { | |
| 104 | + | return ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; | |
| 105 | + | } | |
| 106 | + | ||
| 107 | + | private boolean isDocker() { | |
| 108 | + | return new java.io.File("/.dockerenv").exists(); | |
| 109 | + | } | |
| 110 | + | ||
| 111 | + | private boolean isK8s() { | |
| 112 | + | return System.getenv("KUBERNETES_SERVICE_HOST") != null; | |
| 113 | + | } | |
| 114 | + | ||
| 115 | + | private String getOutboundIp() { | |
| 116 | + | try (DatagramSocket socket = new DatagramSocket()) { | |
| 117 | + | socket.connect(InetAddress.getByName("8.8.8.8"), 10002); | |
| 118 | + | return socket.getLocalAddress().getHostAddress(); | |
| 119 | + | } catch (Exception e) { | |
| 120 | + | return "Unknown"; | |
| 121 | + | } | |
| 122 | + | } | |
| 123 | + | ||
| 124 | + | private List<String> getAllNetworkIps() { | |
| 125 | + | List<String> ips = new ArrayList<>(); | |
| 126 | + | try { | |
| 127 | + | Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); | |
| 128 | + | while (interfaces.hasMoreElements()) { | |
| 129 | + | NetworkInterface ni = interfaces.nextElement(); | |
| 130 | + | if (!ni.isUp() || ni.isLoopback()) continue; | |
| 131 | + | ||
| 132 | + | Enumeration<InetAddress> addresses = ni.getInetAddresses(); | |
| 133 | + | while (addresses.hasMoreElements()) { | |
| 134 | + | InetAddress addr = addresses.nextElement(); | |
| 135 | + | if (addr instanceof Inet4Address) { | |
| 136 | + | ips.add(addr.getHostAddress()); | |
| 137 | + | } | |
| 138 | + | } | |
| 139 | + | } | |
| 140 | + | } catch (Exception ignored) { | |
| 141 | + | } | |
| 142 | + | return ips; | |
| 143 | + | } | |
| 144 | + | ||
| 145 | + | private void logStructuredJson(String appName, int port, String ip, long startupMs) { | |
| 146 | + | log.info("STARTUP_JSON: {{\"app\":\"{}\",\"port\":{},\"ip\":\"{}\",\"startupMs\":{}}}", | |
| 147 | + | appName, port, ip, startupMs); | |
| 148 | + | } | |
| 149 | + | } | |