Log4j java — динамическое создание регистратора в многопоточной средеJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Log4j java — динамическое создание регистратора в многопоточной среде

Сообщение Anonymous »


I am trying to incorporate a new logger in my application, I already have a legacy xml file that looks like this:

C:/qa/TestCaseGenerator/SystemConsole.log C:/qa/TestCaseGenerator/SystemConsoleLogs/%d{yyyy-MM-dd-hh-mm}_SystemConsole.zip I tried using this and adding another appender and logger to the xml file, this is my apender:

I am using ThreadContext to set the autoTesterTask value, but from what I understood in a multithreaded environment using the ThreadContext and the logger.info(msg) needs to be atomic meaning I cannot log anything until one thread is finished due to the fact that it needs to change the ThreadContext and then log, if another log will change the ThreadContext while the first log did not finish running logger.info(msg) there could be a mismatch in data, right?

So I tried also progrematically creating a helper class to add appenders:

public class LoggerManager { private static final Logger logger = LogManager.getLogger(); private final LoggerContext ctx; private final LoggerConfig loggerConfig; private HashMap refs; public static Logger getLogger() { return logger; } private static LoggerManager loggerManagerInstance; private LoggerManager(){ ctx = (LoggerContext) LogManager.getContext(false); loggerConfig = ctx.getConfiguration().getRootLogger(); refs = new HashMap(); } public static LoggerManager LoggerManager(){ if (loggerManagerInstance==null) loggerManagerInstance = new LoggerManager(); return loggerManagerInstance; } public void removeLogger(String name){ Appender appender = ctx.getConfiguration().getAppender(name); if(appender != null){ appender.stop(); loggerConfig.removeAppender(name); ctx.getConfiguration().getAppenders().remove(name); refs.remove(name); ctx.getConfiguration().removeLogger(name); ctx.updateLoggers(); } try{ TimeUnit.SECONDS.sleep(3); }catch (InterruptedException e){ logger.error(e); } logger.traceExit(); } public Logger getLogger(String name) { return ctx.getLogger(name); } public void createLogger(String name,String filePath){ Configuration config = ctx.getConfiguration(); PatternLayout layout = PatternLayout.newBuilder() .withConfiguration(config) .withPattern("%d{HH:mm:ss.SSS} %msg%xEx%n") .build(); Appender appender = RollingFileAppender.newBuilder() .setConfiguration(config) .setName(name) .setLayout(layout) .withFileName(filePath + ".log") .withPolicy(SizeBasedTriggeringPolicy.createPolicy("2 KB")) .withFilePattern(filePath + "_%d{yyyy-MM-dd_hh-mm-ss}.log") .withStrategy(DefaultRolloverStrategy.newBuilder() .withConfig(config) .build()) .withImmediateFlush(false) .build(); appender.start(); config.addAppender(appender); AppenderRef ref = AppenderRef.createAppenderRef(name, null, null); loggerConfig.addAppender(appender, null, null); refs.put(name,ref); ctx.updateLoggers(); } } I get the files being created but multiple unrelated logs that comes I guess from my log4j2.xml root logger that is loading when application is up (which I still need I just want to add other loggers without interfering with the old ones).

Also, read about MDC and NDC which both still sounds like won't fix my thread issues or dynamically creating appenders and loggers issues.

Is there a way to have my log4j2.xml file still loading in the beginning of my application and also when some threads are being executed during my application runtime creating new loggers that will only serve these threads (appender per thread, each thread logs to different file) and once thread dies I don't need them anymore.


Источник: https://stackoverflow.com/questions/780 ... nvironment
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»