-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#348 when java exec sh script, we should redirect the error stream to…
… log
- Loading branch information
Showing
4 changed files
with
107 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 29 additions & 61 deletions
90
saturn-core/src/main/java/com/vip/saturn/job/utils/SaturnLogOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,29 @@ | ||
package com.vip.saturn.job.utils; | ||
|
||
import java.io.PrintStream; | ||
|
||
import org.apache.commons.exec.LogOutputStream; | ||
|
||
/** | ||
* 捕获System.out输出 | ||
* @author dylan.xue | ||
* | ||
*/ | ||
public class SaturnLogOutputStream extends LogOutputStream { | ||
private static final int MAX_LINE = 100; | ||
|
||
private static ThreadLocal<LRUList<String>> lists = new InheritableThreadLocal<LRUList<String>>() { | ||
@Override | ||
protected LRUList<String> initialValue() { | ||
return new LRUList<String>(MAX_LINE); | ||
} | ||
}; | ||
private static PrintStream catchedOut = new PrintStream(new SaturnLogOutputStream(1)); | ||
private static PrintStream out = System.out; // NOSONAR | ||
|
||
static { | ||
System.setOut(catchedOut); | ||
} | ||
|
||
private SaturnLogOutputStream(int level) { | ||
super(level); | ||
} | ||
|
||
protected void processLine(String line, int level) { | ||
LRUList<String> lruList = (LRUList<String>) lists.get(); | ||
lruList.put(line); | ||
out.println(line); | ||
} | ||
|
||
public static void initLogger() { | ||
lists.get().clear(); | ||
} | ||
|
||
private static void clearCache() { | ||
lists.remove(); | ||
} | ||
|
||
public static String clearAndGetLog() { | ||
try { | ||
StringBuilder sb = new StringBuilder(); | ||
LRUList<String> lruList = (LRUList<String>) lists.get(); | ||
for (String line : lruList) { | ||
sb.append(line).append(System.lineSeparator()); | ||
} | ||
lruList.clear(); | ||
clearCache(); | ||
return sb.toString(); | ||
} catch (Exception e) {// NOSONAR | ||
return ""; | ||
} | ||
} | ||
|
||
} | ||
package com.vip.saturn.job.utils; | ||
|
||
import org.apache.commons.exec.LogOutputStream; | ||
import org.slf4j.Logger; | ||
|
||
/** | ||
* @author hebelala | ||
*/ | ||
public class SaturnLogOutputStream extends LogOutputStream { | ||
|
||
public static final int LEVEL_INFO = 1; | ||
public static final int LEVEL_ERROR = 2; | ||
|
||
private Logger log; | ||
|
||
public SaturnLogOutputStream(Logger log, int level) { | ||
super(level); | ||
this.log = log; | ||
} | ||
|
||
@Override | ||
protected void processLine(String line, int logLevel) { | ||
if (logLevel == LEVEL_INFO) { | ||
log.info(line); | ||
} else if (logLevel == LEVEL_ERROR) { | ||
log.error(line); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
saturn-core/src/main/java/com/vip/saturn/job/utils/SaturnSystemOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.vip.saturn.job.utils; | ||
|
||
import java.io.PrintStream; | ||
import org.apache.commons.exec.LogOutputStream; | ||
|
||
/** | ||
* 捕获System.out输出 | ||
* | ||
* @author dylan.xue | ||
*/ | ||
public class SaturnSystemOutputStream extends LogOutputStream { | ||
|
||
private static final int MAX_LINE = 100; | ||
|
||
private static ThreadLocal<LRUList<String>> lists = new InheritableThreadLocal<LRUList<String>>() { | ||
@Override | ||
protected LRUList<String> initialValue() { | ||
return new LRUList<String>(MAX_LINE); | ||
} | ||
}; | ||
private static PrintStream catchedOut = new PrintStream(new SaturnSystemOutputStream(1)); | ||
private static PrintStream out = System.out; // NOSONAR | ||
|
||
static { | ||
System.setOut(catchedOut); | ||
} | ||
|
||
private SaturnSystemOutputStream(int level) { | ||
super(level); | ||
} | ||
|
||
protected void processLine(String line, int level) { | ||
LRUList<String> lruList = (LRUList<String>) lists.get(); | ||
lruList.put(line); | ||
out.println(line); | ||
} | ||
|
||
public static void initLogger() { | ||
lists.get().clear(); | ||
} | ||
|
||
private static void clearCache() { | ||
lists.remove(); | ||
} | ||
|
||
public static String clearAndGetLog() { | ||
try { | ||
StringBuilder sb = new StringBuilder(); | ||
LRUList<String> lruList = (LRUList<String>) lists.get(); | ||
for (String line : lruList) { | ||
sb.append(line).append(System.lineSeparator()); | ||
} | ||
lruList.clear(); | ||
clearCache(); | ||
return sb.toString(); | ||
} catch (Exception e) {// NOSONAR | ||
return ""; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters