10

Trying to redirect the log messages using logback-android, so that messages can be saved in a file. However, it is not getting saved into a file.

This is my logback.xml file configration, which is stored under src/main/assets in my Android Studio

<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> 

This is piece of code where i am initiating the logging.

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

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



    }

Issue: I continue to see the messages in the logcat, but i expect them to be stored in my android sd card memory.

Added the user permission in manifest for writting the logs in the sd card

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

Am i missing some thing in this configuration, as i don't see any errors or message in my logcat for any configuration errors also. Can some one help me here

Shiv
  • 689
  • 8
  • 23
  • 1
    I don't know for sure, hence not an answer; but the path `/data/com.test.equa/files/log/foo.log` isn't external storage; so the permission mentioned shouldn't matter. It's probably also `/data/data/PACKAGE...`. If you want to write to the SD card the path should be something like `/sdcard/Android/PACKAGE/....` and the permission matters then. This is just off the top of my head; so it's more "what I'd try next" than trying to answer. :) – QuinnG Jan 21 '15 at 04:25
  • Thanks Nija for pointing that. I checked the package directory too, the log file doesnt exist there too. Thanks again for responding and pointing that.. – Shiv Jan 21 '15 at 04:38
  • What messages exactly do you see in logcat? –  Jan 22 '15 at 03:36
  • @superuser I see the normal logging messages. There are No warning or errors – Shiv Jan 22 '15 at 06:48
  • Based on your example logback.xml, you should see no messages from your app ("Application Data setup in progress") in logcat, since you don't have a `LogcatAppender`. What exactly is the text of the messages you see in logcat? –  Jan 23 '15 at 15:44
  • Exactly that's behavior i expected, but i see all my log messages in my logcat. How do i verify whether my configuration file (logback.xml) is referred or not, my guess is its not getting picked – Shiv Jan 23 '15 at 21:03
  • By chance, are you using `slf4j-android` (not the same as `slf4j-api`)? `slf4j-android` sends all logback output to logcat, and does not process the `logback.xml` file. –  Jan 31 '15 at 01:38
  • Have similar issue here. I see on logcat in emulator but not as the expected format as declared by 'pattern' which make me believe that the xml file wasn't loaded at all and the logcat messages come from implementation of a default logcat appender (since no logback.xml ) – chenchuk Jan 28 '16 at 23:58
  • @Shiv /data/com.test.equa/files/log/foo.log - should not it be /data/data/com.test.equa/files/log/foo.log? – Leos Literak Mar 26 '16 at 17:07

4 Answers4

3

For Android 6+ You have to define 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 permission in manifest.xml.

I took solution from here.

Community
  • 1
  • 1
LukaszTaraszka
  • 801
  • 1
  • 10
  • 26
  • Curious (and somewhat off topic) - How do you grab a copy of the HTML file that file appender logs to from a real device? I do not think you can? You have to retrieve it from an AndroidStudio emulator using DDMS? – XMAN Nov 21 '17 at 23:10
  • 1
    It's a good question and I don't know the answer - I switched back to .Net modules. You should "Ask Question" on a public forum. Good luck. – LukaszTaraszka Nov 23 '17 at 08:40
  • 1
    This is specially important when you are starting to develop your app and still don't request permissions using the UI. – narko Jan 30 '18 at 16:12
1

For me it was kind of solved by the first comment of Nija on the question.

My file tag now looks like this:

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

The weird think is though, that the logs.log file appeared on my internal storage under the given path (without /sdcard/).

My phone is running Android 6.0.1 in case thats relevant.

Concluding my answer is to try the above file path and check internal storage in case the other answers don't work.

Update:

Just wanted to open the log file on the computer, but couldn't find it. I can still find it with any storage access app (e.g. file commander) on the phone itself. But when looking at exactly the same folder with the PC it's not there. So check it out with a storage access app when that's good enough for you.

Recek
  • 1,258
  • 13
  • 16
  • Adding `/sdcard/android/data/` seems to work at the beginning, but I noticed that when I close and reopen my application it no longer writes to the file; it just doesn't do nothing. But it may be that I did something wrong. – G. Ciardini Jul 25 '19 at 08:01
  • Yeah, happened to me. Cannot see the file on Windows, but with a file manager on the smartphone itself, the file is there. – Jefferson Trigueiro Jul 13 '21 at 12:08
0

Did you try to add configuration to AndroidManifest.xml?

Example manifest (from this 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>
Damian Walczak
  • 1,334
  • 14
  • 21
  • Thanks for the response. I did try that earlier, but let me try one more time from scratch and will update – Shiv Jan 29 '15 at 00:29
  • No luck. it works well using Logcat Appender, but if i replace that with a File Appender and provide a file, its not working. If i configure using the code, it works well. But to simplify my application, i am looking to configure via the XML's. Do you have any working sample codes, which you can share – Shiv Jan 29 '15 at 00:46
0

In my case, the file was only showing/updating in Windows File Explorer when I rebooted the smartphone.

To force the update I used:

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