1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.util;
15
16 import java.net.InetAddress;
17 import java.net.UnknownHostException;
18 import java.util.Iterator;
19 import java.util.Properties;
20
21 import ch.qos.logback.core.Context;
22 import ch.qos.logback.core.CoreConstants;
23 import ch.qos.logback.core.spi.ContextAwareBase;
24
25 public class ContextUtil extends ContextAwareBase {
26
27 public ContextUtil(Context context) {
28 setContext(context);
29 }
30
31 static String getLocalHostName() throws UnknownHostException {
32 InetAddress localhost = InetAddress.getLocalHost();
33 return localhost.getHostName();
34 }
35
36
37
38
39 public void addHostNameAsProperty() {
40 try {
41 String localhostName = getLocalHostName();
42 context.putProperty(CoreConstants.HOSTNAME_KEY, localhostName);
43 } catch (UnknownHostException e) {
44 addError("Failed to get local hostname", e);
45 } catch (SecurityException e) {
46 addError("Failed to get local hostname", e);
47 }
48 }
49
50 public void addProperties(Properties props) {
51 if (props == null) {
52 return;
53 }
54 Iterator i = props.keySet().iterator();
55 while (i.hasNext()) {
56 String key = (String) i.next();
57 context.putProperty(key, props.getProperty(key));
58 }
59 }
60 }