Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

getLocalHostName() now falls back to NetowrkInterface if InetAddress.…

…getLocalHost() throws an exception.
  • Loading branch information...
commit 74747150c16afd2f0ae5bad60b8aec1374c6f55c 1 parent 6b00dcd
Tim Clemons timclemons authored
Showing with 12 additions and 5 deletions.
  1. +12 −5 src/main/java/me/moocar/logbackgelf/GelfAppender.java
17 src/main/java/me/moocar/logbackgelf/GelfAppender.java
View
@@ -4,8 +4,12 @@
import java.io.*;
import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
import java.net.UnknownHostException;
import java.security.MessageDigest;
+import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -104,12 +108,15 @@ private void initExecutor() {
/**
* Retrieves the localhost's hostname, or if unavailable, the ip address
*/
- private String getLocalHostName() throws UnknownHostException {
- InetAddress localhost = InetAddress.getLocalHost();
+ private String getLocalHostName() throws SocketException, UnknownHostException {
try {
- return localhost.getHostName();
- } catch (Exception e) {
- return localhost.getHostAddress();
+ return InetAddress.getLocalHost().getHostName();
+ } catch (UnknownHostException e) {
+ NetworkInterface networkInterface = NetworkInterface.getNetworkInterfaces().nextElement();
+ if (networkInterface == null) throw e;
+ InetAddress ipAddress = networkInterface.getInetAddresses().nextElement();
+ if (ipAddress == null) throw e;
+ return ipAddress.getHostAddress();
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.