欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Logback 动态非配置化实现

程序员文章站 2022-05-10 17:22:03
...
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) {
	}
}