MyLog

Additional

Language
Java
Version
N/A
Created
Oct 31, 2017
Updated
Dec 24, 2019
Owner
Alexey Korolev (Pulimet)
Contributor
Alexey Korolev (Pulimet)
1
Activity
Badge
Generate
Download
Source code

Announcement

MyLog - Library

Simple android logger (2019)

Java version:

Kotlin version:

Installation

  • Add the dependency from jCenter to your app's (not project) build.gradle file:
repositories {
    jcenter()
}

dependencies {
    implementation 'net.alexandroid.utils:mylog:1.5'

    // Kotlin version
    implementation 'net.alexandroid.utils:mylogkt:1.9'
}
  • Consider adding following proguard rule:
# Remove all log* methods from prpject
-assumenosideeffects class net.alexandroid.utils.mylogkt.MyLogKtKt { *; }
# Remove specific logger methods
-assumenosideeffects class net.alexandroid.utils.mylogkt.MyLogKtKt {
    public static *** logD$default(...);
    public static *** logW$default(...);
}
  • Next:
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        
        // Java (Mandatory)
        MyLog.init(this, "MyLog", BuildConfig.DEBUG); // Context,  Tag,   Show logs?
        
        //Kotlin from Java (Optional, to allow isPackageNameVisible = true)
        MyLogKt.INSTANCE.setPackageName(getPackageName());
        
        // Kotlin from Kotlin (Optional, to allow isPackageNameVisible = true)
        MyLogKt.packageName = packageName
        MyLogKt.isLogsShown = BuildConfig.DEBUG

        // Other Koltin version configurations
        var packageName = ""
        var isLogsShown = true
        var isThreadNameVisible = false
        var isTimeVisible = true
        var isPackageNameVisible = false
        var isClassNameVisible = true
        var isMethodNameVisible = true

        val classNameLength = 15
        val packageAndClassNameLength = 35
        val methodNameLength = 15
        val threadNameLength = 6
        val timeFormat = "HH:mm:ss.SSS"
    }
}
  • Also don't forget to add: android:name=".MyApplication" at your application tag in AndroidManifest.xml
 <application
        android:name=".MyApplication"
        ...>

How I use it

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    // Java lib
    MyLog.setTag("STATIC CUSTOM TAG");
    MyLog.d("Empty 1");
    MyLog.i("Empty 2");
    MyLog.w("Empty 3");
    MyLog.e("Empty 4");

    MyLog.d("CustomTag", "Custom tag example message");

    MyLog.e("Show Exception", new NullPointerException());
    
    // Kotlin lib
    MyLogKt.tag = "STATIC CUSTOM TAG"
    logD("Empty 1")
    logI("Empty 2")
    logW("Empty 3")
    logE("Empty 4")

    logD("Custom tag example 1", "CustomTag1")

    logE("Show Exception", t = NullPointerException())
}

Release notes

1.9 (kotlin version) - Allow logging functions to be empty 1.8 (kotlin version) - Remove init, thread name instead of tread id 1.7 (kotlin version) - Koltin library bug fixes 1.5 - Bug fix + Kotlin version 1.4 - Custom tag support 1.3 - AndroidX migration, Target 29 1.2 - Add MyLog.e(String msg, Throwable t) - (by @davidHarush), 2 new constructors

Stage 1 - Remove logcat headers

Usualy logs look like below:

First go to Configure Logcat Header:

Uncheck all options and press OK:

Now your logs looks like below:

Stage 2 - Customize logs color scheme

File -> Settings -> Editor -> Colors & Fonts -> Android Logcat

Stage 3 - Library customization

Default:

MyLog.setPackageNameVisibility(true); 
MyLogKt.isPackageNameVisible = true

MyLog.setIsTimeVisible(false);
MyLogKt.isTimeVisible = false

MyLog.setThreadIdVisibility(true); 
MyLogKt.isThreadIdVisible = true





License

Copyright 2017 Alexey Korolev

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.