Logback-android:日志未写入文件

Logback-android: Log not getting writen to a file

正在尝试使用 logback-android 重定向日志消息,以便将消息保存在文件中。但是,它没有保存到文件中。

这是我的 logback.xml 文件配置,存储在我的 Android Studio

中的 src/main/assets
<configuration debug="true">
    <!-- Create a file appender for a log in the application's data directory -->
    <appender name="FILE" class="ch.qos.logback.classic.android.FileAppender">
        <file>/data/com.test.equa/files/log/foo.log</file>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- Write INFO (and higher-level) messages to the log file -->
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration> 

这是我启动日志记录的一段代码。

 @Override
    public void onCreate() {
        super.onCreate();
        loadData();

        Logger log = LoggerFactory.getLogger(MyActivity.class);
        log.error("Application Data setup in progress");



    }

问题: 我继续在 logcat 中看到消息,但我希望它们存储在我的 android SD 卡内存中。

在manifest中添加了将日志写入sd卡的用户权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我是否遗漏了此配置中的某些内容,因为我在 logcat 中也没有看到任何配置错误的错误或消息。有人可以帮我吗

您是否尝试向 AndroidManifest.xml 添加配置?

清单示例(来自link):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <logback>
        <configuration>
          <appender
              name="LOGCAT"
              class="ch.qos.logback.classic.android.LogcatAppender" >
              <tagEncoder>
                  <pattern>%logger{0}</pattern>
              </tagEncoder>
              <encoder>
                  <pattern>[ %thread ] %msg%n</pattern>
              </encoder>
          </appender>

          <root level="WARN" >
              <appender-ref ref="LOGCAT" />
          </root>
        </configuration>
    </logback>

</manifest>

对于Android 6+ 你必须在manifest.xml.

中定义permissions manually in settings for your application. Otherwise logback will not be allowed to write to external storage even if you grant WRITE_EXTERNAL_STORAGE权限

我从 那里得到了解决方案。

对我来说,Nija 对这个问题的第一条评论解决了这个问题。

我的文件标签现在看起来像这样:

<file>/sdcard/Android/data/[my-package-from-AndroidManifest]/logs.log</file>

奇怪的是,logs.log 文件出现在我的内部存储中给定路径下(没有 /sdcard/)。

我的 phone 是 运行 Android 6.0.1 以防相关。

最后我的答案是尝试上面的文件路径并检查内部存储以防其他答案不起作用。

更新:

只想打开电脑上的日志文件,却找不到。我仍然可以在 phone 本身上使用任何存储访问应用程序(例如文件管理器)找到它。但是当查看与 PC 完全相同的文件夹时,它不存在。因此,当它对您来说足够好时,请使用存储访问应用程序进行检查。

就我而言,当我重新启动智能手机时,Windows 文件资源管理器中的文件只有 showing/updating。

强制更新我使用:

MediaScannerConnection.scanFile(myContext, new String[]{new File(Environment.getExternalStorageDirectory(), "logFile.log").toString()}, null, null);