Let’s imagine you’re looking for a flight ticket booking app for your Android smartphone. When you search for such an app in the Play Store, you’ll find multiple options. How do you decide which one to install? Easy! You try out a few of them. Find out which one is fastest, serves your purpose, and has a nice user interface.
Now, what if you’re a developer? You just released an app. The app has a nice UI and has all the features a user might demand. But, for some reason, the app is a bit slow. If a user comes across your app, because of slow speed, they won’t hesitate to uninstall the app. So how do you deal with this?
The answer is monitoring the app’s log. Let’s walk through what you can do with log monitoring and how to do it with SolarWinds® Papertrail™.
Why Do You Need a Log Monitoring Tool?
In today’s world, competition among applications is fierce. With the millions of apps already available in the app store, users are presented with many alternatives. So, how can you make your app stand out and be successful? The best way is with high customer ratings, which means providing a top-notch user experience.
With UX and UI testing, you can usually catch and fix any front-end bugs. But what about the back end? To identify back-end issues that could impact the app performance or cause a bad user experience, you need to monitor your apps. Monitoring the types of errors and the volume of errors can help you identify bottlenecks and tune your app to improve performance. When there’s a service issue, a good log monitoring tool can notify you before your users notice a problem.
Using Papertrail to View and Monitor Android Logging
You can no longer manage log entries a spreadsheet, or remote in to individual servers for log inspection. The number of applications and services generating logs and the high volume of log messages mean you need to turn to an automated solution. A cloud-based log management service, like Papertrail, is perfect for this.
Papertrail was built by engineers for engineers, so it works the way developers do. It’s easy to use, supports almost every type of log from Android and IOS to devices and servers, and has a familiar search interface. You can set custom alerts right and save searches right from the search bar (no need to click on another menu).
How to Set Up
There are two ways to send Android logs to Papertrail.
- The logback-syslog4j method
- The logback-android method
The best way is with the logback-syslog4j method. It includes TCP, TCP with TLS, and Google App Engine support, and uses syslog4j for message transmission.
To use logback in the app, you need do a few simple steps.
Setup Using the logback-syslog4j Method
1. First, add the following lines in the dependency list of the app’s Gradle:
compile 'org.slf4j:slf4j-api:1.7.25'
compile 'com.github.tony19:logback-android:1.1.1-12'
compile ('com.papertrailapp:logback-syslog4j:1.0.0') {
exclude group: 'ch.qos.logback'
}
2. Update your configuration file. Here is an example:
<configuration>
<appender name="syslog-tls" class="com.papertrailapp.logback.Syslog4jAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{MMM dd HH:mm:ss} Android YOUR_APP: %-5level %logger{35} %m%n</pattern>
</layout>
<syslogConfig class="org.productivity.java.syslog4j.impl.net.tcp.ssl.SSLTCPNetSyslogConfig">
<host>logsN.papertrailapp.com</host>
<port>XXXXX</port>
<sendLocalName>false</sendLocalName>
<sendLocalTimestamp>false</sendLocalTimestamp>
<maxMessageLength>128000</maxMessageLength>
</syslogConfig>
</appender>
<appender name="async" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="syslog-tls" />
</appender>
<root level="INFO">
<appender-ref ref="async" />
</root>
</configuration>
You’ll need to change YOUR_APP within the <pattern> tag, LogsN in the <host> tag, and the XXXXX in <port>.
YOUR_APP is the name you’re going to give to your Android app. LogsN and your port information are available in the destinations.json file. You can view it manually under the Log Destinations or send the following command to retrieve the file:
$ curl -v -H "X-Papertrail-Token: abc123" https://papertrailapp.com/api/v1/destinations.json
If you have minifyEnabled true in ProGuard, in the proguard-rules.pro file, add the following line.
-keep class org.productivity.java.syslog4j.** { *; }
If you skip it, ProGuard will remove the Syslog4j classes, thus resulting in the following runtime error:
java.lang.ClassNotFoundException: Didn't find class "org.productivity.java.syslog4j.impl.net.tcp.ssl.SSLTCPNetSyslogConfig"
That’s it. Your setup is ready.
Send Your App Logs
To show you what this will look like when you start sending your own logs, I ran the following code that I know will throw an exception.
public class MyActivity extends AppCompatActivity {
private Logger log;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
log = LoggerFactory.getLogger(MyActivity.class);
try {
String test = null;
test.toString();
} catch (Exception ex){
log.error('Client error : ',ex.getMessage());
}
}
}
I have declared the test as null, so the test.toString() function is going to throw a nullpointer exception.
The event message in Papertrail looks like this.
03-14 16:54:19.854 4145-4145/com.demo E/Client error : Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
You can get the same level of detail on log messages generated by back-end services. Here is a sample message for HTTP 500 error.
03-14 17:54:19.854 8181-8212/com.demo E/Server Error: org.apache.http.client.HttpResponseException: INTERNAL SERVER ERROR
Ensure Smooth Operations of Your Android Apps With Papertrail
You only get one shot to wow customers. An overlooked error or missing image, and your app’s page in the Play Store could be filled with negative reviews. The best way to avoid that is to test thoroughly and monitor your app to identify issues before users notice them. Sign up for a free trial of Papertrail and start actively managing your Android app logs today.
This post was written by Arnab Roy Chowdhury. Arnab is a UI developer by profession and a blogging enthusiast. He has strong expertise in the latest UI/UX trends, project methodologies, testing, and scripting.