如何配置BugVm / PlayN / RoboVm以使用特定的日志记录基础设施来处理通过slf4j-api触发的所有日志?
BugVm / PlayN / RoboVm是允许在ios和android之上进行开发的一些工具。 java字节码被编译为平台细节,通常的日志记录库不记录任何内容。
答案 0 :(得分:0)
首先确保您可以访问BugPlatform对象
{
// create a full-screen window
CGRect bounds = UIScreen.getMainScreen().getBounds();
UIWindow window = new UIWindow(bounds);
// create and initialize the PlayN platform
BugPlatform.Config config = new BugPlatform.Config();
config.orients = UIInterfaceOrientationMask.All;
//get a BugPlatform
final BugPlatform pf = BugPlatform.create(window, config);
pf.log().info("test direct logging");
//add a new appender
addSlf4jBugVmAppender(pf);
//test the new log via slf4j
org.slf4j.LoggerFactory.getLogger("a").info("logging via BugVmAppender with [{}]","success");
}
private void addSlf4jBugVmAppender(BugPlatform pf) {
Logger logbackLogger = (ch.qos.logback.classic.Logger) LoggerFactory
.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
LoggerContext lc = logbackLogger.getLoggerContext();
PatternLayout pl = new PatternLayout();
pl.setPattern("%d %5p %t [%c:%L] %m%n)");
pl.setContext(lc);
pl.start();
BugVmAppender<ILoggingEvent> appender = new BugVmAppender<ILoggingEvent>(pf);
appender.setName("BUGVM_APPENDER");
appender.setLayout(pl);
appender.start();
Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.addAppender(appender);
}
BugVmAppender就是这个:
package playn.showcase.bugvm;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import ch.qos.logback.core.Layout;
import playn.bugvm.BugPlatform;
import playn.core.Log;
public class BugVmAppender<T extends ILoggingEvent> extends AppenderBase<T> {
private Log log;
private Layout<T> layout;
public BugVmAppender(BugPlatform pf) {
this.log = pf.log();
}
@Override
protected void append(T event) {
int level = event.getLevel().levelInt;
if (level == Level.ERROR.levelInt) {
log.error(layout.doLayout(event));
} else if (level == Level.WARN.levelInt) {
log.warn(layout.doLayout(event));
} else if (level == Level.INFO.levelInt) {
log.info(layout.doLayout(event));
} else if (level == Level.DEBUG.levelInt) {
log.debug(layout.doLayout(event));
} else if (level == Level.TRACE.levelInt) {
log.debug(layout.doLayout(event));
}
}
public void setLayout(Layout<T> layout) {
this.layout = layout;
}
}
现在登录ios应该是这样的:
Oct 16 05:06:54 iPad ShowcaseBugVM[1941] <Warning>: INFO: test direct logging
Oct 16 05:06:54 iPad ShowcaseBugVM[1941] <Warning>: INFO: 2016-10-16 05:06:54,774 INFO main [a:-1] logging via BugVmAppender with [success]
Oct 16 05:06:54 iPad ShowcaseBugVM[1941] <Warning>: INFO: 2016-10-16 05:06:54,786 INFO main [playn.showcase.bugvm.AppInfo:-1] searching information for class playn.showcase.bugvm.ShowcaseBugVM$1 defined in groupId=[] artifactId=[]