Android best logger for logging into file

前端 未结 2 860
鱼传尺愫
鱼传尺愫 2021-02-06 14:02

What is the best logger framework which perfectly use in Android system for logging text into file?

I tried to use SLF4J-android but I got an exception

0         


        
相关标签:
2条回答
  • 2021-02-06 14:34

    slf4j-android only supports logging to logcat and thus omits several classes from the regular SLF4J jar. If you want to use logback to log to a file, you need the API jar (not slf4j-android) and logback-android. You're looking for the FileAppender or RollingFileAppender.

    Instructions:

    1. Add slf4j-api-<version>.jar and logback-android-<version>.jar to your classpath.

    2. Create the file assets/logback.xml in your project (or use the AndroidManifest.xml...see example), containing the following configuration:

    <configuration>
      <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>/sdcard/testFile.log</file>
        <append>true</append>
        <encoder>
          <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
      </appender>
            
      <root level="DEBUG">
        <appender-ref ref="FILE" />
      </root>
    </configuration>
    

    Note: Since the specified path is on SD, make sure to use WRITE_EXTERNAL_STORAGE permission. You can instead specify a different path where you already have write permissions.

    Your Java code, which contains the SLF4J logging calls, now logs all events at or above the DEBUG level to the /sdcard/testFile.log.

    0 讨论(0)
  • 2021-02-06 14:41

    Make a folder named "libs" and put the jar inside that folder.

    0 讨论(0)
提交回复
热议问题