java - MemoryHandler not Pushing when application terminates -
i writing java server application using java.util.logging, memoryhandler.
how make memoryhandler push data out file when application terminates?
i tried add shutdownhook , manually push data file, appears log handlers removed when reaching "hook".
this code:
public class example implements runnable{ static { // set upp logging // adding memoryhandler filehandler filehandler = null; memoryhandler memoryhandler; try { filehandler = new filehandler("myapp.log"); filehandler.setlevel(level.all); } catch (ioexception e) { e.printstacktrace(); } memoryhandler = new memoryhandler( filehandler, 100, level.severe); logger rootlogger = logger.getlogger(""); rootlogger.addhandler(memoryhandler); } public static void main(string[] args) throws exception { thread serverthread = new thread(new example()); serverthread.setdaemon(false); serverthread.start(); } public void run() { runtime.getruntime().addshutdownhook(new thread() { @override public void run() { // execute when application shutting down. // // memoryhandler not not in handlers list (handler handler: logger.getlogger("").gethandlers()) { system.out.println("looping handlers"); if (handler.getclass() == memoryhandler.class) { system.out.println("memoryhandler found"); //this never happens. ((memoryhandler)handler).push(); } } }); try { // alot of stuff } catch (exception ex) { } } }
please advice!
Comments
Post a Comment