Git Product home page Git Product logo

Comments (2)

tony19 avatar tony19 commented on August 16, 2024

Instead of trying to prevent the exception (a symptom), I think you should be wondering how to get the SD card's path into a variable for use in logback configuration files. This assumes you prefer maximum portability by not using an absolute path to the SD card (which is smart :).

The init() function you're using is flawed and actually unnecessary. It looks like you were trying to restart the auto-config after setting an environment variable to store the SD path (so that you can use that variable in a file path for the RollingFileAppender). And then you were printing the JoranException once in a catch block, again by StatusPrinter.printInCaseOfErrorsOrWarnings, and yet again from the debug='true' setting from your logback config. So, you'd see the same error printed 3 times in logcat (probably not what you wanted). Finally, the init() directly uses classes from logback, thus adding an unnecessary dependency and defeating the point of the SLF4J facade.

Luckily, you can accomplish your goal without all this init() stuff you have going on. Android exposes the SD card path in an environment variable, named EXTERNAL_STORAGE. (I'm not sure which Android versions expose this, but I just verified Jelly Bean 4.1.1.) Logback configuration can read environment variables and can define local properties. Armed with these tools, you can use the following config without any initialization code and without any extra dependencies beyond SLF4J (note the single <property> element I added to your original config):

<configuration>
    <appender
        name="LOGCAT"
        class="ch.qos.logback.classic.android.LogcatAppender" >
        <tagEncoder>
            <pattern>%logger{0}</pattern>
        </tagEncoder>
        <encoder>
            <pattern>%d{HH:mm:ss} - %msg %n</pattern>
        </encoder>
    </appender>


    <!--
    ########################################################
     Use Android's built-in environment variable for the
     path to the SD card: EXTERNAL_STORAGE
    ########################################################
    -->
    <property name="log_path" value="${EXTERNAL_STORAGE}/neusoft/mdm/log/" />



    <appender
        name="File"
        class="ch.qos.logback.core.rolling.RollingFileAppender" >
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <pattern>%d{HH:mm:ss} [%thread] [%.-1level] - %msg %n</pattern>
        </encoder>

        <File>${log_path:-}agent.log</File>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
            <FileNamePattern>${log_path:-}agent.%d{yyyy-MM-dd}.zip</FileNamePattern>
            <!-- keep 7 days' worth of history -->
            <MaxHistory>7</MaxHistory>

            <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" >
                <MaxFileSize>10MB</MaxFileSize><!-- MaxFileSize should be just above daily log size -->
            </TimeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>

    <root level="TRACE" >
        <!-- <appender-ref ref="LOGCAT" />  -->
        <appender-ref ref="File" />
    </root>
</configuration>

from logback-android.

lock avatar lock commented on August 16, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

from logback-android.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.