log4j2输出带有ip日志,便于集群环境查找
2023-09-27 14:21:32 时间
1.设置环境变量
/设置系统环境变量 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配置获取环境变量
<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过滤掉还回和虚拟网卡地址
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
相关文章
- SQL Server 2008下日志清理方法 2
- gunicorn日志系列1-输出限制大小的问题 Unable to configure root logger: Unable to add handler ‘console‘: ‘console‘
- 日志
- 记一次CDH6.3.2集群日志数据清理指南
- 记一次CDH集群节点日志文件清理
- Linux常用日志查询命令
- 110 Storm集群的进程及日志熟悉
- 135 日志监控告警系统案例(数据模型设计)
- Solr/Lucene日志分析-查询热点词-脚本工具
- CentOS日志的简单介绍
- weblogic日志无法分割,weblogic日志分割,weblogic集群日志分割
- 《日志管理与分析权威指南》一3.3 日志来源分类
- 《Storm实时数据处理》一2.7 为日志流集群创建集成测试
- CentOS7下 简单安装和配置Elasticsearch Kibana Filebeat 快速搭建集群日志收集平台(版本6.x)
- Unity3D 开发工具系列 日志系统:核心模块Logging
- DBCC SHRINKFILE收缩日志/收缩数据库/收缩文件