IOException while attempting to communicate with Loggly java.net.SocketException: Socket is closed

220 Views Asked by At

I am trying to send android logs to Loggly and for that using logback logging. I created a simple project to test the functionality. I got to know that either we need to violate strict mode or create Async task to send logs otherwise it will give you NetworkOnMainThreadException. So I tried StrictMode for now. But after using that now I am getting "java.net.SocketException: Socket is closed" this exception.

I searched a lot and tried few things but no success so far on it. Not sure how to get rid of this issue. Does anybody know how to solve this issue so that I can send android logs to loggly via logback logging which uses slf4j..! Using android-studio and gradle build for it.

Attaching the code snippet and generated error log for reference. Thanks!

MainActivity Class:

    import android.os.StrictMode;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    public class MainActivity extends AppCompatActivity  {

    final static Logger logger = LoggerFactory.getLogger(MainActivity.class);

        @Override
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .permitAll()
                .build());

        logger.info("This is a dummy info message");
        logger.info("This is another dummy info message for testing");
        logger.debug("This is a dummy debug message.");

    }
}

logback.xml:

<configuration debug='true'>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <level>INFO</level>
      <onMatch>ACCEPT</onMatch>
      <onMismatch>DENY</onMismatch>
    </filter>
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <!-- Create a file appender for a log in the application's data directory -->
  <appender name="file" class="ch.qos.logback.core.FileAppender">
    <file>/data/data/com.android.myapplication/files/log/logbackLogs.log</file>
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="LOGGLY" class="ch.qos.logback.ext.loggly.LogglyAppender">
    <endpointUrl>https://logs-01.loggly.com/inputs/TOKEN/tag/logbacklogglytest</endpointUrl>
    <pattern>%d{"ISO8601", UTC}  %p %t %c{0}.%M - %m%n</pattern>
  </appender>

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

</configuration>

AndroidManifest.xml

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

Gradle build file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.android.myapplication"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile project(':logback-android-1.1.1-4')
    compile project(':slf4j-api-1.7.6')
    compile project(':logback-ext-loggly-0.1.4')
}
0

There are 0 best solutions below