Showing posts with label Log created from. Show all posts
Showing posts with label Log created from. Show all posts

Sunday, December 03, 2017

Custom Logs

Custom Logs is used to show all information's of logs (like name of class->function->line number->tags and message).




Steps to use Custom Logs in your project.

  1. Crate a project(LogsDemo) in android studio.

  2. Create a class MyLog.Java and update it-


    package pankaj.log.demo;
    
    import android.util.Log;
    
    /**
     * Created by gsolc-05 on 30/7/17.
     */
    
    public class MyLogs {
        static String TAG = "DEMO_LOG";
    
        public static void LogMessage(String message) {
            StackTraceElement l = new Exception().getStackTrace()[1];
            Log.e(TAG, l.getClassName() + "->" + l.getMethodName() + " : " + l.getLineNumber() + " : " + message);
        }
    
    
    }
    
    
    
    
    
     
  3. Create a class MainActivity.Java and update it-


    package pankaj.log.demo;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            MyLogs.LogMessage("HELLO...");
        }
    
        public void helloPrint(View view) {
            MyLogs.LogMessage("HELLO...");
        }
    
        public void hiPrint(View view) {
            MyLogs.LogMessage("HI...");
        }
    }
    
    
    
     
     
  4. Open your main_activity.xml file and update it-


    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="pankaj.log.demo.MainActivity">
    
    
        <TextView
            android:layout_width="match_parent"
            android:paddingTop="20dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Press buttons to print logs" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="helloPrint"
                android:text="PRINT HELLO... LOG" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="hiPrint"
                android:text="PRINT HI... LOG" />
    
    
        </LinearLayout>
    
    
    </android.support.constraint.ConstraintLayout>
    
    
    
    
    

  5. All the application development process with CustomLogs has completed, Now run the app and look the screen. 








  6. Good bye, Thanks to read this blog.