logging - Java custom logger issues -
i have custom logger
class:
import java.io.outputstream; import java.io.printstream; public class logger extends printstream{ public logger(outputstream outputstream) { super(outputstream); logheader(); } private void logheader(){ (int i=0; i<20; i++) print("#"); print("program execution start: "); print(calendarclock.getinittimestamp()); (int i=0; i<20; i++) print("#"); println(); } public void logfooter(){ (int i=0; i<=20; i++) print("#"); print("program execution stop: "); print(calendarclock.getcurrenttimestamp()); (int i=0; i<=20; i++) print("#"); println("\n"); } @override public void print(string arg0) { super.print(arg0); if (this.out != system.out) system.out.print(arg0); } @override public void println(string arg0) { super.println(arg0); if (this.out != system.out) system.out.println(arg0); } }
and here main
class:
public class main { @suppresswarnings("resource") public static void main(string[] args){ file logfile = new file("data/test.log"); logger logger; try{ if (!logfile.exists()) logfile.createnewfile(); logger = new logger(new fileoutputstream(logfile, false)); } catch (ioexception e) { logger = new logger(system.out); } logger.println("hello world."); logger.println("foo bar"); logger.logfooter(); logger.close(); } }
firstly, eclipse keeps displaying warning @ logger = new logger(system.out);
inside catch(ioexception e)
: resource leak: 'logger' not closed @ location
. have add @suppresswarnings("resource")
make disappear.
secondly program outputs following console:
####################program execution start: 2015-10-27 10:55:51####################hello world.hello world. foo barfoo bar #####################program execution stop: 2015-10-27 10:55:51#####################
while logs file correctly:
####################program execution start: 2015-10-27 10:55:51#################### hello world. foo bar #####################program execution stop: 2015-10-27 10:55:51#####################
lastly, if run following code:
public class main { @suppresswarnings("resource") public static void main(string[] args){ file logfile = new file("data/test.log"); logger logger; try{ throw new ioexception("threw exception!"); /*if (!logfile.exists()) logfile.createnewfile(); logger = new logger(new fileoutputstream(logfile, false));*/ } catch (ioexception e) { logger = new logger(system.out); logger.println(e.getmessage()); } logger.println("hello world."); logger.println("foo bar"); logger.logfooter(); logger.close(); } }
the output correctly printed as:
####################program execution start: 2015-10-27 10:59:18#################### threw exception! hello world. foo bar #####################program execution stop: 2015-10-27 10:59:18#####################
obviously, seems beyond understanding, , explanation of behavior appreciated.
update: solved issue of no new line @ end of header overriding null argument println
:
@override public void println() { super.println(); if (out != system.out) system.out.println(); }
now output of first main
is:
####################program execution start: 2015-10-27 11:15:30#################### hello world.hello world. foo barfoo bar #####################program execution stop: 2015-10-27 11:15:30#####################
i used debugger see sequence of code execution, , found printstream
class implements prinln(arg0)
call print(arg0)
, newline()
equivalent println()
. every time println(arg0)
called in logger
, arg0
printed stdout
twice, once in print(arg0)
, once in println(arg0)
.
to overcome problem, changed implementation of println(arg0)
in logger
class this:
@override public void println(string arg0) { print(arg0); println(); }
Comments
Post a Comment