java - Logger in HashMap -
i'm using java logger class log stuff , depending on data need configure log routes.
let have following code:
public class loggerlocator { private static hashmap<string, logger> loggermap = new hashmap<string, logger>(); private static int count = 0; public logger getlogger(string id) { if(!loggerlocator.loggermap.containskey(id)) { configure(id); } return loggerlocator.loggermap.get(id); } private void configure(string id) { logger logger = logger.getlogger(loggerlocator.class.getname()); filehandler fh = new filehandler(string.format("/home/abc/logs/mylog_%d.log", id), true); filehandler.setformatter(new myformatter()); logger.addhandler(fh); } loggerlocator.count++; loggerlocator.loggermap.put(id, logger); }
my problem when extress test sending lot of requests server, printed count variable in every request, expected have value of 1 instead of getting value of 2 , 2 files name mylog_{id}.log, mylog_{id}.log.1
is posibble loggermap empty before create first logger , assigning hashmap , thread getting false in loggerlocator.loggermap.containskey(id)?
yes, it's not possible loggermap
empty before create first logger, empty. define variable empty map. if you'd restrict access map single thread @ time need use syncrhonized
keyword somewhere. additionally not recommend putting loggerlocator.count++;
or loggerlocator.loggermap.put(id, logger);
outside of methods. strikes me anti-pattern. methods getlogger
, configure
should static , class definition should abstract
avoid being able to/having instantiate instance of loggerlocator.
Comments
Post a Comment