Skip to content

Commit a0b642c

Browse files
KlausDornsbachklaus.freitas.sccloudsJoaoJandre
authored
Refactor cloud-agent module logs for Log4j2 (#8714)
* Refactor cloud-agent module logs for Log4j2 * better log4j2 logs for cloud-agent module * erasing commented lines * erasing unused import * fixing mistakes * more log refactor * fixing log refactor lambdas * adding suggestions to log refactor * Apply suggestions from code review Co-authored-by: João Jandre <48719461+JoaoJandre@users.noreply.github.com> * Update Agent.java revert a lambda expression because log4j2 does not support a non lambda + lambda as parameters * Update Agent.java fixing little mistake --------- Co-authored-by: klaus.freitas.scclouds <klaus.freitas@scclouds.com.br> Co-authored-by: João Jandre <48719461+JoaoJandre@users.noreply.github.com>
1 parent 88d2504 commit a0b642c

File tree

6 files changed

+88
-116
lines changed

6 files changed

+88
-116
lines changed

agent/src/main/java/com/cloud/agent/Agent.java

Lines changed: 55 additions & 74 deletions
Large diffs are not rendered by default.

agent/src/main/java/com/cloud/agent/AgentShell.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ void loadProperties() throws ConfigurationException {
222222
throw new ConfigurationException("Unable to find agent.properties.");
223223
}
224224

225-
LOGGER.info("agent.properties found at " + file.getAbsolutePath());
225+
LOGGER.info("agent.properties found at {}", file.getAbsolutePath());
226226

227227
try {
228228
PropertiesUtil.loadFromFile(_properties, file);
@@ -382,15 +382,15 @@ public void init(String[] args) throws ConfigurationException {
382382
if (_version == null) {
383383
throw new CloudRuntimeException("Unable to find the implementation version of this agent");
384384
}
385-
LOGGER.info("Implementation Version is " + _version);
385+
LOGGER.info("Implementation Version is {}", _version);
386386

387387
loadProperties();
388388
parseCommand(args);
389389

390390
if (LOGGER.isDebugEnabled()) {
391391
List<String> properties = Collections.list((Enumeration<String>)_properties.propertyNames());
392392
for (String property : properties) {
393-
LOGGER.debug("Found property: " + property);
393+
LOGGER.debug("Found property: {}", property);
394394
}
395395
}
396396

@@ -411,7 +411,7 @@ public void init(String[] args) throws ConfigurationException {
411411

412412
private void launchAgent() throws ConfigurationException {
413413
String resourceClassNames = AgentPropertiesFileHandler.getPropertyValue(AgentProperties.RESOURCE);
414-
LOGGER.trace("resource=" + resourceClassNames);
414+
LOGGER.trace("resource={}", resourceClassNames);
415415
if (resourceClassNames != null) {
416416
launchAgentFromClassInfo(resourceClassNames);
417417
return;
@@ -444,7 +444,7 @@ private void launchAgentFromTypeInfo() throws ConfigurationException {
444444
LOGGER.error("Unable to retrieve the type");
445445
throw new ConfigurationException("Unable to retrieve the type of this agent.");
446446
}
447-
LOGGER.trace("Launching agent based on type=" + typeInfo);
447+
LOGGER.trace("Launching agent based on type={}", typeInfo);
448448
}
449449

450450
public void launchNewAgent(ServerResource resource) throws ConfigurationException {
@@ -506,7 +506,7 @@ public void start() {
506506
String pidDir = getProperty(null, "piddir");
507507

508508
final String run = "agent." + instance + "pid";
509-
LOGGER.debug("Checking to see if " + run + " exists.");
509+
LOGGER.debug("Checking to see if {} exists.", run);
510510
ProcessUtil.pidCheck(pidDir, run);
511511

512512
launchAgent();

agent/src/main/java/com/cloud/agent/dao/impl/PropertiesStorage.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,12 @@ public synchronized boolean configure(String name, Map<String, Object> params) {
9393
file = new File(path);
9494
try {
9595
if (!file.createNewFile()) {
96-
logger.error(String.format("Unable to create _file: %s", file.getAbsolutePath()));
96+
logger.error("Unable to create _file: {}", file.getAbsolutePath());
9797
return false;
9898
}
9999
} catch (IOException e) {
100-
logger.error(String.format("Unable to create file: %s", file.getAbsolutePath()));
101-
if (logger.isDebugEnabled()) {
102-
logger.debug(String.format("IOException while trying to create file: %s", file.getAbsolutePath()), e);
103-
}
100+
logger.error("Unable to create file: {}", file.getAbsolutePath());
101+
logger.debug("IOException while trying to create file: {}", file.getAbsolutePath(), e);
104102
return false;
105103
}
106104
}

agent/src/main/java/com/cloud/agent/mockvm/MockVmMgr.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ public String startVM(String vmName, String vnetId, String gateway, String dns,
8787

8888
@Override
8989
public String stopVM(String vmName, boolean force) {
90-
if (logger.isInfoEnabled())
91-
logger.info("Stop VM. name: " + vmName);
90+
logger.info("Stop VM. name: {}", vmName);
9291

9392
synchronized (this) {
9493
MockVm vm = vms.get(vmName);
@@ -103,8 +102,7 @@ public String stopVM(String vmName, boolean force) {
103102

104103
@Override
105104
public String rebootVM(String vmName) {
106-
if (logger.isInfoEnabled())
107-
logger.info("Reboot VM. name: " + vmName);
105+
logger.info("Reboot VM. name: {}", vmName);
108106

109107
synchronized (this) {
110108
MockVm vm = vms.get(vmName);
@@ -116,8 +114,7 @@ public String rebootVM(String vmName) {
116114

117115
@Override
118116
public boolean migrate(String vmName, String params) {
119-
if (logger.isInfoEnabled())
120-
logger.info("Migrate VM. name: " + vmName);
117+
logger.info("Migrate VM. name: {}", vmName);
121118

122119
synchronized (this) {
123120
MockVm vm = vms.get(vmName);

agent/src/main/java/com/cloud/agent/properties/AgentPropertiesFileHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ public static <T> T getPropertyValue(AgentProperties.Property<T> property) {
4848
File agentPropertiesFile = PropertiesUtil.findConfigFile(KeyStoreUtils.AGENT_PROPSFILE);
4949

5050
if (agentPropertiesFile == null) {
51-
LOGGER.debug(String.format("File [%s] was not found, we will use default defined values. Property [%s]: [%s].", KeyStoreUtils.AGENT_PROPSFILE, name, defaultValue));
51+
LOGGER.debug("File [{}] was not found, we will use default defined values. Property [{}]: [{}].", KeyStoreUtils.AGENT_PROPSFILE, name, defaultValue);
5252

5353
return defaultValue;
5454
}
5555

5656
try {
5757
String configValue = PropertiesUtil.loadFromFile(agentPropertiesFile).getProperty(name);
5858
if (StringUtils.isBlank(configValue)) {
59-
LOGGER.debug(String.format("Property [%s] has empty or null value. Using default value [%s].", name, defaultValue));
59+
LOGGER.debug("Property [{}] has empty or null value. Using default value [{}].", name, defaultValue);
6060
return defaultValue;
6161
}
6262

@@ -68,11 +68,11 @@ public static <T> T getPropertyValue(AgentProperties.Property<T> property) {
6868
ConvertUtils.register(new LongConverter(defaultValue), Long.class);
6969
}
7070

71-
LOGGER.debug(String.format("Property [%s] was altered. Now using the value [%s].", name, configValue));
71+
LOGGER.debug("Property [{}] was altered. Now using the value [{}].", name, configValue);
7272
return (T)ConvertUtils.convert(configValue, property.getTypeClass());
7373

7474
} catch (IOException ex) {
75-
LOGGER.debug(String.format("Failed to get property [%s]. Using default value [%s].", name, defaultValue), ex);
75+
LOGGER.debug("Failed to get property [{}]. Using default value [{}].", name, defaultValue, ex);
7676
}
7777

7878
return defaultValue;

agent/src/main/java/com/cloud/agent/resource/consoleproxy/ConsoleProxyResource.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ private Answer executeProxyLoadScan(final Command cmd, final long proxyVmId, fin
175175
try {
176176
is.close();
177177
} catch (final IOException e) {
178-
logger.warn("Exception when closing , console proxy address : " + proxyManagementIp);
178+
logger.warn("Exception when closing , console proxy address : {}", proxyManagementIp);
179179
success = false;
180180
}
181181
}
182182
} catch (final IOException e) {
183-
logger.warn("Unable to open console proxy command port url, console proxy address : " + proxyManagementIp);
183+
logger.warn("Unable to open console proxy command port url, console proxy address : {}", proxyManagementIp);
184184
success = false;
185185
}
186186

@@ -278,34 +278,33 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
278278
disableRpFilter();
279279
}
280280

281-
if (logger.isInfoEnabled())
282-
logger.info("Receive proxyVmId in ConsoleProxyResource configuration as " + proxyVmId);
281+
logger.info("Receive proxyVmId in ConsoleProxyResource configuration as {}", proxyVmId);
283282

284283
return true;
285284
}
286285

287286
private void addRouteToInternalIpOrCidr(String localgw, String eth1ip, String eth1mask, String destIpOrCidr) {
288-
logger.debug("addRouteToInternalIp: localgw=" + localgw + ", eth1ip=" + eth1ip + ", eth1mask=" + eth1mask + ",destIp=" + destIpOrCidr);
287+
logger.debug("addRouteToInternalIp: localgw={}, eth1ip={}, eth1mask={}, destIp={}", localgw, eth1ip, eth1mask, destIpOrCidr);
289288
if (destIpOrCidr == null) {
290289
logger.debug("addRouteToInternalIp: destIp is null");
291290
return;
292291
}
293292
if (!NetUtils.isValidIp4(destIpOrCidr) && !NetUtils.isValidIp4Cidr(destIpOrCidr)) {
294-
logger.warn(" destIp is not a valid ip address or cidr destIp=" + destIpOrCidr);
293+
logger.warn(" destIp is not a valid ip address or cidr destIp={}", destIpOrCidr);
295294
return;
296295
}
297296
boolean inSameSubnet = false;
298297
if (NetUtils.isValidIp4(destIpOrCidr)) {
299298
if (eth1ip != null && eth1mask != null) {
300299
inSameSubnet = NetUtils.sameSubnet(eth1ip, destIpOrCidr, eth1mask);
301300
} else {
302-
logger.warn("addRouteToInternalIp: unable to determine same subnet: eth1ip=" + eth1ip + ", dest ip=" + destIpOrCidr + ", eth1mask=" + eth1mask);
301+
logger.warn("addRouteToInternalIp: unable to determine same subnet: eth1ip={}, dest ip={}, eth1mask={}", eth1ip, destIpOrCidr, eth1mask);
303302
}
304303
} else {
305304
inSameSubnet = NetUtils.isNetworkAWithinNetworkB(destIpOrCidr, NetUtils.ipAndNetMaskToCidr(eth1ip, eth1mask));
306305
}
307306
if (inSameSubnet) {
308-
logger.debug("addRouteToInternalIp: dest ip " + destIpOrCidr + " is in the same subnet as eth1 ip " + eth1ip);
307+
logger.debug("addRouteToInternalIp: dest ip {} is in the same subnet as eth1 ip {}", destIpOrCidr, eth1ip);
309308
return;
310309
}
311310
Script command = new Script("/bin/bash", logger);
@@ -317,9 +316,9 @@ private void addRouteToInternalIpOrCidr(String localgw, String eth1ip, String et
317316
command.add("ip route add " + destIpOrCidr + " via " + localgw);
318317
String result = command.execute();
319318
if (result != null) {
320-
logger.warn("Error in configuring route to internal ip err=" + result);
319+
logger.warn("Error in configuring route to internal ip err={}", result);
321320
} else {
322-
logger.debug("addRouteToInternalIp: added route to internal ip=" + destIpOrCidr + " via " + localgw);
321+
logger.debug("addRouteToInternalIp: added route to internal ip={} via {}", destIpOrCidr, localgw);
323322
}
324323
}
325324

@@ -332,7 +331,7 @@ private void launchConsoleProxy(final byte[] ksBits, final String ksPassword, fi
332331
final Object resource = this;
333332
logger.info("Building class loader for com.cloud.consoleproxy.ConsoleProxy");
334333
if (consoleProxyMain == null) {
335-
logger.info("Running com.cloud.consoleproxy.ConsoleProxy with encryptor password=" + encryptorPassword);
334+
logger.info("Running com.cloud.consoleproxy.ConsoleProxy with encryptor password={}", encryptorPassword);
336335
consoleProxyMain = new Thread(new ManagedContextRunnable() {
337336
@Override
338337
protected void runInContext() {
@@ -355,7 +354,7 @@ protected void runInContext() {
355354
logger.error("Unable to launch console proxy due to IllegalAccessException", e);
356355
System.exit(ExitStatus.Error.value());
357356
} catch (InvocationTargetException e) {
358-
logger.error("Unable to launch console proxy due to InvocationTargetException " + e.getTargetException().toString(), e);
357+
logger.error("Unable to launch console proxy due to InvocationTargetException {}", e.getTargetException().toString(), e);
359358
System.exit(ExitStatus.Error.value());
360359
}
361360
} catch (final ClassNotFoundException e) {
@@ -418,10 +417,10 @@ public String authenticateConsoleAccess(String host, String port, String vmId, S
418417
result.setTunnelUrl(authAnswer.getTunnelUrl());
419418
result.setTunnelSession(authAnswer.getTunnelSession());
420419
} else {
421-
logger.error("Authentication failed for vm: " + vmId + " with sid: " + sid);
420+
logger.error("Authentication failed for vm: {} with sid: {}", vmId, sid);
422421
}
423422
} catch (AgentControlChannelException e) {
424-
logger.error("Unable to send out console access authentication request due to " + e.getMessage(), e);
423+
logger.error("Unable to send out console access authentication request due to {}", e.getMessage(), e);
425424
}
426425

427426
return new Gson().toJson(result);
@@ -431,26 +430,23 @@ public void reportLoadInfo(String gsonLoadInfo) {
431430
ConsoleProxyLoadReportCommand cmd = new ConsoleProxyLoadReportCommand(proxyVmId, gsonLoadInfo);
432431
try {
433432
getAgentControl().postRequest(cmd);
434-
435-
if (logger.isDebugEnabled())
436-
logger.debug("Report proxy load info, proxy : " + proxyVmId + ", load: " + gsonLoadInfo);
433+
logger.debug("Report proxy load info, proxy : {}, load: {}", proxyVmId, gsonLoadInfo);
437434
} catch (AgentControlChannelException e) {
438-
logger.error("Unable to send out load info due to " + e.getMessage(), e);
435+
logger.error("Unable to send out load info due to {}", e.getMessage(), e);
439436
}
440437
}
441438

442439
public void ensureRoute(String address) {
443440
if (localGateway != null) {
444-
if (logger.isDebugEnabled())
445-
logger.debug("Ensure route for " + address + " via " + localGateway);
441+
logger.debug("Ensure route for {} via {}", address, localGateway);
446442

447443
// this method won't be called in high frequency, serialize access
448444
// to script execution
449445
synchronized (this) {
450446
try {
451447
addRouteToInternalIpOrCidr(localGateway, eth1Ip, eth1Mask, address);
452448
} catch (Throwable e) {
453-
logger.warn("Unexpected exception while adding internal route to " + address, e);
449+
logger.warn("Unexpected exception while adding internal route to {}", address, e);
454450
}
455451
}
456452
}

0 commit comments

Comments
 (0)