• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Cannot log into file

Peter Remec

Lurker
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:
<configuration>
  <property name="LOG_DIR" value="/data/data/my.package/files" />

  <appender name="logcat" class="ch.qos.logback.classic.android.LogcatAppender">
    <encoder>
      <pattern>%msg</pattern>
    </encoder>
  </appender>

  <appender name="TraceLog" class="ch.qos.logback.core.FileAppender">
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <level>TRACE</level>
      <onMatch>ACCEPT</onMatch>
      <onMismatch>DENY</onMismatch>
    </filter>

    <file>${LOG_DIR}/app_trace.log</file>

    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="DebugLog" class="ch.qos.logback.core.FileAppender">
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <level>DEBUG</level>
      <onMatch>ACCEPT</onMatch>
      <onMismatch>DENY</onMismatch>
    </filter>

    <file>${LOG_DIR}/app_debug.log</file>

    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <logger name="my.package.A" level="TRACE">
    <appender-ref ref="TraceLog" />
  </logger>

  <logger name="my.package.B" level="DEBUG">
    <appender-ref ref="DebugLog" />
  </logger>

  <root level="INFO">
    <appender-ref ref="logcat" />
  </root>
</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)
 
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.
 
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?
 
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?


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:
/storage/emulated/0/data/data/my.package/files

and log file(s) are created in
Code:
/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.
 
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.
 
No problem, you sure helped me find a solution with your suggestions. Maybe someone else can give some explanation. Thanks!
 
Back
Top Bottom