Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android best logger for logging into file

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

04-29 12:58:57.604: E/AndroidRuntime(372): java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory

here is my code:

public class Main extends TabActivity {
    private static final Logger log = LoggerFactory.getLogger(Main.class); 

I added the slf4j-android-1.6.1-RC1.jar into build path

What will be the problem?

like image 763
Laszlo Avatar asked Apr 29 '12 13:04

Laszlo


People also ask

Which log level is best in Android?

FINEST indicates a highly detailed tracing message. INFO is a message level for informational messages. OFF is a special level that can be used to turn off logging. SEVERE is a message level indicating a serious failure.

Is there a log file in Android?

System logs are useful when something in the system throws an error. Android allows collecting system logs using Logcat. Log messages can be viewed in a Logcat window in Android Studio, or you can use the command line tool to pull them.

Which log has highest priority in Android Studio?

The order in terms of verbosity, from least to most is ERROR, WARN, INFO, DEBUG, VERBOSE.

How do you access logs on Android?

View your app logs Build and run your app on a device. Click View > Tool Windows > Logcat (or click Logcat in the tool window bar).


2 Answers

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.

like image 157
tony19 Avatar answered Sep 24 '22 05:09

tony19


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

like image 40
Fustigador Avatar answered Sep 23 '22 05:09

Fustigador