web services - How to log JAVA Webservice to a file -
i build service jax-rs (jersey 2.0) , tried implement logger writes down events errors or results requests. works fine! :) unfortunately creates file every time service called nice writes in 1 file.
have @ code (it's relevant part):
package webservice.feedbacks; import java.io.ioexception; // imports @path("webservice") public class deletefeedback { logger logger = logger.getlogger("feedbacklogger"); @post @path("/deletefeedback") @consumes(mediatype.application_json + ";charset=utf-8") @produces("text/plain") public string setfeedbacks(string incoming){ filehandler fh; jsonobject jsonobj = new jsonobject(incoming); try { fh = new filehandler("d:/eclipse-projekte/smartliveservice/logs/deletefeedback_log.log"); logger.addhandler(fh); simpleformatter formatter = new simpleformatter(); fh.setformatter(formatter); logger.info("----- beginn des protokolls -----"); } catch (securityexception | ioexception e) {e.getmessage();} int id = jsonobj.getint("id"); string result = executequery(id); logger.info("----- ende des protokolls -----"); return result; } private string executequery(int id) { try { class.forname("com.mysql.jdbc.driver"); } catch (classnotfoundexception e) {logger.info("treiberfehler: " + e.getmessage());} connection con = null; statement stmt = null; try{...} catch(sqlexception e){ logger.info("sql-fehler: " + e.getmessage()); return "error"; } { try { if (stmt != null) stmt.close();} catch (exception e) {logger.info("das statement konnte nicht geschlossen werden: " + e.getmessage());}; try { if (con != null) con.close();} catch (exception e) {logger.info("die verbindung konnte nicht geschlossen werden: " + e.getmessage());}; } return "success"; } }}
as can see created filehandler
. got tutorial because i'm not familiar java had been few years ago. code made database-query , sends results.
so, how have write code logs 1 file instead of creating new 1 every time interface called?
hope can me.
you should not initialize filehandler everytime method called. maybe should make static or initialize in constructor. see append also.
Comments
Post a Comment