1. Download our Official Android App: Forums for Android!

Cannot log into file

Discussion in 'Android Development' started by Peter Remec, Sep 5, 2017.

  1. Peter Remec

    Peter Remec Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Sep 5, 2017

    Sep 5, 2017
    5
    0
    5
    Male
    I'm trying to add some log files for my Android app using Android Logback. It logs to a logcat while logging to file fails.

    Here's my initialization - logback.xml:

    HTML:
    1. <configuration>
    2.   <property name="LOG_DIR" value="/data/data/my.package/files" />
    3.  
    4.   <appender name="logcat" class="ch.qos.logback.classic.android.LogcatAppender">
    5.     <encoder>
    6.       <pattern>%msg</pattern>
    7.     </encoder>
    8.   </appender>
    9.  
    10.   <appender name="TraceLog" class="ch.qos.logback.core.FileAppender">
    11.     <filter class="ch.qos.logback.classic.filter.LevelFilter">
    12.       <level>TRACE</level>
    13.       <onMatch>ACCEPT</onMatch>
    14.       <onMismatch>DENY</onMismatch>
    15.     </filter>
    16.  
    17.     <file>${LOG_DIR}/app_trace.log</file>
    18.  
    19.     <encoder>
    20.       <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    21.     </encoder>
    22.   </appender>
    23.  
    24.   <appender name="DebugLog" class="ch.qos.logback.core.FileAppender">
    25.     <filter class="ch.qos.logback.classic.filter.LevelFilter">
    26.       <level>DEBUG</level>
    27.       <onMatch>ACCEPT</onMatch>
    28.       <onMismatch>DENY</onMismatch>
    29.     </filter>
    30.  
    31.     <file>${LOG_DIR}/app_debug.log</file>
    32.  
    33.     <encoder>
    34.       <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    35.     </encoder>
    36.   </appender>
    37.  
    38.   <logger name="my.package.A" level="TRACE">
    39.     <appender-ref ref="TraceLog" />
    40.   </logger>
    41.  
    42.   <logger name="my.package.B" level="DEBUG">
    43.     <appender-ref ref="DebugLog" />
    44.   </logger>
    45.  
    46.   <root level="INFO">
    47.     <appender-ref ref="logcat" />
    48.   </root>
    49. </configuration>
    I also tried to write log files to a sd card (with WRITE_EXTERNAL_STORAGE permission) - no luck. Also there's no error in a logcat that could give me some hint.

    Any idea how to solve my problem? Is there any other permission that needs to be added?

    Using slf4j-1.7.21, logback-android-core-1.1.1-6 and logback-android-classic-1.1.1-6. I'm running my app on Sony Xperia Z with Android KitKat (4.4.4)
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,450
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,450
    10,736
    1,988
    Male
    Software developer
    South West of England
  3. Peter Remec

    Peter Remec Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Sep 5, 2017

    Sep 5, 2017
    5
    0
    5
    Male
    My logback.xml is in app/src/main/assets folder. I believe there's nothing wrong with config file location since logcat logging works. I'll try to put config in a manifest file but I don't think that will do any difference.
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,450
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,450
    10,736
    1,988
    Male
    Software developer
    South West of England
    This file has no relevance to Logcat.
     
  5. Peter Remec

    Peter Remec Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Sep 5, 2017

    Sep 5, 2017
    5
    0
    5
    Male
    I don't understand. How can this file has no relevance to logcat? I'm defining an appender to logcat in it.

    I also tried to put initialization in AndroidManifest.xml - no luck. Even logcat logging stopped working.

    Configuration in code (https://github.com/tony19/logback-android/wiki#configuration-in-code) also doesn't work - it logs to logcat but not into file.
     
    #5 Peter Remec, Sep 6, 2017
    Last edited: Sep 6, 2017
  6. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,450
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,450
    10,736
    1,988
    Male
    Software developer
    South West of England
    Ok I see this library works in a similar way to Log4j.

    A couple of things you can check are that the target LOG_DIR exists, and it has the correct permissions.
    Are you getting nothing in the log file, or does it just not exist?
     
  7. Peter Remec

    Peter Remec Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Sep 5, 2017

    Sep 5, 2017
    5
    0
    5
    Male

    I appreciate your help but in the meantime I managed to make my logging work. I just added /storage/emulated/0 prefix to a LOG_DIR property value in a logback.xml file. So my new LOG_DIR value is

    Code (Text):
    1. /storage/emulated/0/data/data/my.package/files
    and log file(s) are created in
    Code (Text):
    1. /data/data/my.package/files
    folder.

    Since I'm quite a beginner in Android development I'd like to ask you about this solution: Why is this /storage/emulated/0 needed? Is it some kind of root directory that apps have access to?

    And one other thing... If I browse my device storage directly on a device (with File Manager + app), log directory is visible, but when I connect my device on a PC via USB, I can't see that same directory. Why is that?

    Thank you for you help.
     
  8. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,450
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,450
    10,736
    1,988
    Male
    Software developer
    South West of England
    Well it's good you found a solution. Must admit I can offer no explanation as to why you need that modified path to get it working.

    On the device connection issue, is the device connection mode 'file transfer'. Most devices connect in charging only mode by default.
    Another thing to check is the USB cable. Not all cables offer data transfer capability.
     
    Peter Remec likes this.
  9. Peter Remec

    Peter Remec Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Sep 5, 2017

    Sep 5, 2017
    5
    0
    5
    Male
    No problem, you sure helped me find a solution with your suggestions. Maybe someone else can give some explanation. Thanks!
     

Share This Page

Loading...