Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logconfig option to write logs to specified file #287

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand All @@ -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;
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand All @@ -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() {
Expand All @@ -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 "";
Expand All @@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -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);
}

}
Expand Down
42 changes: 29 additions & 13 deletions src/main/java/org/jolokia/docker/maven/log/LogDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.*;

Expand All @@ -33,32 +35,42 @@ public class LogDispatcher {
private Map<String,ContainerLogOutputSpec> outputSpecs;
private Map<String,LogGetHandle> logHandles;

private List<PrintStream> printStreams;
private HashMap<String, PrintStream> printStreamsMap;

private DockerAccess dockerAccess;

public LogDispatcher(DockerAccess dockerAccess,boolean withColor) {
this.dockerAccess = dockerAccess;
this.withColor = withColor;
outputSpecs = new HashMap<>();
printStreams = new ArrayList<>();
printStreamsMap = new HashMap<String, PrintStream>();
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));
logHandles.put(id, handle);
}

public synchronized void fetchContainerLog(String id, ContainerLogOutputSpec spec) {
addLogOutputStreams(id, spec);
outputSpecs.put(id, spec);

dockerAccess.getLogSync(id,createLogCallBack(id));
}

Expand All @@ -71,7 +83,7 @@ public void log(int type, Timestamp timestamp, String txt) {

@Override
public void error(String error) {
printError(error);
printError(id, error);
}
};
}
Expand All @@ -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);
}
}
}

Expand Down