Logback 2.0 на Android дает NullPointerException, когда я запускаю модульные тесты - PullRequest
2 голосов
/ 28 октября 2019

Я перешел на выход 2.0. Когда я запускаю модульные тесты (используя @Test), все тесты работают хорошо. Однако они выдают ошибку:

Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Reported exception:
java.lang.NullPointerException
at ch.qos.logback.core.android.AndroidContextUtil.getMountedExternalStorageDirectoryPath(Unknown Source)
at ch.qos.logback.core.android.AndroidContextUtil.setupProperties(Unknown Source)
at org.slf4j.impl.StaticLoggerBinder.init(Unknown Source)
at org.slf4j.impl.StaticLoggerBinder.<clinit>(Unknown Source)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
at com.accells.MyApplication.<clinit>(MyApplication.java:35)
at sun.reflect.GeneratedSerializationConstructorAccessor7.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:45)
...

Класс MyApplication расширяет класс Application. Он содержит строку:

private static final Logger logger = LoggerFactory.getLogger(MyApplication.class);

Я использую код для настройки конфигурации обратного входа. Конфигурация обратного входа:

public static void configure(Context context, String logFileName) {
    String appInfo = String.format(APP_INFO_PATTERN, AppUtils.getVersionName(context), CommunicationManager.API_VERSION, AppUtils.getAndroidVersion(), Build.MODEL);
    String logPattern = LOG_PATTERN.replace("__app__info__", appInfo).replace("%pid", String.valueOf(android.os.Process.myPid()));
    // since we want to reconfigure it
    LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
    lc.reset();

    // add the newly created appenders to the root logger;
    // qualify Logger to disambiguate from org.slf4j.Logger
    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    root.setLevel(BuildConfig.LOG_LEVEL);

    // use encrypted
    RollingFileAppender<ILoggingEvent> rollingFileAppender = new EncryptedLog4JAppender<>();

    rollingFileAppender.setName("RollingFileAppender");
    rollingFileAppender.setContext(lc);
    rollingFileAppender.setFile(logFileName);
    rollingFileAppender.setAppend(true);

    rollingFileAppender.setImmediateFlush(true);

    SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<>();
    rollingFileAppender.setTriggeringPolicy(triggeringPolicy);
    FileSize fileSize = new FileSize(FileSize.MB_COEFFICIENT);
    triggeringPolicy.setMaxFileSize(fileSize);
    triggeringPolicy.setContext(lc);
    triggeringPolicy.start();

    FixedWindowRollingPolicy fixedWindowRollingPolicy = new FixedWindowRollingPolicy();
    rollingFileAppender.setRollingPolicy(fixedWindowRollingPolicy);
    fixedWindowRollingPolicy.setContext(lc);
    fixedWindowRollingPolicy.setMinIndex(0);
    fixedWindowRollingPolicy.setMaxIndex(1);
    fixedWindowRollingPolicy.setFileNamePattern(MyApplication.getInstance().getCacheFolder() + LOG_FILE_NAME_PATTERN);
    fixedWindowRollingPolicy.setParent(rollingFileAppender);
    fixedWindowRollingPolicy.setContext(lc);
    fixedWindowRollingPolicy.start();

    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(lc);
    encoder.setPattern(logPattern);
    rollingFileAppender.setEncoder(encoder);
    encoder.start();
    rollingFileAppender.start();
    root.addAppender(rollingFileAppender);
    lc.start();

    EnvironmentProxyHelper.getInstance().addLogCatIfNeed(lc, root, logPattern);
}

Как избавиться от NullPointerException навсегда?

...