Logback 动态非配置化实现

阅读更多
public void main(String[] args){
	private LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

	SyslogAppender syslogAppender = new SyslogAppender();
	syslogAppender.setSyslogHost("127.0.0.1");
	syslogAppender.setPort(9000);
	syslogAppender.setName("LOGSYS");
	syslogAppender.setFacility("LOCAL0");
	syslogAppender.setContext(context);
	syslogAppender.setSuffixPattern("%d %-5level [%thread] %logger{0}: %msg%n");
	syslogAppender.start();

	AsyncAppender asyncSysLogAppender = new AsyncAppender();
	asyncSysLogAppender.setContext(context);
	asyncSysLogAppender.setName("ASYNC_SYSLOG");
	asyncSysLogAppender.setQueueSize(qianbaoProperties.getLogging().getLogstash().getQueueSize());
	asyncSysLogAppender.addAppender(syslogAppender);
	asyncSysLogAppender.start();

	context.getLogger("ROOT").addAppender(asyncSysLogAppender);

	LogbackLoggerContextListener loggerContextListener = new LogbackLoggerContextListener();
	loggerContextListener.setContext(context);
	context.addListener(loggerContextListener);
}

class LogbackLoggerContextListener extends ContextAwareBase implements LoggerContextListener {

	@Override
	public boolean isResetResistant() {
		return true;
	}

	@Override
	public void onStart(LoggerContext context) {

	}

	@Override
	public void onReset(LoggerContext context) {

	}

	@Override
	public void onStop(LoggerContext context) {
	}

	@Override
	public void onLevelChange(ch.qos.logback.classic.Logger logger, Level level) {
	}
}

 

你可能感兴趣的:(logback,非配置,动态)