Creating a Logical Log

I have a problem with the logback project. My requirement is that I have to dynamically create log properties. Let me explain this with an example.

My project creates a socket connection with an external system and can have multiple sockets. For each socket, I want to have different log files that will contain messages that are read and sent. To do this, I create a program log for sockets. The problem is when I want to reconfigure loggers based on logback.xml (by adding scan = "true" or by reinitializing the log), the logs I create cannot be used. How can I fix this, or can you advise me a different solution?

This is my configuration file (logback.xml)

<?xml version="1.0" ?> <configuration> <property name="HOME_PATH" value="/data/logs/myapp/" scope="CONTEXT" /> <property name="MYAPP_LOG_FILE" value="myapp.log" /> <property name="MYAPP_ROLLING_TEMPLATE" value="%d{yy-MM-dd}" scope="CONTEXT" /> <property name="MYAPP_OLD_LOG_FILE" value="${MYAPP_LOG_FILE}.%d{yy-MM-dd}" /> <property name="DEFAULT_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%file:%line] [%level] %msg%n" scope="CONTEXT" /> <appender name="myAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${HOME_PATH}${MYAPP_LOG_FILE}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${HOME_PATH}${MYAPP_LOG_FILE}.${MYAPP_ROLLING_TEMPLATE}</fileNamePattern> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${DEFAULT_PATTERN}</pattern> </encoder> </appender> <logger name="com.myapp" level="DEBUG" additivity="false"> <appender-ref ref="myAppender" /> </logger> <root level="OFF"> </root> </configuration> 

and here you can see how I create a logg programmatically (again, I only do this for socket logs).

 public static Logger createLogger(String name) { ch.qos.logback.classic.Logger templateLogger = (ch.qos.logback.classic.Logger) LogUtil.getLogger("com.myapp"); LoggerContext context = templateLogger.getLoggerContext(); String logDir = context.getProperty("HOME_PATH"); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setPattern(context.getProperty("DEFAULT_PATTERN")); encoder.setContext(context); DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent> timeBasedTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent>(); timeBasedTriggeringPolicy.setContext(context); TimeBasedRollingPolicy<ILoggingEvent> timeBasedRollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>(); timeBasedRollingPolicy.setContext(context); timeBasedRollingPolicy.setFileNamePattern(logDir + name + ".log." + context.getProperty("MYAPP_ROLLING_TEMPLATE")); timeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(timeBasedTriggeringPolicy); timeBasedTriggeringPolicy.setTimeBasedRollingPolicy(timeBasedRollingPolicy); RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>(); rollingFileAppender.setAppend(true); rollingFileAppender.setContext(context); rollingFileAppender.setEncoder(encoder); rollingFileAppender.setFile(logDir + name + ".log"); rollingFileAppender.setName(name + "Appender"); rollingFileAppender.setPrudent(false); rollingFileAppender.setRollingPolicy(timeBasedRollingPolicy); rollingFileAppender.setTriggeringPolicy(timeBasedTriggeringPolicy); timeBasedRollingPolicy.setParent(rollingFileAppender); encoder.start(); timeBasedRollingPolicy.start(); rollingFileAppender.stop(); rollingFileAppender.start(); ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) LogUtil.getLogger(name); logbackLogger.setLevel(templateLogger.getLevel()); logbackLogger.setAdditive(false); logbackLogger.addAppender(rollingFileAppender); return logbackLogger; } 

And this is how I reinitialize logback

 private static void initializeLogback() { File logbackFile = new File(logFilePath); System.setProperty("logback.configurationFile", logbackFile.getAbsolutePath()); StaticLoggerBinder loggerBinder = StaticLoggerBinder.getSingleton(); LoggerContext loggerContext = (LoggerContext) loggerBinder.getLoggerFactory(); loggerContext.reset(); JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(loggerContext); try { configurator.doConfigure(logbackFile); } catch( JoranException e ) { throw new ColumbusRuntimeException(e.getMessage(), e); } } 
+6
source share
1 answer

It looks like you need SiftingAppender where your discriminator will be the socket id itself or any combined options. I don't know what kind of threading problems you encountered with this (when the MDC is being read, etc.), but this should be a good starting point and looks like your case.

+3
source

Source: https://habr.com/ru/post/907201/


All Articles