Getting started with AndroidLayoutsGradle for AndroidRecyclerView onClickListenersNavigationViewIntentJSON in Android with org.jsonAndroid StudioResourcesData Binding LibraryExceptionsGetting Calculated View DimensionsAsyncTaskSharedPreferencesEmulatorMaterial DesignLint WarningsServiceStoring Files in Internal & External StorageWebViewProject SDK versionsRecyclerViewGoogle Maps API v2 for AndroidPorterDuff Mode9-Patch ImagesAndroid NDKRecyclerView DecorationsCamera 2 APIViewPagerCardViewHttpURLConnectionSQLiteADB (Android Debug Bridge)ButterKnifeSupporting Screens With Different Resolutions, SizesGlideRetrofit2DialogACRAGreenDAOFormatting StringsNotificationsAlarmManagerFragmentsHandlerCreating Custom ViewsBroadcastReceiverActivitySnackbarRuntime Permissions in API-23 +Logging and using LogcatVectorDrawable and AnimatedVectorDrawableTools AttributesToastInterfacesAnimatorsLocationTheme, Style, AttributeThe Manifest FileParcelableMediaPlayerMultidex and the Dex Method LimitData Synchronization with Sync AdapterMenuInstant Run in Android StudioPicassoBluetooth and Bluetooth LE APIRoboGuiceMemory LeaksUniversal Image LoaderVolleyWidgetsDate and Time PickersIntegrate Google Sign InIn-app BillingFloatingActionButtonContentProviderDagger 2RealmUnit testing in Android with JUnitAndroid VersionsWi-Fi ConnectionsSensorManagerLocalization with resources in AndroidProgressBarCustom FontsVibrationGoogle Awareness APIsText to Speech(TTS)UI LifecycleSpinnerData Encryption/DecryptionTesting UI with EspressoWriting UI tests - AndroidGreenRobot EventBusOkHttpEnhancing Android Performance Using Icon FontsHandling Deep LinksCanvas drawing using SurfaceViewFirebaseCrash Reporting ToolsCheck Internet ConnectivityFacebook SDK for AndroidUnzip File in AndroidAndroid Places APICreating your own libraries for Android applicationsGsonDevice Display MetricsTextViewListViewBuilding Backwards Compatible AppsLoaderProGuard - Obfuscating and Shrinking your codeDetect Shake Event in AndroidTypedef Annotations: @IntDef, @StringDefCapturing ScreenshotsMVP ArchitectureOrientation ChangesXposedSecurityPackageManagerImageViewGesture DetectionDoze ModeAndroid Sound and MediaSearchViewCamera and GalleryCallback URLTwitter APIsDrawablesColorsConstraintLayoutRenderScriptFrescoSwipe to RefreshAutoCompleteTextViewInstalling apps with ADBIntentServiceAdMobImplicit IntentsPublish to Play StoreFirebase Realtime DataBaseImage CompressionEmail ValidationKeyboardButtonTextInputLayoutBottom SheetsCoordinatorLayout and BehaviorsEditTextAndroid Paypal Gateway IntegrationFirebase App IndexingFirebase Crash ReportingDisplaying Google AdsAndroid Vk SdkLocalized Date/Time in AndroidCount Down TimerBarcode and QR code readingOtto Event BusTransitionDrawablePort Mapping using Cling library in AndroidCreating Overlay (always-on-top) WindowsExoPlayerInter-app UI testing with UIAutomatorMediaSessionSpeech to Text ConversionFileProviderPublish .aar file to Apache Archiva with GradleXMPP register login and chat simple exampleAndroid AuthenticatorRecyclerView and LayoutManagersAudioManagerJob SchedulingAccounts and AccountManagerIntegrate OpenCV into Android StudioSplit Screen / Multi-Screen ActivitiesThreadMediaStoreTime UtilsTouch EventsFingerprint API in androidMVVM (Architecture)BottomNavigationViewORMLite in androidYoutube-APITabLayoutRetrofit2 with RxJavaDayNight Theme (AppCompat v23.2 / API 14+)ShortcutManagerLruCacheJenkins CI setup for Android ProjectsZip file in androidVector DrawablesfastlaneDefine step value (increment) for custom RangeSeekBarGetting started with OpenGL ES 2.0+Check Data ConnectionAndroid Java Native Interface (JNI)FileIO with AndroidPerformance OptimizationRobolectricMoshiStrict Mode Policy : A tool to catch the bug in the Compile Time.Internationalization and localization (I18N and L10N)Fast way to setup Retrolambda on an android project.How to use SparseArrayFirebase Cloud MessagingShared Element TransitionsAndroid ThingsVideoViewViewFlipperLibrary Dagger 2: Dependency Injection in ApplicationsFormatting phone numbers with pattern.How to store passwords securelyAndroid Kernel OptimizationPaintAudioTrackWhat is ProGuard? What is use in Android?Create Android Custom ROMsJava on AndroidPagination in RecyclerViewGenymotion for androidHandling touch and motion eventsCreating Splash screenConstraintSetCleverTapPublish a library to Maven Repositoriesadb shellPing ICMPAIDLAndroid programming with KotlinAutosizing TextViewsSign your Android App for ReleaseContextActivity RecognitionSecure SharedPreferencesSecure SharedPreferencesBitmap CacheAndroid-x86 in VirtualBoxJCodecDesign PatternsOkioGoogle signin integration on androidTensorFlowAndroid game developmentNotification Channel Android OBluetooth Low EnergyLeakcanaryAdding a FuseView to an Android ProjectAccessing SQLite databases using the ContentValues classEnhancing Alert DialogsHardware Button Events/Intents (PTT, LWP, etc.)SpannableStringLooperOptimized VideoViewGoogle Drive APIAnimated AlertDialog BoxAnnotation ProcessorSyncAdapter with periodically do sync of dataCreate Singleton Class for Toast MessageFastjsonAndroid Architecture ComponentsJacksonGoogle Play StoreLoading Bitmaps EffectivelyGetting system font names and using the fontsSmartcardConvert vietnamese string to english string Android

