java怎么用logback做日志记录以html的形式展示
1个回答
展开全部
<span style="font-size:12px;">import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.FileAppender;
/**
* 根据不同的日志类型,打印不同路径下的yyyyMMdd.log格式日志,例如./logs/flume/playerLogin/20141111.log
* @author melvin0987@163.com
*/
public class LoggerUtil {
public static Logger getLogger(Class<?> clazz) {
Logger rootLogger = (Logger) LoggerFactory.getLogger(clazz);
LoggerContext loggerContext = rootLogger.getLoggerContext();
//loggerContext.reset();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%m");
encoder.start();
FileAppender<ILoggingEvent> appender = new FileAppender<ILoggingEvent>();
String fileName = getTime("yyyyMMdd") + ".log";
String filePath = LogPathConst.LOG_PREFIX + clazz.getSimpleName() + "/" + fileName;
appender.setFile(filePath);
appender.setContext(loggerContext);
appender.setEncoder(encoder);
appender.start();
rootLogger.addAppender(appender);
return rootLogger;
}
private static String getTime(String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date());
}
}
</span>
import java.util.Date;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.FileAppender;
/**
* 根据不同的日志类型,打印不同路径下的yyyyMMdd.log格式日志,例如./logs/flume/playerLogin/20141111.log
* @author melvin0987@163.com
*/
public class LoggerUtil {
public static Logger getLogger(Class<?> clazz) {
Logger rootLogger = (Logger) LoggerFactory.getLogger(clazz);
LoggerContext loggerContext = rootLogger.getLoggerContext();
//loggerContext.reset();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%m");
encoder.start();
FileAppender<ILoggingEvent> appender = new FileAppender<ILoggingEvent>();
String fileName = getTime("yyyyMMdd") + ".log";
String filePath = LogPathConst.LOG_PREFIX + clazz.getSimpleName() + "/" + fileName;
appender.setFile(filePath);
appender.setContext(loggerContext);
appender.setEncoder(encoder);
appender.start();
rootLogger.addAppender(appender);
return rootLogger;
}
private static String getTime(String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date());
}
}
</span>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询