Skip to content

Commit

Permalink
Handle log file not found case
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Aug 24, 2021
1 parent 478eabb commit 5e6b3f1
Showing 1 changed file with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,40 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class LogRedirect {

public static void bootRedirectToFile(String name) throws IOException {
public static void bootRedirectToFile(String name) {
String logfilePath = System.getProperty("sts.log.file");
if (StringUtil.hasText(logfilePath)) {
PrintStream logFile = logFileStream(logfilePath);
System.setErr(logFile);
try {
PrintStream logFile = logFileStream(logfilePath);
System.setErr(logFile);
} catch (FileNotFoundException e) {
System.err.println(e);
System.err.println("Log file '" + logfilePath + "' cannot be found. Ensure that all folders in the path exist!");
System.setErr(switchOffLogging());
}
}
}

private static PrintStream logFileStream(String logfilePath) throws FileNotFoundException {
if (logfilePath.equals("/dev/null")) {
System.err.println("Disabling server log output. No more output will be sent after this.");
//redirect to a file called "/dev/null" works fine in Unix, but we also want this
// to work on Mac and Windows. So we create our own '/dev/null' stream
return new PrintStream(new NullOutputStream());
return switchOffLogging();
} else {
System.err.println("Redirecting log output to: "+logfilePath);
File logfile = new File(logfilePath);
PrintStream logFile = new PrintStream(new FileOutputStream(logfile, false));
return logFile;
}
}

public static void redirectToFile(String name) throws IOException {
String logfilePath = System.getProperty("sts.log.file");
if (StringUtil.hasText(logfilePath)) {
PrintStream logFile = logFileStream(logfilePath);
System.setErr(logFile);
}
}

private static PrintStream switchOffLogging() {
System.err.println("Disabling server log output. No more output will be sent after this.");
//redirect to a file called "/dev/null" works fine in Unix, but we also want this
// to work on Mac and Windows. So we create our own '/dev/null' stream
return new PrintStream(new NullOutputStream());
}

}

0 comments on commit 5e6b3f1

Please sign in to comment.