logback日志配置里获取服务器ip和端口

1、新建一个类继承ClassicConverter,在方法中获取服务器ip

package com.xxx.xxx.xxx.common;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author xiaoming
 * @date 2019/5/14 11:37
 * @description
 */
public class LogIpConfig extends ClassicConverter {
 private static final Logger logger = LoggerFactory.getLogger(LogIpConfig .class);
    private static String webIP;
    static {
        try {
            webIP = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            logger.error("获取日志Ip异常", e);
            webIP = null;
        }
    }

    @Override
    public String convert(ILoggingEvent event) {
        return webIP;
    }
}

2、在配置文件logback.xml增加如下配置:

<conversionRule conversionWord="ip" converterClass="com.xxx.xxx.xxx.common.LogIpConfig" />

3、在需要用到ip的位置这样写:  %ip

"host": "%ip" 这样写,本地日志输入的时候内容是: "host": "127.0.0.1"

4、获取端口号,同理:

package com.xxx.xxx.xxx.common;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author xiaoming
 * @date 2019/5/14 11:37
 * @description
 */
public class LogPortConfig extends ClassicConverter {
    private static final Logger logger = LoggerFactory.getLogger(LogPortConfig.class);
    private static String webPort;

    static {
        try {
            List<MBeanServer> serverList = MBeanServerFactory.findMBeanServer(null);
            for (MBeanServer server : serverList) {
                Set<ObjectName> names = new HashSet<ObjectName>();
                names.addAll(server.queryNames(new ObjectName("Catalina:type=Connector,*"), null));
                Iterator<ObjectName> it = names.iterator();
                while (it.hasNext()) {
                    ObjectName oName = (ObjectName) it.next();
                    String pValue = (String) server.getAttribute(oName, "protocol");
                    if (StringUtils.equals("HTTP/1.1", pValue)) {
                        webPort = ObjectUtils.toString(server.getAttribute(oName, "port"));
                    }
                }
            }
        } catch (Exception e) {
            logger.error("获取port失败,影响logback的文件拼接", e);
            webPort = null;
        }
    }

    @Override
    public String convert(ILoggingEvent event) {
        return webPort;
    }
}
    <conversionRule conversionWord="port" converterClass="com.xxx.xxx.xxx.common.LogPortConfig" />
%ip:%port 
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值