最后活跃于 1772103084

监听WebServer初始化事件,打印程序运行相关信息,将该文件下载并粘贴到项目代码目录中即可生效

修订 7541570f1ac551c422eb92811468ddd9b0055f9f

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