From fd51c7259a0bcfffb51c3215035d4fe8a0411150 Mon Sep 17 00:00:00 2001 From: Victor Gdalevich Date: Wed, 16 Sep 2015 14:36:28 -0400 Subject: [PATCH] Add logconfig option to write logs to specified file Signed-off-by: Victor Gdalevich spacing spacing spacing --- .../docker/maven/AbstractDockerMojo.java | 7 +++- .../docker/maven/config/LogConfiguration.java | 23 ++++++++-- .../maven/log/ContainerLogOutputSpec.java | 18 ++++++-- .../docker/maven/log/LogDispatcher.java | 42 +++++++++++++------ 4 files changed, 68 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/jolokia/docker/maven/AbstractDockerMojo.java b/src/main/java/org/jolokia/docker/maven/AbstractDockerMojo.java index f9c95c1cb..c03d325dc 100644 --- a/src/main/java/org/jolokia/docker/maven/AbstractDockerMojo.java +++ b/src/main/java/org/jolokia/docker/maven/AbstractDockerMojo.java @@ -290,7 +290,6 @@ protected LogDispatcher getLogDispatcher(DockerAccess docker) { LogDispatcher dispatcher = (LogDispatcher) getPluginContext().get(CONTEXT_KEY_LOG_DISPATCHER); if (dispatcher == null) { dispatcher = new LogDispatcher(docker, useColor); - dispatcher.addLogOutputStream(System.out); getPluginContext().put(CONTEXT_KEY_LOG_DISPATCHER, dispatcher); } return dispatcher; @@ -302,6 +301,7 @@ protected ContainerLogOutputSpec getContainerLogSpec(String containerId, ImageCo addLogFormat(builder, logConfig); addPrefix(builder, logConfig.getPrefix(), imageConfiguration.getAlias(), containerId); + addLogFile(builder, logConfig.getFileLocation()); builder.containerId(containerId) .color(logConfig.getColor()); @@ -330,6 +330,11 @@ private void addLogFormat(ContainerLogOutputSpec.Builder builder, LogConfigurati } } + private void addLogFile(ContainerLogOutputSpec.Builder builder, String logFile) { + String file = logFile; + builder.file(file); + } + private LogConfiguration extractLogConfiguration(ImageConfiguration imageConfiguration) { RunImageConfiguration runConfig = imageConfiguration.getRunConfiguration(); LogConfiguration logConfig = null; diff --git a/src/main/java/org/jolokia/docker/maven/config/LogConfiguration.java b/src/main/java/org/jolokia/docker/maven/config/LogConfiguration.java index a3bddca8e..4d10789d1 100644 --- a/src/main/java/org/jolokia/docker/maven/config/LogConfiguration.java +++ b/src/main/java/org/jolokia/docker/maven/config/LogConfiguration.java @@ -6,7 +6,7 @@ */ public class LogConfiguration { - public static final LogConfiguration DEFAULT = new LogConfiguration(false, null, null, null); + public static final LogConfiguration DEFAULT = new LogConfiguration(false, null, null, null, null); /** * @parameter default-value="true" @@ -28,13 +28,19 @@ public class LogConfiguration { */ private String color; + /** + * @parameter + */ + private String file; + public LogConfiguration() {} - private LogConfiguration(boolean enabled, String prefix, String color, String date) { + private LogConfiguration(boolean enabled, String prefix, String color, String date, String file) { this.enabled = enabled; this.prefix = prefix; this.date = date; this.color = color; + this.file = file; } public String getPrefix() { @@ -53,11 +59,15 @@ public boolean isEnabled() { return enabled; } + public String getFileLocation() { + return file; + } + // ============================================================================= public static class Builder { private boolean enabled = true; - private String prefix, timestamp, color; + private String prefix, timestamp, color, file; public Builder enabled(boolean enabled) { this.enabled = enabled; @@ -79,8 +89,13 @@ public Builder color(String color) { return this; } + public Builder file(String file) { + this.file = file; + return this; + } + public LogConfiguration build() { - return new LogConfiguration(enabled, prefix, color, timestamp); + return new LogConfiguration(enabled, prefix, color, timestamp, file); } } } diff --git a/src/main/java/org/jolokia/docker/maven/log/ContainerLogOutputSpec.java b/src/main/java/org/jolokia/docker/maven/log/ContainerLogOutputSpec.java index 82c547da7..7feca30eb 100644 --- a/src/main/java/org/jolokia/docker/maven/log/ContainerLogOutputSpec.java +++ b/src/main/java/org/jolokia/docker/maven/log/ContainerLogOutputSpec.java @@ -28,13 +28,13 @@ */ public class ContainerLogOutputSpec { - public static final ContainerLogOutputSpec DEFAULT = new ContainerLogOutputSpec("", YELLOW, null,null); + public static final ContainerLogOutputSpec DEFAULT = new ContainerLogOutputSpec("", YELLOW, null,null,null); private final String containerId; private String prefix; private Ansi.Color color; private DateTimeFormatter timeFormatter; - + private String file; // Palette used for prefixing the log output private final static Ansi.Color COLOR_PALETTE[] = { @@ -45,11 +45,12 @@ public class ContainerLogOutputSpec { //Fill prefix up to this length private String FILLER = " "; - private ContainerLogOutputSpec(String prefix, Ansi.Color color, DateTimeFormatter timeFormatter, String containerId) { + private ContainerLogOutputSpec(String prefix, Ansi.Color color, DateTimeFormatter timeFormatter, String containerId, String file) { this.prefix = prefix; this.color = color; this.containerId = containerId; this.timeFormatter = timeFormatter; + this.file = file; } public String getContainerId() { @@ -61,6 +62,10 @@ public String getPrompt(boolean withColor,Timestamp timestamp) { return formatTimestamp(timestamp,withColor) + formatPrefix(prefix, withColor) + "> "; } + public String getFile(){ + return file; + } + private String formatTimestamp(Timestamp timestamp,boolean withColor) { if (timeFormatter == null) { return ""; @@ -82,6 +87,7 @@ public static class Builder { private Ansi.Color color; private String containerId; private DateTimeFormatter timeFormatter; + private String file; public Builder prefix(String prefix) { this.prefix = prefix; @@ -102,6 +108,10 @@ public Builder color(String color) { } return this; } + public Builder file(String file){ + this.file = file; + return this; + } public Builder timeFormatter(String formatOrConstant) { if (formatOrConstant == null || formatOrConstant.equalsIgnoreCase("NONE") @@ -137,7 +147,7 @@ public Builder containerId(String id) { } public ContainerLogOutputSpec build() { - return new ContainerLogOutputSpec(prefix,color,timeFormatter,containerId); + return new ContainerLogOutputSpec(prefix,color,timeFormatter,containerId, file); } } diff --git a/src/main/java/org/jolokia/docker/maven/log/LogDispatcher.java b/src/main/java/org/jolokia/docker/maven/log/LogDispatcher.java index e51291fb4..6a49caf09 100644 --- a/src/main/java/org/jolokia/docker/maven/log/LogDispatcher.java +++ b/src/main/java/org/jolokia/docker/maven/log/LogDispatcher.java @@ -15,6 +15,8 @@ * limitations under the License. */ +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.PrintStream; import java.util.*; @@ -33,7 +35,7 @@ public class LogDispatcher { private Map outputSpecs; private Map logHandles; - private List printStreams; + private HashMap printStreamsMap; private DockerAccess dockerAccess; @@ -41,15 +43,25 @@ public LogDispatcher(DockerAccess dockerAccess,boolean withColor) { this.dockerAccess = dockerAccess; this.withColor = withColor; outputSpecs = new HashMap<>(); - printStreams = new ArrayList<>(); + printStreamsMap = new HashMap(); logHandles = new HashMap<>(); } - public synchronized void addLogOutputStream(PrintStream out) { - printStreams.add(out); + public void addLogOutputStreams(String id, ContainerLogOutputSpec spec){ + String logFile = spec.getFile(); + try { + if (logFile == null) { + printStreamsMap.put(id, System.out); + } else { + printStreamsMap.put(id, new PrintStream(new FileOutputStream(logFile), true)); + } + }catch (FileNotFoundException e) { + e.printStackTrace(); + } } public synchronized void trackContainerLog(String id, ContainerLogOutputSpec spec) { + addLogOutputStreams(id, spec); outputSpecs.put(id, spec); LogGetHandle handle = dockerAccess.getLogAsync(id, createLogCallBack(id)); @@ -57,8 +69,8 @@ public synchronized void trackContainerLog(String id, ContainerLogOutputSpec spe } public synchronized void fetchContainerLog(String id, ContainerLogOutputSpec spec) { + addLogOutputStreams(id, spec); outputSpecs.put(id, spec); - dockerAccess.getLogSync(id,createLogCallBack(id)); } @@ -71,7 +83,7 @@ public void log(int type, Timestamp timestamp, String txt) { @Override public void error(String error) { - printError(error); + printError(id, error); } }; } @@ -86,18 +98,22 @@ private void addLogEntry(LogEntry logEntry) { } // FIX me according to spec - print(spec.getPrompt(withColor,logEntry.getTimestamp()) + logEntry.getText()); + print(spec.getContainerId(), spec.getPrompt(withColor,logEntry.getTimestamp()) + logEntry.getText()); } - private void printError(String e) { - for (PrintStream ps : printStreams) { - ps.println(e); + private void printError(String id, String e) { + for (String containerId : printStreamsMap.keySet() ){ + if (containerId == id){ + printStreamsMap.get(id).println(e); + } } } - private void print(String line) { - for (PrintStream ps : printStreams) { - ps.println(line); + private void print(String id, String line) { + for (String containerId : printStreamsMap.keySet() ){ + if (containerId == id) { + printStreamsMap.get(id).println(line); + } } }