java -jar mein.jar >stdout.txt 2>stderr.txt
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class DualOutput {
public static void main(String[] args) throws IOException {
FileOutputStream fileOut = new FileOutputStream("output.log");
PrintStream dualOut = new PrintStream(new DualStream(System.out, new PrintStream(fileOut, true)));
PrintStream dualErr = new PrintStream(new DualStream(System.err, new PrintStream(fileOut, true)));
System.setOut(dualOut);
System.setErr(dualErr);
System.out.println("Dies ist eine Nachricht auf System.out");
System.err.println("Dies ist eine Nachricht auf System.err");
}
public static class DualStream extends PrintStream {
private final PrintStream stream1;
private final PrintStream stream2;
public DualStream(PrintStream stream1, PrintStream stream2) {
super(stream1);
this.stream1 = stream1;
this.stream2 = stream2;
}
@Override
public void write(int b) {
stream1.write(b);
stream2.write(b);
}
@Override
public void write(byte[] buf, int off, int len) {
stream1.write(buf, off, len);
stream2.write(buf, off, len);
}
@Override
public void flush() {
stream1.flush();
stream2.flush();
}
@Override
public void close() {
stream1.close();
stream2.close();
}
}
}
und eine Logging-Library zu verwenden.
Eine Logging-Library ist nicht unbedingt notwendig. Java selbst stellt auch Logging-Funktionen zu Verfügung.und eine Logging-Library zu verwenden.
package home.playground;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class Main {
public static void main(final String[] args) {
final Logger logger = initLoggerGlobal();
final LogTester tester = new LogTester();
try {
tester.hello("Hello, World!");
tester.hello("Hello, Logging ...");
tester.hello(null); // cause exception
} catch (final IllegalArgumentException e) {
logger.severe("IllegalArgumentException: " + e.getMessage());
}
}
/**
* Initializes and configures the global logger to write messages to a file
* named "log.txt".
* The logger is set to handle all log levels (from FINEST to SEVERE).
*
* @return The configured global logger instance.
*/
private static Logger initLoggerGlobal() {
final Logger logger = Logger.getGlobal();
// File handler set to show all messages (from FINEST to SEVERE)
FileHandler fileHandler = null;
try {
fileHandler = new FileHandler("log.txt", true);
fileHandler.setLevel(Level.ALL);
fileHandler.setFormatter(new SimpleFormatter());
} catch (final Exception e) {
e.printStackTrace();
}
logger.addHandler(fileHandler);
logger.severe("This is a SEVERE error message");
logger.warning("This is a WARNING message");
logger.info("This is an INFO message");
logger.config("This is a CONFIG message");
logger.fine("This is a FINE debug message");
logger.finer("This is a FINER debug message (more detailed)");
logger.finest("This is a FINEST debug message (most detailed)");
return logger;
}
}
class LogTester {
final Logger logger = Logger.getLogger(this.getClass().getName());
/**
* This function is responsible for logging a message with the INFO level.
*
* @param message The message to be logged. It cannot be null.
*
* @throws IllegalArgumentException If the provided message is null.
*
* @return This function does not return any value.
*/
void hello(final String message) {
if (message == null) {
throw new IllegalArgumentException("Message cannot be null");
}
logger.info(message);
}
}