log4j2输出带有ip日志,便于集群环境查找
1.设置环境变量
1 2 3 4 5 6 7 8 9 10 11 | /设置系统环境变量 System.setProperty( "local-ip" , "10.99.1.51" ); //获取系统环境变量 System.out.println(System.getProperty( "local-ip" )); try { //设置本机地址到环境变量 System.setProperty( "local-ip" , LocalIP.getIpAddress().getHostAddress()); } catch (SocketException e) { e.printStackTrace(); } |
2.log4j2.xml配置获取环境变量
1 2 3 4 5 6 | <Property name= "logFormat" > [${sys:local-ip}] [%thread] %-5level %logger{ 35 } - %msg %n </Property> <Property name= "log-local-ip" > ${sys:local-ip} </Property> |
3.java获取本地ip过滤掉还回和虚拟网卡地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class LocalIP { public static void main(String[] args) throws SocketException { System.out.println(getIpAddress().getHostAddress()); } public static InetAddress getIpAddress() throws SocketException { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface current = interfaces.nextElement(); if (!current.isUp() || current.isLoopback() || current.isVirtual()) continue ; Enumeration<InetAddress> addresses = current.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr.isLoopbackAddress()) continue ; if (addr.isSiteLocalAddress()) { //去掉还回和虚拟地址 return addr; } // System.out.println(addr.isSiteLocalAddress()); // System.out.println(addr); } } throw new SocketException( "Can't get our ip address, interfaces are: " + interfaces); } } |
11
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
· Windows 提权-UAC 绕过
2013-11-24 Spring加载Hibernate 映射的几种方式及区别
2010-11-24 List的二种循环速度比较
2010-11-24 java 动态编译源代码
2010-11-24 jvm的内存调优