I want to create a logger which logs all my messages sent to System.out.
For this I got to know the java Logger class.
But if I understand the purpose of this class, it only allows to write Log messages which are pre defined in the code.
So if I wan’t to create a System.out. logger I would have to create my own logger class.
String dateTime = new String (now("MM-dd-yy_HH!mm!ss"));
BufferedReader sysLogger = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter sysWriter = new BufferedWriter(new FileWriter(new File(currentDir+"/logs/commando/CmdLog_"+dateTime+".txt")));
String readLine = new String();
String lastLine = new String();
readLine = sysLogger.readLine();
while(iSysLog == 1){ //TODO
if(lastLine != readLine){
sysWriter.write(readLine);
lastLine = readLine;
}
readLine = sysLogger.readLine();
}
Why is this codenot working?
also: Is there a better way to achieve my goal?
Found the answer in:
Java – Capturing System.err.println or Capturing a PrintStream
Still: how can I set the System.out to the console AND the file?