1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.docker;
18
19 import java.io.IOException;
20 import java.net.URL;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.logging.log4j.Logger;
25 import org.apache.logging.log4j.core.LogEvent;
26 import org.apache.logging.log4j.core.config.plugins.Plugin;
27 import org.apache.logging.log4j.core.lookup.AbstractLookup;
28 import org.apache.logging.log4j.core.lookup.StrLookup;
29 import org.apache.logging.log4j.core.util.NetUtils;
30 import org.apache.logging.log4j.docker.model.Container;
31 import org.apache.logging.log4j.docker.model.Network;
32 import org.apache.logging.log4j.status.StatusLogger;
33 import org.apache.logging.log4j.util.PropertiesUtil;
34 import com.fasterxml.jackson.core.type.TypeReference;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36
37
38
39
40 @Plugin(name = "docker", category = StrLookup.CATEGORY)
41 public class DockerLookup extends AbstractLookup {
42
43 private static final Logger LOGGER = StatusLogger.getLogger();
44 private static final String DOCKER_URI = "DOCKER_URI";
45 private static final String HTTP = "http";
46 private final Container container;
47
48
49
50
51 public DockerLookup() {
52 String baseUri = System.getenv(DOCKER_URI);
53 if (baseUri == null) {
54 PropertiesUtil props = PropertiesUtil.getProperties();
55 baseUri = props.getStringProperty(DOCKER_URI);
56 }
57 if (baseUri == null) {
58 LOGGER.warn("No Docker URI provided. Docker information is unavailable");
59 container = null;
60 return;
61 }
62 Container current = null;
63 try {
64 URL url= new URL(baseUri + "/containers/json");
65 if (url.getProtocol().equals(HTTP)) {
66 String macAddr = NetUtils.getMacAddressString();
67 ObjectMapper objectMapper = new ObjectMapper();
68 List<Container> containerList = objectMapper.readValue(url, new TypeReference<List<Container>>(){});
69
70 for (Container container : containerList) {
71 if (macAddr != null && container.getNetworkSettings() != null) {
72 Map<String, Network> networks = container.getNetworkSettings().getNetworks();
73 if (networks != null) {
74 for (Network network: networks.values()) {
75 if (macAddr.equals(network.getMacAddress())) {
76 current = container;
77 break;
78 }
79 }
80 }
81 }
82 if (current != null) {
83 break;
84 }
85 }
86 }
87 if (current == null) {
88 LOGGER.warn("Unable to determine current container");
89 }
90 } catch (IOException ioe) {
91 LOGGER.warn("Unable to read container information: " + ioe.getMessage());
92 }
93 container = current;
94 }
95
96 @Override
97 public String lookup(LogEvent event, String key) {
98 if (container == null) {
99 return null;
100 }
101 switch (key) {
102 case "shortContainerId": {
103 return container.getId().substring(0, 12);
104 }
105 case "containerId": {
106 return container.getId();
107 }
108 case "containerName": {
109 if (container.getNames().size() > 1) {
110 return container.getNames().toString();
111 }
112 return container.getNames().get(0);
113 }
114 case "shortImageId": {
115 return container.getImageId().substring(0, 12);
116 }
117 case "imageId": {
118 return container.getImageId();
119 }
120 case "imageName": {
121 return container.getImage();
122 }
123 default:
124 return null;
125 }
126 }
127 }