中间件内置logger功能
程序员文章站
2023-12-29 14:55:16
...
在开发中间件的时候有时候希望日志能强制输出到我们制定的地方而不需要用户关心,这个可以通过JAVA API的方式实现
static public final Logger log = new LoggerContext().getLogger(ClientImpl.class); private static void initLog() throws Exception { // 使用logback初始化 final String logPath = LocalConfigInfoProcessor.getLogFile(); System.out.println("jasmine client log path : " + new File(logPath).getAbsolutePath()); RollingFileAppender appender = new RollingFileAppender(); appender.setContext(log.getLoggerContext()); appender.setAppend(true); appender.setFile(logPath); TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy(); rollingPolicy.setContext(log.getLoggerContext()); rollingPolicy.setFileNamePattern(LocalConfigInfoProcessor.logRoot + File.separator + "jasmine-client-%d{yyyy-MM-dd}.log"); rollingPolicy.setMaxHistory(30); rollingPolicy.setParent(appender); rollingPolicy.start(); appender.setRollingPolicy(rollingPolicy); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(log.getLoggerContext()); encoder.setPattern("%date{ISO8601} %-5level [%thread] %logger{32} - [%X{uid}] [%X{coeus.uid}] %message%n"); encoder.start(); appender.setEncoder(encoder); appender.start(); log.setLevel(Level.INFO); log.setAdditive(false); log.addAppender(appender); }