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