protected FileAppender<E> buildAppender(LoggerContext context) {
        if (archive) {
            final RollingFileAppender<E> appender = new RollingFileAppender<>();
            appender.setContext(context);
            appender.setFile(currentLogFilename);
            appender.setBufferSize(new FileSize(bufferSize.toBytes()));

            if (maxFileSize != null && !requireNonNull(archivedLogFilenamePattern).contains("%d")) {
                final FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
                rollingPolicy.setContext(context);
                rollingPolicy.setMaxIndex(getArchivedFileCount());
                rollingPolicy.setFileNamePattern(getArchivedLogFilenamePattern());
                rollingPolicy.setParent(appender);
                rollingPolicy.start();
                appender.setRollingPolicy(rollingPolicy);

                final SizeBasedTriggeringPolicy<E> triggeringPolicy = new SizeBasedTriggeringPolicy<>();
                triggeringPolicy.setMaxFileSize(new FileSize(maxFileSize.toBytes()));
                triggeringPolicy.setContext(context);
                triggeringPolicy.start();
                appender.setTriggeringPolicy(triggeringPolicy);

                return appender;
            } else {
                final TimeBasedRollingPolicy<E> rollingPolicy;
                if (maxFileSize == null) {
                    rollingPolicy = new TimeBasedRollingPolicy<>();

                    final TimeBasedFileNamingAndTriggeringPolicy<E> triggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<>();
                    triggeringPolicy.setContext(context);
                    triggeringPolicy.setTimeBasedRollingPolicy(rollingPolicy);
                    appender.setTriggeringPolicy(triggeringPolicy);
                } else {
                    // Creating a size and time policy does not need a separate triggering policy set
                    // on the appender because this policy registers the trigger policy
                    final SizeAndTimeBasedRollingPolicy<E> sizeAndTimeBasedRollingPolicy = new SizeAndTimeBasedRollingPolicy<>();
                    sizeAndTimeBasedRollingPolicy.setMaxFileSize(new FileSize(maxFileSize.toBytes()));
                    rollingPolicy = sizeAndTimeBasedRollingPolicy;
                }

                if (totalSizeCap != null) {
                    rollingPolicy.setTotalSizeCap(new FileSize(totalSizeCap.toBytes()));
                }

                rollingPolicy.setContext(context);
                rollingPolicy.setFileNamePattern(archivedLogFilenamePattern);
                rollingPolicy.setMaxHistory(archivedFileCount);

                appender.setRollingPolicy(rollingPolicy);

                rollingPolicy.setParent(appender);
                rollingPolicy.start();
                return appender;
            }
        }

        final FileAppender<E> appender = new FileAppender<>();
        appender.setContext(context);
        appender.setFile(currentLogFilename);
        appender.setBufferSize(new FileSize(bufferSize.toBytes()));
        return appender;
    }
@Test
    void validSetTotalSizeCapNoMaxFileSize() throws IOException, ConfigurationException {
        final YamlConfigurationFactory<FileAppenderFactory> factory =
            new YamlConfigurationFactory<>(FileAppenderFactory.class, validator, mapper, "dw");

        final FileAppender appender = factory.build(new ResourceConfigurationSourceProvider(), "yaml/appender_file_cap2.yaml")
            .buildAppender(new LoggerContext());
        assertThat(appender).isInstanceOfSatisfying(RollingFileAppender.class, roller -> assertThat(roller.getRollingPolicy())
            .isInstanceOfSatisfying(TimeBasedRollingPolicy.class, policy -> assertThat(policy)
                .satisfies(p -> assertThat(p)
                    .extracting("totalSizeCap")
                    .isInstanceOfSatisfying(FileSize.class, x ->
                        assertThat(x.getSize()).isEqualTo(DataSize.mebibytes(50).toBytes())))
                .satisfies(p -> assertThat(p.getMaxHistory()).isEqualTo(5))));
    }