For example, application scenarios:
When a service needs to start n ports to listen for data from n sources and process data logic consistent; but I want their logs to be printed separately in folders to better analyze the problem, then I can use the template I provide below;
Dynamically generate log loggers, and then manage them through objects. Then we can use loggers from this manager to print logs, or use logger objects from LoggerFactory according to id
public void initLogger(Set<Integer> keySet) { // Get Logback's LoggerContext LoggerContext context = (LoggerContext) (); for (Integer port : keySet) { Logger logger = null; try { String currPath = bsePath + "/" + port + "/"; // Create Logger logger = ("LOGGER-" + port); (); (false); // Appenders that inherit the parent Logger are forbidden // Create Appender RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>(); (context); ("DYNAMIC_APPENDER_" + port); (currPath + ""); // Configure scrolling policy TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<>(); (context); (appender); (currPath + "log-%d{yyyy-MM-dd}.%"); (3); (("7GB")); // Configure SizeAndTimeBasedFNATP SizeAndTimeBasedFNATP<ILoggingEvent> triggeringPolicy = new SizeAndTimeBasedFNATP<>(); (context); (("100MB")); (rollingPolicy); (triggeringPolicy); try { (); // Start the scrolling policy } catch (Exception e) { ("Failed to start rolling or triggering policy:", e); } // Configure the encoder PatternLayoutEncoder encoder = new PatternLayoutEncoder(); (context); ("%d{yyyy-MM-dd HH:mm:} [%thread] %-5level %logger{36} - %msg%n"); (); // Set Appender (rollingPolicy); (encoder); try { (); } catch (Exception e) { ("Failed to start appender: ", e); } // Add Appender to Logger (appender); // Output log ("Test log,Output to dynamic path file:{}", currPath); } catch (Exception e) { ("initializationloggerfail:{}", port); throw new RuntimeException(e); } LightManagerBo lightManagerBo = (port, k -> new LightManagerBo()); (logger); } }
This is the article about Java's example of batch generation of logback files. For more related Java's batch generation of logback files, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!