LogPathHostNameProperty.java 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package com.fdkankan.modeling.config;
  2. import ch.qos.logback.core.PropertyDefinerBase;
  3. import com.fdkankan.common.util.FileUtils;
  4. import org.springframework.util.ObjectUtils;
  5. //@Component
  6. public class LogPathHostNameProperty extends PropertyDefinerBase {
  7. // @Value("${hostName.filePath:/opt/hosts/hosts.txt}")
  8. // private String hostNamePath;
  9. @Override
  10. public String getPropertyValue() {
  11. String hostNamePath = getContext().getProperty("hostName.filePath");
  12. if(ObjectUtils.isEmpty(hostNamePath)){
  13. hostNamePath = "/opt/hosts/hosts.txt";
  14. }
  15. String hostName = "null";
  16. try {
  17. hostName = FileUtils.readFile(hostNamePath);
  18. // 去除空格
  19. if(!ObjectUtils.isEmpty(hostName)){
  20. hostName = hostName.trim().replaceAll("\\s","");
  21. }
  22. } catch (Exception e) {
  23. System.err.println("=========================error======================");
  24. System.err.println("从文件中获取服务器名称失败,文件路径:"+hostNamePath);
  25. return hostName;
  26. }
  27. return hostName;
  28. }
  29. }