Skip to content

Commit b12a992

Browse files
jshum2479ddsharpe
authored andcommitted
add logging and remove setLogger in ImageOperation - use logging.prop… (#10)
* add logging and remove setLogger in ImageOperation - use logging.properties to control it. * change and comment default file name in create if it ever happens
1 parent accfab7 commit b12a992

File tree

9 files changed

+76
-66
lines changed

9 files changed

+76
-66
lines changed
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
handlers=java.util.logging.ConsoleHandler
2-
# Uncomment to enable file logging
32
#handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
3+
4+
# Enable as needed
5+
com.oracle.weblogic.imagetool.level=INFO
6+
#com.oracle.weblogic.imagetool.cli.menu.CreateImage.level = FINEST
7+
#com.oracle.weblogic.imagetool.cli.menu.UpdateImage.level = FINEST
8+
#com.oracle.weblogic.imagetool.cli.menu.ImageOperation.level = FINEST
9+
#com.oracle.weblogic.imagetool.util.ARUUtil.level = FINEST
10+
#com.oracle.weblogic.imagetool.util.XPathUtil.level = FINEST
11+
#com.oracle.weblogic.imagetool.impl.PatchFile.level = FINEST
12+
#com.oracle.weblogic.imagetool.impl.InstallerFile.level = FINEST
13+
#com.oracle.weblogic.imagetool.util.Utils.level = FINEST
14+
15+
#
16+
# Change log file location and handlers logging level as needed
417
#
5-
# Change log file location as needed
618
java.util.logging.FileHandler.pattern=tool.log
7-
java.util.logging.FileHandler.limit=50000
819
java.util.logging.FileHandler.count=1
920
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
1021
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%3$s] [%4$-7s] %5$s %n
11-
java.util.logging.FileHandler.level=INFO
12-
.level = INFO
13-
# Enable as needed
14-
#com.oracle.weblogic.imagetool.cli.menu.CreateImage.level = INFO
15-
#com.oracle.weblogic.imagetool.cli.menu.UpdateImage.level = INFO
16-
#com.oracle.weblogic.imagetool.cli.menu.ImageOperation.level = INFO
17-
#com.oracle.weblogic.imagetool.util.ARUUtil.level = INFO
18-
#com.oracle.weblogic.imagetool.util.XPathUtil.level = INFO
19-
#com.oracle.weblogic.imagetool.impl.PatchFile.level = INFO
20-
#com.oracle.weblogic.imagetool.impl.InstallerFile.level = INFO
21-
#com.oracle.weblogic.imagetool.util.Utils.level = FINEST
22+
java.util.logging.FileHandler.level=OFF
23+
java.util.logging.ConsoleHandler.level=INFO
2224

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/CreateImage.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ public CreateImage(boolean isCLIMode) {
5151

5252
@Override
5353
public CommandResponse call() throws Exception {
54-
54+
logger.finer("Entering CreateImage call ");
5555
Instant startTime = Instant.now();
5656

57-
setupLogger(isCLIMode);
5857
Path tmpDir = null;
5958
Path tmpDir2 = null;
6059

@@ -79,6 +78,7 @@ public CommandResponse call() throws Exception {
7978
cmdBuilder.addAll(handleInstallerFiles(tmpDir));
8079

8180
if (fromImage != null && !fromImage.isEmpty()) {
81+
logger.finer("User specified fromImage " + fromImage);
8282
cmdBuilder.add(Constants.BUILD_ARG);
8383
cmdBuilder.add("BASE_IMAGE=" + fromImage);
8484

@@ -136,6 +136,7 @@ public CommandResponse call() throws Exception {
136136
Utils.deleteFilesRecursively(tmpDir2);
137137
}
138138
Instant endTime = Instant.now();
139+
logger.finer("Exiting CreateImage call ");
139140
return new CommandResponse(0, "build successful in " + Duration.between(startTime, endTime).getSeconds() + "s. image tag: " + imageTag);
140141
}
141142

@@ -148,11 +149,14 @@ public CommandResponse call() throws Exception {
148149
* @throws Exception in case of error
149150
*/
150151
private List<String> handleInstallerFiles(Path tmpDir) throws Exception {
152+
153+
logger.finer("Entering CreateImage.handleInstallerFiles: " + tmpDir.toAbsolutePath().toString());
151154
List<String> retVal = new LinkedList<>();
152155
String tmpDirPath = tmpDir.toAbsolutePath().toString();
153156
List<InstallerFile> requiredInstallers = gatherRequiredInstallers();
154157
for (InstallerFile eachInstaller : requiredInstallers) {
155158
String targetFilePath = eachInstaller.resolve(cacheStore);
159+
logger.finer("Entering CreateImage.handleInstallerFiles targetFilePath: " + targetFilePath);
156160
File targetFile = new File(targetFilePath);
157161
try {
158162
Path targetLink = Files.copy(Paths.get(targetFilePath), Paths.get(tmpDirPath, targetFile.getName()));
@@ -161,16 +165,19 @@ private List<String> handleInstallerFiles(Path tmpDir) throws Exception {
161165
ee.printStackTrace();
162166
}
163167
}
168+
logger.finer("Exiting CreateImage.handleInstallerFiles: " );
164169
return retVal;
165170
}
166171

167172
@Override
168173
List<String> handlePatchFiles(Path tmpDir, Path tmpPatchesDir) throws Exception {
174+
logger.finer("Entering CreateImage.handlePatchFiles: " + tmpDir.toAbsolutePath().toString());
169175
if ((latestPSU || !patches.isEmpty()) && Utils.compareVersions(installerVersion, Constants.DEFAULT_WLS_VERSION) == 0) {
170176
addOPatch1394ToImage(tmpDir);
171177
}
172178
//we need a local installerVersion variable for the command line Option. so propagate to super.
173179
super.installerVersion = installerVersion;
180+
logger.finer("Exiting CreateImage.handlePatchFiles: ");
174181
return super.handlePatchFiles(tmpDir, tmpPatchesDir);
175182
}
176183

@@ -184,6 +191,7 @@ List<String> handlePatchFiles(Path tmpDir, Path tmpPatchesDir) throws Exception
184191
* @throws IOException in case of error
185192
*/
186193
private List<String> handleWDTArgsIfRequired(Path tmpDir) throws IOException {
194+
logger.finer("Entering CreateImage.handleWDTArgsIfRequired: " + tmpDir.toAbsolutePath().toString());
187195
List<String> retVal = new LinkedList<>();
188196
String tmpDirPath = tmpDir.toAbsolutePath().toString();
189197
if (wdtModelPath != null) {
@@ -231,6 +239,7 @@ private List<String> handleWDTArgsIfRequired(Path tmpDir) throws IOException {
231239
throw new IOException("WDT model file " + wdtModelPath + " not found");
232240
}
233241
}
242+
logger.finer("Exiting CreateImage.handleWDTArgsIfRequired: " );
234243
return retVal;
235244
}
236245

@@ -242,6 +251,7 @@ private List<String> handleWDTArgsIfRequired(Path tmpDir) throws IOException {
242251
* @throws IOException in case of error
243252
*/
244253
private List<String> getWDTRequiredBuildArgs(Path wdtVariablesPath) throws IOException {
254+
logger.finer("Entering CreateImage.getWDTRequiredBuildArgs: " + wdtVariablesPath.toAbsolutePath().toString());
245255
List<String> retVal = new LinkedList<>();
246256
Properties variableProps = new Properties();
247257
variableProps.load(new FileInputStream(wdtVariablesPath.toFile()));
@@ -253,6 +263,7 @@ private List<String> getWDTRequiredBuildArgs(Path wdtVariablesPath) throws IOExc
253263
retVal.add(Constants.BUILD_ARG);
254264
retVal.add(((String) x).toUpperCase() + "=" + variableProps.getProperty((String) x));
255265
});
266+
logger.finer("Exiting CreateImage.getWDTRequiredBuildArgs: ");
256267
return retVal;
257268
}
258269

@@ -264,6 +275,7 @@ private List<String> getWDTRequiredBuildArgs(Path wdtVariablesPath) throws IOExc
264275
* @throws Exception in case of error
265276
*/
266277
private List<InstallerFile> gatherRequiredInstallers() throws Exception {
278+
logger.finer("Entering CreateImage.gatherRequiredInstallers: ");
267279
List<InstallerFile> retVal = new LinkedList<>();
268280
if (wdtModelPath != null && Files.isRegularFile(wdtModelPath)) {
269281
InstallerFile wdtInstaller = new InstallerFile(useCache, InstallerType.WDT, wdtVersion, null, null);
@@ -273,6 +285,7 @@ private List<InstallerFile> gatherRequiredInstallers() throws Exception {
273285
retVal.add(new InstallerFile(useCache, InstallerType.fromValue(installerType.toString()), installerVersion,
274286
userId, password));
275287
retVal.add(new InstallerFile(useCache, InstallerType.JDK, jdkVersion, userId, password));
288+
logger.finer("Exiting CreateImage.gatherRequiredInstallers: ");
276289
return retVal;
277290
}
278291

@@ -283,6 +296,7 @@ private List<InstallerFile> gatherRequiredInstallers() throws Exception {
283296
* @throws Exception in case of error
284297
*/
285298
private void addWDTURL(String wdtKey) throws Exception {
299+
logger.finer("Entering CreateImage.wdtKey: ");
286300
String wdtURLKey = wdtKey + "_url";
287301
if (cacheStore.getValueFromCache(wdtKey) == null) {
288302
if (useCache == CachePolicy.ALWAYS) {
@@ -298,6 +312,7 @@ private void addWDTURL(String wdtKey) throws Exception {
298312
throw new Exception("Couldn't find WDT download url for version:" + wdtVersion);
299313
}
300314
}
315+
logger.finer("Exiting CreateImage.wdtKey: ");
301316
}
302317

303318
/**

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/ImageOperation.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import com.oracle.weblogic.imagetool.util.ARUUtil;
1515
import com.oracle.weblogic.imagetool.util.Constants;
1616
import com.oracle.weblogic.imagetool.util.Utils;
17-
import picocli.CommandLine.Option;
18-
import picocli.CommandLine.Unmatched;
1917

2018
import java.io.File;
2119
import java.io.IOException;
@@ -26,15 +24,15 @@
2624
import java.util.LinkedList;
2725
import java.util.List;
2826
import java.util.concurrent.Callable;
29-
import java.util.logging.FileHandler;
30-
import java.util.logging.Level;
3127
import java.util.logging.Logger;
32-
import java.util.logging.SimpleFormatter;
3328
import java.util.regex.Matcher;
3429
import java.util.regex.Pattern;
3530
import java.util.stream.Collectors;
3631
import java.util.stream.Stream;
3732

33+
import picocli.CommandLine.Option;
34+
import picocli.CommandLine.Unmatched;
35+
3836
public abstract class ImageOperation implements Callable<CommandResponse> {
3937

4038
private final Logger logger = Logger.getLogger(ImageOperation.class.getName());
@@ -53,6 +51,7 @@ public abstract class ImageOperation implements Callable<CommandResponse> {
5351

5452
@Override
5553
public CommandResponse call() throws Exception {
54+
logger.finer("Entering ImageOperation call ");
5655
handleProxyUrls();
5756
password = handlePasswordOptions();
5857
// check user support credentials if useCache not set to always and we are applying any patches
@@ -64,6 +63,7 @@ public CommandResponse call() throws Exception {
6463
return new CommandResponse(-1, "user Oracle support credentials do not match");
6564
}
6665
}
66+
logger.finer("Exiting ImageOperation call ");
6767
return new CommandResponse(0, null);
6868
}
6969

@@ -87,6 +87,7 @@ private String handlePasswordOptions() throws IOException {
8787
* @throws Exception in case of error
8888
*/
8989
List<String> handlePatchFiles(Path tmpDir, Path tmpPatchesDir) throws Exception {
90+
logger.finer("Entering ImageOperation.handlePatchFiles");
9091
List<String> retVal = new LinkedList<>();
9192
List<String> patchLocations = new LinkedList<>();
9293
String toPatchesPath = tmpPatchesDir.toAbsolutePath().toString();
@@ -125,6 +126,7 @@ List<String> handlePatchFiles(Path tmpDir, Path tmpPatchesDir) throws Exception
125126
filterStartTags.add("UPDATE_PATCH_");
126127
}
127128
}
129+
logger.finer("Exiting ImageOperation.handlePatchFiles");
128130
return retVal;
129131
}
130132

@@ -135,6 +137,7 @@ List<String> handlePatchFiles(Path tmpDir, Path tmpPatchesDir) throws Exception
135137
*/
136138
List<String> getInitialBuildCmd() {
137139

140+
logger.finer("Entering ImageOperation.getInitialBuildCmd");
138141
List<String> cmdBuilder = Stream.of("docker", "build",
139142
"--force-rm", "--rm=true", "--no-cache").collect(Collectors.toList());
140143

@@ -159,6 +162,7 @@ List<String> getInitialBuildCmd() {
159162
if (dockerPath != null && Files.isExecutable(dockerPath)) {
160163
cmdBuilder.set(0, dockerPath.toAbsolutePath().toString());
161164
}
165+
logger.finer("Exiting ImageOperation.getInitialBuildCmd");
162166
return cmdBuilder;
163167
}
164168

@@ -180,17 +184,6 @@ void addOPatch1394ToImage(Path tmpDir) throws Exception {
180184
}
181185
}
182186

183-
/**
184-
* Enable logging when using cli mode and required log file path is supplied
185-
*
186-
* @param isCLIMode whether tool is run in cli mode
187-
*/
188-
void setupLogger(boolean isCLIMode) {
189-
if (!isCLIMode) {
190-
logger.setLevel(Level.OFF);
191-
}
192-
}
193-
194187
WLSInstallerType installerType = WLSInstallerType.WLS;
195188

196189
String installerVersion = Constants.DEFAULT_WLS_VERSION;

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/UpdateImage.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public UpdateImage(boolean isCLIMode) {
4848

4949
@Override
5050
public CommandResponse call() throws Exception {
51+
logger.finer("Entering UpdateImage.call ");
5152
Instant startTime = Instant.now();
5253

53-
setupLogger(isCLIMode);
5454
Path tmpDir = null;
5555
Path tmpDir2 = null;
5656

@@ -103,6 +103,7 @@ public CommandResponse call() throws Exception {
103103
baseImageProperties.keySet().forEach(x -> logger.info(x + "=" + baseImageProperties.getProperty(x.toString())));
104104

105105
if (latestPSU || !patches.isEmpty()) {
106+
logger.finer("Verifying Patches to WLS ");
106107
if (useCache == CachePolicy.ALWAYS) {
107108
logger.warning("skipping patch conflict check. useCache set to " + useCache);
108109
} else {
@@ -155,6 +156,8 @@ public CommandResponse call() throws Exception {
155156
Utils.deleteFilesRecursively(tmpDir2);
156157
}
157158
Instant endTime = Instant.now();
159+
logger.finer("Exiting UpdateImage.call ");
160+
158161
return new CommandResponse(0, "build successful in " + Duration.between(startTime, endTime).getSeconds() + "s. image tag: " + imageTag);
159162
}
160163

imagetool/src/main/java/com/oracle/weblogic/imagetool/util/ARUUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public static List<String> getPatchesFor(String category, String version, List<S
176176
public static ValidationResult validatePatches(String lsInventoryPath, List<String> patches, String category,
177177
String version, String userId, String password) throws IOException {
178178

179-
179+
logger.finer("Entering ARUUtil.validatePatches");
180180
ValidationResult validationResult = new ValidationResult();
181181
validationResult.setSuccess(true);
182182
validationResult.setResults(null);
@@ -230,8 +230,8 @@ public static ValidationResult validatePatches(String lsInventoryPath, List<Stri
230230
}
231231
payload.append("</conflict_check_request>");
232232

233+
logger.finer("Posting to ARU conflict check");
233234
Document result = HttpUtil.postCheckConflictRequest(Constants.CONFLICTCHECKER_URL, payload.toString(), userId, password);
234-
235235
try {
236236
NodeList conflictSets = XPathUtil.applyXPathReturnNodeList(result, "/conflict_check/conflict_sets/set");
237237
if (conflictSets.getLength() > 0) {
@@ -269,6 +269,7 @@ public static ValidationResult validatePatches(String lsInventoryPath, List<Stri
269269
throw new IOException(xpe);
270270

271271
}
272+
logger.finer("Exiting ARUUtil.validatePatches");
272273
return validationResult;
273274
}
274275

imagetool/src/main/java/com/oracle/weblogic/imagetool/util/HttpUtil.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
*/
55
package com.oracle.weblogic.imagetool.util;
66

7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.io.StringReader;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.logging.Logger;
13+
14+
import javax.xml.parsers.DocumentBuilder;
15+
import javax.xml.parsers.DocumentBuilderFactory;
16+
import javax.xml.parsers.ParserConfigurationException;
17+
718
import org.apache.http.HttpEntity;
819
import org.apache.http.auth.AuthScope;
920
import org.apache.http.auth.UsernamePasswordCredentials;
@@ -27,17 +38,10 @@
2738
import org.xml.sax.InputSource;
2839
import org.xml.sax.SAXException;
2940

30-
import javax.xml.parsers.DocumentBuilder;
31-
import javax.xml.parsers.DocumentBuilderFactory;
32-
import javax.xml.parsers.ParserConfigurationException;
33-
import java.io.File;
34-
import java.io.IOException;
35-
import java.io.StringReader;
36-
import java.util.ArrayList;
37-
import java.util.List;
38-
3941
public class HttpUtil {
4042

43+
private static final Logger logger = Logger.getLogger(HttpUtil.class.getName());
44+
4145
/**
4246
* Return the xml result of a GET from the url
4347
*
@@ -48,6 +52,8 @@ public class HttpUtil {
4852
* @throws IOException when it fails to access the url
4953
*/
5054
public static Document getXMLContent(String url, String username, String password) throws IOException {
55+
56+
logger.finest("HTTPUtil.getXMLContent " + url);
5157
String xmlString = Executor.newInstance(getOraClient(username, password))
5258
.execute(Request.Get(url).connectTimeout(30000).socketTimeout(30000))
5359
.returnContent().asString();
@@ -56,7 +62,7 @@ public static Document getXMLContent(String url, String username, String passwor
5662
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
5763
InputSource is = new InputSource(new StringReader(xmlString));
5864
Document doc = docBuilder.parse(is);
59-
// XPathUtil.prettyPrint(doc);
65+
logger.finest("HTTPUtil.getXMLContent result " + XPathUtil.prettyPrint(doc));
6066
return doc;
6167
} catch (ParserConfigurationException ex) {
6268
throw new IllegalStateException(ex);
@@ -117,6 +123,8 @@ public static void downloadFile(String url, String fileName, String username, St
117123

118124
public static Document postCheckConflictRequest(String url, String payload, String username, String password)
119125
throws IOException {
126+
127+
logger.finest("HTTPUtil.postCheckConflictRequest: " + payload);
120128
RequestConfig.Builder config = RequestConfig.custom();
121129
config.setCircularRedirectsAllowed(true);
122130
config.setRedirectsEnabled(true);
@@ -144,13 +152,13 @@ public static Document postCheckConflictRequest(String url, String payload, Stri
144152
String xmlString =
145153
httpExecutor.execute(Request.Post(url).connectTimeout(30000).socketTimeout(30000).body(entity))
146154
.returnContent().asString();
147-
155+
logger.finest("Returned Raw result " + xmlString);
148156
try {
149157
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
150158
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
151159
InputSource is = new InputSource(new StringReader(xmlString));
152160
Document doc = docBuilder.parse(is);
153-
// XPathUtil.prettyPrint(doc);
161+
logger.finest("HTTPUtil.postCheckConflictRequest result " + XPathUtil.prettyPrint(doc));
154162
return doc;
155163
} catch (ParserConfigurationException ex) {
156164
throw new IllegalStateException(ex);

0 commit comments

Comments
 (0)