Logging and using Logcat

Other topics

Remarks:

Definition

Logcat is a command-line tool that dumps a log of system messages, including stack traces when the device throws an error and messages that you have written from your app with the Log class.

When to use

If you are considering using Java's System.out methods for printing to the console instead of using one of Android's Log methods, then you should know that they basically work the same. However, it is better to avoid using Java's methods because the extra information and formatting provided by Android's Log methods are more beneficial. Also, the System.out print methods are redirected to the Log.i() method.

Useful links

Filtering the logcat output

It is helpful to filter the logcat output because there are many messages which are not of interest. To filter the output, open the "Android Monitor" and click on the drop down on the top-right and select Edit Filter Configuration

Android Monitor

Now you can add custom filters to show messages which are of interest, as well as filter out well-known log lines which can safely be ignored. To ignore a part of the output you may define a Regular Expression. Here is an example of excluding matching tags:

^(?!(HideMe|AndThis))

This can be entered by following this example:

Create new logcat filter

The above is a regular expression which excludes inputs. If you wanted to add another tag to the blacklist, add it after a pipe | character. For example, if you wanted to blacklist "GC", you would use a filter like this:

^(?!(HideMe|AndThis|GC))

For more documentation and examples visit Logging and using Logcat

Logging

Any quality Android application will keep track of what it's doing through application logs. These logs allow easy debugging help for the developer to diagnose what's going on with the application. Full Android Documentation can be found here, but a summary follows:


Basic Logging

The Log class is the main source of writing developer logs, by specifying a tag and a message. The tag is what you can use to filter log messages by to identify which lines come from your particular Activity. Simply call

Log.v(String tag, String msg);

And the Android system will write a message to the logcat:

07-28 12:00:00.759 24812-24839/my.packagename V/MyAnimator: Some log messages
 └ time stamp             |  app.package┘     |    └ any tag  |
     process & thread ids ┘          log level┘               └ the log message

TIP:
Notice the process id and the thread id. If they are the same - the log is coming from the main/UI thread!

Any tag can be used, but it is common to use the class name as a tag:

public static final String tag = MyAnimator.class.getSimpleName();

Log Levels

The Android logger has 6 different levels, each of which serve a certain purpose:

  • ERROR: Log.e()
    • Used to indicate critical failure, this is the level printed at when throwing an Exception.
  • WARN: Log.w()
    • Used to indicate a warning, mainly for recoverable failures
  • INFO: Log.i()
    • Used to indicate higher-level information about the state of the application
  • DEBUG: Log.d()
    • Used to log information that would be useful to know when debugging the application, but would get in the way when running the application
  • VERBOSE: Log.v()
    • Used to log information that reflects the small details about the state of the application
  • ASSERT: Log.wtf()
    • Used to log information about a condition that should never happen.
    • wtf stands for "What a Terrible Failure".

Motivation For Logging

