Search code examples
androidservicemultiprocessinglogback

How to log from 1 android-app using 2 processes into 1 logfile


I'm using an App (runs in Process1 P1) that is starting an Android Service in a separate process (runs in Process2 P2).

Following common problem when using android os >= API 23, then WRITE_EXTERNAL_STORAGE permission has to be granted before being able to do create a logging directory and do logging, is already solved by initializing the Logger context:

val loggerContext :LoggerContext = LoggerFactory.getILoggerFactory() as LoggerContext
        loggerContext.reset()
        val contextInitializer = ContextInitializer(loggerContext)
        contextInitializer.autoConfig()

Im using a RollingFileAppender like this:

<appender name="myMultiProcessApp" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_DIR}/log.txt</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- Group the logs by day -->
            <fileNamePattern>${LOG_DIR}/log-%d{yyyy-MM-dd}#%i.zip</fileNamePattern>
            <!-- Keeping DAYS_TO_KEEP (defaulting to 5) grouped logs - days in this case -->
            <maxHistory>${DAYS_TO_KEEP:-5}</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- Assuming a compression ratio of 50% (very conservative) each compressed file should be less than 2MB -->
                <maxFileSize>${MAX_FILE_SIZE:-4Mb}</maxFileSize>
                <!-- Total Size Cap - this option is not available on logback-android, only on mainstream logback -->
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <pattern>[%date{ISO8601}] [%thread] %-5level %logger{36} %M - %msg%n</pattern>
        </encoder>
    </appender>

The Problem, The Situation: P1 and P2 are using classes which are logging using the logback framework But currently only the logs of P2 are appearing in the log-File! P2 will be started from P1 after P1 has all necessary permissions granted.

Now The Question: Does anybody know, if it is possible to write Logs in 1 File from 2 separate Processes used in 1 Android-App with logback? Do I have to explicitly grand write Permission for P2?

If yes, can you tell me how?

TIA Luke


Solution

  • Answer: Yes, it is possible that 2 Processes are writing into 1 logfile on same time. We have to activate the prudent mode, which takes 3 times more time to log, but is working fine for most of the situations:

    look here in the documentation: documentation for prudent mode in File Appender

    Prudent mode has following restrictions:

    1. no filename in the file property. It will be started with the naming of the filenamePattern. <file></file>
    2. no compression is supported. (The example in the question but with a .log instead of a .zip)<fileNamePattern>${LOG_DIR}/log-%d{yyyy-MM-dd}#%i.log</fileNamePattern>

    look here regarding the limitations: prudent limitations