[JAVA] I want to simplify the log output on Android

On Android, you can output the log by writing Log.d (TAG," some log text ... "), but it was troublesome to declare the TAG variable every time, so depending on the caller. I thought it would be convenient if the file name and line number were automatically acquired and TAG was set. ** It's done. ** Create a class in Texto and create a method like the following. Let's call it the Logger class.

Logger.java


public class Logger {
  public static void log(String text) {
    StackTraceElement elem = Thread.currentThread().getStackTrace()[2];
    String tag = elem.getFileName();
    Log.d(tag, text);
  }
}

Then call this method where you want to spit out the log.

Logger.log("hello world");

Output example


09-15 19:52:03.270 15378-15378/com.niusounds.myapps D/MainActivity.java: hello world

Easy! !!

By the way, calling the same code from Kotlin gave different results. In the case of Kotlin, it seems that you have to get Thread.currentThread (). StackTrace [3].

Log.kt


fun log(text: String) {
    val caller = Thread.currentThread().stackTrace[3]
    val tag = "${caller.fileName}:${caller.lineNumber}"
    Log.d(tag, text)
}

Recommended Posts

I want to simplify the log output on Android
I want to change the log output settings of UtilLoggingJdbcLogger
I want to output the day of the week
I want to embed any TraceId in the log
[Android Studio] I want to use Maven library on Android
I want to use screen sharing on the login screen on Ubuntu 18
(ยด-`) .. oO (I want to easily find the standard output "Hello".
I want to put the JDK on my Mac PC
I want to simplify the conditional if-else statement in Java
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
I want to display background-ground-image on heroku.
I want to install PHP 7.2 on Ubuntu 20.04.
I want to add the disabled option to f.radio_button depending on the condition
[Android] I want to get the listener from the button in ListView
[Ruby] I want to output only the odd-numbered characters in the character string
I want to var_dump the contents of the intent
I want to truncate after the decimal point
I want to get the value in Ruby
I want to play a GIF image on the Andorid app (Java, Kotlin)
I want to judge the range using the monthly degree
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to dark mode with the SWT app
I want to call the main method using reflection
[Rough commentary] I want to marry the pluck method
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to add a delete function to the comment function
Rspec: I want to test the post-execution state when I set a method on subject
I want to output the character number from the left where an arbitrary character string appears.
I want to go back to a specific VC by tapping the back button on the NavigationBar!
I want to see only the latest because the Docker log has become too large
I want to morphologically analyze the log in the DB and put it in the DB to classify messages 1
I want to place RadioButtons in the same RadioGroup at any position on the screen.
I want to hit the API with Rails on multiple docker-composes set up locally
I want to convert characters ...
[Beginner] I want to modify the migration file-How to use rollback-
I tried adding a separator line to TabLayout on Android
I want to set the conditions to be displayed in collection_check_boxes
[Rails] [bootstrap] I want to change the font size responsively
I want to bring Tomcat to the server and start the application
I want to expand the clickable part of the link_to method
I was addicted to looping the Update statement on MyBatis
I want to call a method and count the number
I want to use the Java 8 DateTime API slowly (now)
I want to create a form to select the [Rails] category
I want to use the sanitize method other than View.
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to give a class name to the select attribute
Correct log output method on Android (standard Log class vs AnkoLogger)
I want to recursively search the class list under the package
I want to distinct the duplicated data with has_many through
I want to transition to the same screen in the saved state
I want to narrow down the display of docker ps
I want to return multiple return values for the input argument
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
I want to pass the startup command to postgres with docker-compose.
[Java] I want to test standard input & standard output with JUnit
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
Create an Android app for those who don't want to play music on the speaker