mirror of
https://github.com/alibaba/arthas.git
synced 2026-11-30 07:32:57 +00:00
improve redirect. #495
This commit is contained in:
1 parent
793df05b2f
commit
9a57b5d8f3
2 files changed
+29
-16
No files matched your search
+26
-14
@@ -1,35 +1,47 @@
|
||||
package com.taobao.arthas.core.shell.command.internal;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import com.taobao.arthas.core.util.LogUtil;
|
||||
import com.taobao.middleware.logger.LoggerFactory;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* 重定向处理类
|
||||
*
|
||||
*
|
||||
* @author gehui 2017年7月27日 上午11:38:40
|
||||
* @author hengyunabc 2019-02-06
|
||||
*/
|
||||
public class RedirectHandler extends PlainTextHandler implements CloseFunction {
|
||||
|
||||
private Logger logger = null;
|
||||
private PrintWriter out;
|
||||
|
||||
public RedirectHandler() {
|
||||
|
||||
}
|
||||
|
||||
public RedirectHandler(String name) {
|
||||
com.taobao.middleware.logger.Logger log = LoggerFactory.getLogger(name);
|
||||
log.activateAppenderWithSizeRolling("arthas-cache", name, "UTF-8", "200MB", 3);
|
||||
log.setAdditivity(false);
|
||||
log.activateAsync(128, -1);
|
||||
logger = (Logger) log.getDelegate();
|
||||
public RedirectHandler(String name, boolean append) throws IOException {
|
||||
File file = new File(name);
|
||||
|
||||
if (file.isDirectory()) {
|
||||
throw new IOException(name + ": Is a directory");
|
||||
}
|
||||
|
||||
if (!file.exists()) {
|
||||
File parentFile = file.getParentFile();
|
||||
if (parentFile != null) {
|
||||
parentFile.mkdirs();
|
||||
}
|
||||
}
|
||||
out = new PrintWriter(new BufferedWriter(new FileWriter(file, append)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apply(String data) {
|
||||
data = super.apply(data);
|
||||
if (logger != null) {
|
||||
logger.info(data);
|
||||
if (out != null) {
|
||||
out.write(data);
|
||||
} else {
|
||||
LogUtil.getResultLogger().info(data);
|
||||
}
|
||||
@@ -38,6 +50,6 @@ public class RedirectHandler extends PlainTextHandler implements CloseFunction {
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
LogUtil.closeSlf4jLogger(logger);
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import com.taobao.arthas.core.util.TokenUtils;
|
||||
import io.termd.core.function.Function;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@@ -149,7 +150,7 @@ public class JobControllerImpl implements JobController {
|
||||
return runInBackground;
|
||||
}
|
||||
|
||||
private Process createCommandProcess(Command command, ListIterator<CliToken> tokens, int jobId, Term term) {
|
||||
private Process createCommandProcess(Command command, ListIterator<CliToken> tokens, int jobId, Term term) throws IOException {
|
||||
List<CliToken> remaining = new ArrayList<CliToken>();
|
||||
List<CliToken> pipelineTokens = new ArrayList<CliToken>();
|
||||
boolean isPipeline = false;
|
||||
@@ -176,7 +177,7 @@ public class JobControllerImpl implements JobController {
|
||||
throw new IllegalStateException("The amount of async command that saving result to file can't > 8");
|
||||
}
|
||||
}
|
||||
redirectHandler = new RedirectHandler(name);
|
||||
redirectHandler = new RedirectHandler(name, ">>".equals(tokenValue));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user