Skip to content

Commit 0dcdb31

Browse files
jshum2479ddsharpe
authored andcommitted
Add logging (#8)
* add logging framework pass1 * switch to standard java logging config pattern
1 parent c17effd commit 0dcdb31

File tree

8 files changed

+51
-39
lines changed

8 files changed

+51
-39
lines changed

imagetool/src/assembly/zip.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<outputDirectory>bin</outputDirectory>
1717
<includes>
1818
<include>*.sh</include>
19+
<include>logging.properties</include>
1920
</includes>
2021
<fileMode>0750</fileMode>
2122
<lineEnding>unix</lineEnding>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
handlers=java.util.logging.ConsoleHandler
2+
# Uncomment to enable file logging
3+
#handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
4+
#
5+
# Change log file location as needed
6+
java.util.logging.FileHandler.pattern=tool.log
7+
java.util.logging.FileHandler.limit=50000
8+
java.util.logging.FileHandler.count=1
9+
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
10+
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+

imagetool/src/main/bin/setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ unalias imagetool 2> /dev/null
4848
script_dir=$( dirname "$( read_link "${BASH_SOURCE[0]}" )" )
4949
IMAGETOOL_HOME=`cd "${script_dir}/.." ; pwd`
5050
export IMAGETOOL_HOME
51-
alias imagetool="${JAVA_HOME}/bin/java -cp \"${IMAGETOOL_HOME}/lib/*\" com.oracle.weblogic.imagetool.cli.CLIDriver"
51+
alias imagetool="${JAVA_HOME}/bin/java -cp \"${IMAGETOOL_HOME}/lib/*\" -Djava.util.logging.config.file=${IMAGETOOL_HOME}/bin/logging.properties com.oracle.weblogic.imagetool.cli.CLIDriver"
5252
source ${IMAGETOOL_HOME}/lib/imagetool_completion.sh

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public CommandResponse call() throws Exception {
5454

5555
Instant startTime = Instant.now();
5656

57-
FileHandler fileHandler = setupLogger(isCLIMode);
57+
setupLogger(isCLIMode);
5858
Path tmpDir = null;
5959
Path tmpDir2 = null;
6060

@@ -134,10 +134,6 @@ public CommandResponse call() throws Exception {
134134
} finally {
135135
Utils.deleteFilesRecursively(tmpDir);
136136
Utils.deleteFilesRecursively(tmpDir2);
137-
if (fileHandler != null) {
138-
fileHandler.close();
139-
logger.removeHandler(fileHandler);
140-
}
141137
}
142138
Instant endTime = Instant.now();
143139
return new CommandResponse(0, "build successful in " + Duration.between(startTime, endTime).getSeconds() + "s. image tag: " + imageTag);

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

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -184,27 +184,11 @@ void addOPatch1394ToImage(Path tmpDir) throws Exception {
184184
* Enable logging when using cli mode and required log file path is supplied
185185
*
186186
* @param isCLIMode whether tool is run in cli mode
187-
* @return log file handler or null
188187
*/
189-
FileHandler setupLogger(boolean isCLIMode) {
190-
FileHandler fileHandler = null;
191-
try {
192-
if (isCLIMode) {
193-
Path toolLogPath = Utils.createFile(toolLog, "tool.log");
194-
if (toolLogPath != null) {
195-
fileHandler = new FileHandler(toolLogPath.toAbsolutePath().toString());
196-
fileHandler.setFormatter(new SimpleFormatter());
197-
logger.addHandler(fileHandler);
198-
logger.setLevel(Level.INFO);
199-
}
200-
} else {
201-
logger.setLevel(Level.OFF);
202-
}
203-
} catch (IOException e) {
204-
//suppress exception
205-
fileHandler = null;
188+
void setupLogger(boolean isCLIMode) {
189+
if (!isCLIMode) {
190+
logger.setLevel(Level.OFF);
206191
}
207-
return fileHandler;
208192
}
209193

210194
WLSInstallerType installerType = WLSInstallerType.WLS;
@@ -298,13 +282,6 @@ FileHandler setupLogger(boolean isCLIMode) {
298282
)
299283
Path dockerLog;
300284

301-
@Option(
302-
names = {"--toolLog"},
303-
description = "file to log output from this tool. This is different from the docker build log.",
304-
hidden = true
305-
)
306-
private Path toolLog;
307-
308285
@Unmatched
309286
List<String> unmatchedOptions;
310287
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public UpdateImage(boolean isCLIMode) {
5050
public CommandResponse call() throws Exception {
5151
Instant startTime = Instant.now();
5252

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

@@ -153,10 +153,6 @@ public CommandResponse call() throws Exception {
153153
} finally {
154154
Utils.deleteFilesRecursively(tmpDir);
155155
Utils.deleteFilesRecursively(tmpDir2);
156-
if (fileHandler != null) {
157-
fileHandler.close();
158-
logger.removeHandler(fileHandler);
159-
}
160156
}
161157
Instant endTime = Instant.now();
162158
return new CommandResponse(0, "build successful in " + Duration.between(startTime, endTime).getSeconds() + "s. image tag: " + imageTag);

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,12 @@ public static Path createFile(Path filePath, String defaultFileName) {
8282
} else {
8383
if (Files.isDirectory(logFilePath)) {
8484
if (defaultFileName == null || defaultFileName.isEmpty()) {
85-
defaultFileName = "log.log";
85+
defaultFileName = "tool.log";
8686
}
8787
logFilePath = Paths.get(logFilePath.toAbsolutePath().toString(), defaultFileName);
88+
if (Files.exists(logFilePath)) {
89+
Files.delete(logFilePath);
90+
}
8891
Files.createFile(logFilePath);
8992
}
9093
}
@@ -171,7 +174,9 @@ private static Map<String, String> readPlaceHolderResource(String resourcePath,
171174
String endTag = matcher.group(4);
172175
if (startTag.equals(endTag) && desiredPrefixes.stream().anyMatch(
173176
x -> startTag.startsWith(x) || extraIdentifier.equalsIgnoreCase(x))) {
174-
retMap.put(startTag, content);
177+
// this matching patten leaves LF in beginning and end
178+
int len = content.length()-2;
179+
retMap.put(startTag, content.substring(1).substring(0,len) );
175180
}
176181
} else {
177182
logger.fine("pattern mismatch in resource " + resourcePath);

tool.log

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[2019-05-21 22:40:56] [com.oracle.weblogic.imagetool.cli.menu.CreateImage] [INFO ] tmp directory used for build context: /home/johnny/wlsimgbuilder_temp256175790702709505
2+
[2019-05-21 22:41:06] [com.oracle.weblogic.imagetool.util.Utils] [FINEST ] Putting COPY --chown=oracle:oracle p28186730_139400_Generic.zip $OTMPDIR/opatch/
3+
[2019-05-21 22:41:06] [com.oracle.weblogic.imagetool.util.Utils] [FINEST ] Putting && cd $OTMPDIR/opatch \
4+
&& $JAVA_HOME/bin/jar -xf $OTMPDIR/opatch/p28186730_139400_Generic.zip \
5+
&& $JAVA_HOME/bin/java -jar $OTMPDIR/opatch/6880880/opatch_generic.jar -silent oracle_home=$ORACLE_HOME \
6+
[2019-05-21 22:41:06] [com.oracle.weblogic.imagetool.util.Utils] [FINEST ] Putting COPY --chown=oracle:oracle $PATCHDIR/* $OTMPDIR/patches/
7+
[2019-05-21 22:41:06] [com.oracle.weblogic.imagetool.util.Utils] [FINEST ] Putting && $ORACLE_HOME/OPatch/opatch napply -silent -oh $ORACLE_HOME -phBaseDir $OTMPDIR/patches \
8+
&& $ORACLE_HOME/OPatch/opatch util cleanup -silent -oh $ORACLE_HOME \
9+
[2019-05-21 22:41:06] [com.oracle.weblogic.imagetool.util.Utils] [FINEST ] Putting RUN yum -y --downloaddir=$OTMPDIR install gzip tar unzip \
10+
&& yum -y --downloaddir=$OTMPDIR clean all \
11+
&& rm -rf $OTMPDIR
12+
[2019-05-21 22:41:06] [com.oracle.weblogic.imagetool.util.Utils] [FINEST ] Putting RUN yum -y --downloaddir=$OTMPDIR update \
13+
&& yum -y --downloaddir=$OTMPDIR clean all \
14+
&& rm -rf $OTMPDIR
15+
[2019-05-21 22:41:06] [com.oracle.weblogic.imagetool.cli.menu.CreateImage] [INFO ] docker cmd = docker build --force-rm --rm=true --no-cache --tag sample:wls --build-arg http_proxy=http://www-proxy.us.oracle.com:80 --build-arg https_proxy=http://www-proxy.us.oracle.com:80 --build-arg WLS_PKG=fmw_12.2.1.3.0_wls_Disk1_1of1.zip --build-arg JAVA_PKG=jdk-8u201-linux-x64.tar.gz --build-arg PATCHDIR=patches /home/johnny/wlsimgbuilder_temp256175790702709505

0 commit comments

Comments
 (0)