Last active
November 30, 2022 01:19
-
-
Save PatrykGala/55603fe4259d812fdc0ffbc9e63eaabc to your computer and use it in GitHub Desktop.
Add logstash logging for Android App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
//Add dependencies | |
dependencies { | |
//... | |
compile 'org.slf4j:slf4j-api:1.7.25' | |
compile 'com.github.tony19:logback-android:1.1.1-11' | |
compile group: 'net.logstash.logback', name: 'logstash-logback-encoder', version: '5.0',{ | |
exclude group: 'ch.qos.logback', module: 'logback-core' | |
} | |
} | |
//... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- app/src/main/assets/logback.xml --> | |
<?xml version="1.0" encoding="UTF-8"?> | |
<configuration> | |
<appender name="stash" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> | |
<!-- Change te host! --> | |
<destination>10.77.41.34:9203</destination> | |
<!-- encoder is required --> | |
<encoder class="net.logstash.logback.encoder.LogstashEncoder" /> | |
</appender> | |
<appender name="logcat" class="ch.qos.logback.classic.android.LogcatAppender"> | |
<tagEncoder> | |
<pattern>%logger{12}</pattern> | |
</tagEncoder> | |
<encoder> | |
<pattern>[%-20thread] %msg</pattern> | |
</encoder> | |
</appender> | |
<root name="testowy" level="DEBUG"> | |
<appender-ref ref="logcat" /> | |
</root> | |
<root name="stas" level="WARN"> | |
<appender-ref ref="stash" /> | |
</root> | |
</configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input { | |
tcp { | |
port => 9203 | |
codec => json_lines | |
type => "android" | |
} | |
} | |
filter { | |
if [type] == "android" { | |
mutate{ | |
add_field => { | |
"service" => "mobile" | |
} | |
remove_field => [ "timestamp" ] | |
} | |
} | |
} | |
output { | |
elasticsearch { hosts => ["localhost:9200"] } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.slf4j.MDC; | |
//Logging | |
LoggerFactory.getLogger(MainActivity.class).warn("Hello"); | |
//Add optional fields in onCreate method (in Activity or Application): | |
MDC.put("version_android", Build.VERSION.RELEASE); | |
MDC.put("version_name", BuildConfig.VERSION_NAME); | |
MDC.put("version_code", String.valueOf(BuildConfig.VERSION_CODE)); | |
MDC.put("application_id", BuildConfig.APPLICATION_ID); | |
MDC.put("user", "test_user); | |
//LOG all uncought excpetion in application | |
private void registerExceptionLogger() { | |
final Thread.UncaughtExceptionHandler old = Thread.getDefaultUncaughtExceptionHandler(); | |
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { | |
@Override | |
public void uncaughtException(Thread thread, Throwable e) { | |
LOG.error("Application stopped ", e); | |
if (old != null) { | |
old.uncaughtException(thread, e); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment