Simple android logger (2020)
- Add the dependency from jCenter to your app's (not project) build.gradle file:
repositories {
jcenter()
}
dependencies {
implementation 'net.alexandroid.utils:mylogkt:1.13'
}
- 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 (All the settings below are optional):
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Kotlin from Kotlin (Optional, to allow isPackageNameVisible = true)
MyLogKt.packageName = packageName
MyLogKt.isLogsShown = BuildConfig.DEBUG
MyLogKt.tag = "STATIC CUSTOM TAG"
// Other Kotlin version configurations
MyLogKt.packageName = ""
MyLogKt.isLogsShown = true
MyLogKt.isThreadNameVisible = false
MyLogKt.isTimeVisible = true
MyLogKt.isPackageNameVisible = false
MyLogKt.isClassNameVisible = true
MyLogKt.isMethodNameVisible = true
MyLogKt.isLengthShouldWrap = true
MyLogKt.classNameLength = 15
MyLogKt.packageAndClassNameLength = 35
MyLogKt.methodNameLength = 15
MyLogKt.threadNameLength = 6
MyLogKt.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"
...>
logD("Empty 1")
logI("Empty 2")
logW("Empty 3")
logE("Empty 4")
logV("Empty 5")
// Same functions
debug("Empty 1")
info("Empty 2")
warn("Empty 3")
error("Empty 4")
verbose("Empty 5")
logD("Custom tag example 1", "CustomTag1")
logE("Show Exception", t = NullPointerException())
}
- 1.13(kotlin version) - Added "debug, info, warn, error, verbose" functions
- 1.12(kotlin version) - Allow more configurations
- 1.11(kotlin version) - Refactoring, classes/methods wrapping
- 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
First go to Configure Logcat Header:
Uncheck all options and press OK:
Now your logs looks like below:
File -> Settings -> Editor -> Colors & Fonts -> Android Logcat
MyLog.setPackageNameVisibility(true);
MyLogKt.isPackageNameVisible = true
MyLog.setIsTimeVisible(false);
MyLogKt.isTimeVisible = false
MyLog.setThreadIdVisibility(true);
MyLogKt.isThreadIdVisible = true
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.
- Add the dependency from jCenter to your app's (not project) build.gradle file:
repositories {
jcenter()
}
dependencies {
implementation 'net.alexandroid.utils:mylog:1.5'
}
- Next:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Java (Mandatory)
MyLog.init(this, "MyLog", BuildConfig.DEBUG); // Context, Tag, Show logs?
}
}
@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());
}