The motivation for logging is to easily find errors, warnings, and other information by glancing at the chain of events from the application. For instance, imagine an application that reads lines from a text file, but incorrectly assumes that the file will never be empty. The log trace (of an app that doesn't log) would look something like this:

E/MyApplication: Process: com.example.myapplication, PID: 25788
                          com.example.SomeRandomException: Expected string, got 'null' instead

Followed by a bunch of stack traces that would eventually lead to the offending line, where stepping through with a debugger would eventually lead to the problem

However, the log trace of an application with logging enabled could look something like this:

V/MyApplication: Looking for file myFile.txt on the SD card
D/MyApplication: Found file myFile.txt at path <path>
V/MyApplication: Opening file myFile.txt
D/MyApplication: Finished reading myFile.txt, found 0 lines
V/MyApplication: Closing file myFile.txt
...
E/MyApplication: Process: com.example.myapplication, PID: 25788
                          com.example.SomeRandomException: Expected string, got 'null' instead

A quick glance at the logs and it is obvious that the file was empty.


Things To Considering When Logging:

Although logging is a powerful tool that allows Android developers to gain a greater insight into the inner working of their application, logging does have some drawbacks.

Log Readability:

It is common for Android Applications to have several logs running synchronously. As such, it is very important that each log is easily readable and only contains relevant, necessary information.

Performance:

Logging does require a small amount of system resources. In general, this does not warrant concern, however, if overused, logging may have a negative impact on application performance.

Security:

Recently, several Android Applications have been added to the Google Play marketplace that allow the user to view logs of all running applications. This unintended display of data may allow users to view confidential information. As a rule of thumb, always remove logs that contain on non-public data before publishing your application to the marketplace.


Conclusion:

Logging is an essential part of an Android application, because of the power it gives to developers. The ability to create a useful log trace is one of the most challenging aspects of software development, but Android's Log class helps to make it much easier.


For more documentation and examples visit Logging and using Logcat

Log with link to source directly from Logcat

This is a nice trick to add a link to code, so it will be easy to jump to the code that issued the log.

With the following code, this call:

MyLogger.logWithLink("MyTag","param="+param);


Will result in:

07-26...012/com.myapp D/MyTag: MyFrag:onStart(param=3)  (MyFrag.java:2366) // << logcat converts this to a link to source!

This is the code (inside a class called MyLogger):

static StringBuilder sb0 = new StringBuilder(); // reusable string object

public static void logWithLink(String TAG, Object param) {
    StackTraceElement stack = Thread.currentThread().getStackTrace()[3];
    sb0.setLength(0);
    String c = stack.getFileName().substring(0, stack.getFileName().length() - 5); // removes the ".java"
    sb0.append(c).append(":");
    sb0.append(stack.getMethodName()).append('(');
    if (param != null) {
        sb0.append(param);
    }
    sb0.append(") ");
    sb0.append(" (").append(stack.getFileName()).append(':').append(stack.getLineNumber()).append(')');
    Log.d(TAG, sb0.toString());
}

This is a basic example, it can be easily extended to issue a link to the caller (hint: the stack will be [4] instead of [3]), and you can also add other relevant information.

Using the Logcat

Logcat is a command-line tool that dumps a log of system messages, including stack traces when the device throws an error and messages that you have written from your app with the Log class.

The Logcat output can be displayed within Android Studio's Android Monitor or with adb command line.

In Android Studio

Show by clicking the "Android Monitor" icon: enter image description here Or by pressing Alt+6 on Windows/Linux or CMD+6 on Mac.

via command line:

Simple usage:

$ adb logcat

With timestamps:

$ adb logcat -v time

Filter on specific text:

$ adb logcat -v time | grep 'searchtext'

There are many options and filters available to command line logcat, documented here.
A simple but useful example is the following filter expression that displays all log messages with priority level "error", on all tags:

$ adb logcat *:E

Generating Logging code

Android Studio's Live templates can offer quite a few shortcuts for quick logging.
To use Live templates, all you need to do is to start typing the template name, and hit TAB or enter to insert the statement.

Examples:

  • logi → turns into → android.util.Log.i(TAG, "$METHOD_NAME$: $content$");
    • $METHOD_NAME$ will automatically be replaced with your method name, and the cursor will wait for the content to be filled.
  • loge → same, for error
  • etc. for the rest of the logging levels.

Full list of templates can be found in Android Studio's settings (ALT+s and type "live"). And it is possible to add your custom templates as well.

If you find Android Studio's Live templates not enough for your needs, you can consider Android Postfix Plugin

This is a very useful library which helps you to avoid writing the logging line manually.

The syntax is absolutely simple:

.log - Logging. If there is constant variable "TAG", it use "TAG" . Else it use class name.

enter image description here

Android Studio usage

  1. Hide/show printed information: Screenshot

  2. Control verbosity of the logging: Log verbosity screenshot

  3. Disable/enable opening log window when starting run/debug application Disable opening log screenshot

Clear logs

In order to clear (flush) the entire log:

adb logcat -c

Syntax:

  • Log.v(String tag, String msg, Throwable tr)
  • Log.v(String tag, String msg)
  • Log.d(String tag, String msg, Throwable tr)
  • Log.d(String tag, String msg)
  • Log.i(String tag, String msg, Throwable tr)
  • Log.i(String tag, String msg)
  • Log.w(String tag, String msg, Throwable tr)
  • Log.w(String tag, String msg)
  • Log.e(String tag, String msg, Throwable tr)
  • Log.e(String tag, String msg)

Parameters:

OptionDescription
-b (buffer)Loads an alternate log buffer for viewing, such as events or radio. The main buffer is used by default. See Viewing Alternative Log Buffers.
-cClears (flushes) the entire log and exits.
-dDumps the log to the screen and exits.
-f (filename)Writes log message output to (filename). The default is stdout.
-gPrints the size of the specified log buffer and exits.
-n (count)Sets the maximum number of rotated logs to (count). The default value is 4. Requires the -r option.
-r (kbytes)Rotates the log file every (kbytes) of output. The default value is 16. Requires the -f option.
-sSets the default filter spec to silent.
-v (format)Sets the output format for log messages. The default is brief format.

Contributors

Topic Id: 1552

Example Ids: 477,5036,14411,14718,16104,17125,17594

This site is not affiliated with any of the contributors.