我正在尝试使用logback日志记录将android日志发送到Loggly。我创建了一个简单的项目来测试功能。我知道要么我们需要违反严格模式,要么创建异步任务来发送日志,否则它会给你NetworkOnMainThreadException。所以我现在尝试了StrictMode。但是在使用之后我现在得到“java.net.SocketException:Socket is closed”这个异常。
我经常搜索并尝试了一些事情,但到目前为止还没有成功。不知道如何摆脱这个问题。有没有人知道如何解决这个问题,以便我可以通过使用slf4j的logback日志记录将android日志发送到loggly ..!使用android-studio和gradle构建它。
附加代码段并生成错误日志以供参考。 谢谢!
MainActivity类:
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构建文件:
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')
}