import java.io.IOException;
import java.nio.charset.Charset;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.core.FileAppender;
* Log修改管理
*
* @author bash
* @version V1.0
* @since 2016-03-21 10:53
*/
public class LoggerDynamicConfig {
* 日志记录Logger
*/
private static final Logger LOG = (Logger) LoggerFactory.getLogger(LoggerDynamicConfig.class);
@Test
public void addAppender() throws IOException {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setCharset(Charset.forName("UTF-8"));
encoder.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
encoder.setImmediateFlush(true);
encoder.setContext(lc);
FileAppender appender = new FileAppender();
appender.setEncoder(encoder);
appender.setFile("/home/bash/workspace/temp/test.log");
appender.setName("TestFile");
appender.setAppend(false);
appender.setContext(lc);
encoder.start();
appender.start();
LOG.addAppender(appender);
LOG.info("这里时一条所有appender都可以输出的数据。");
LOG.setLevel(Level.ERROR);
LOG.info("这里的数据所有appender都不会输出。");
LOG.error("这里时一条所有appender都可以输出的数据。");
LOG.detachAppender("TestFile");
LOG.error("这条数据不会在FileAppender中输出。");
}
}