Android logging utility
Fast, easy and poweful logging utility for Android and Java.
All links are clickable and clicking on them will navigate to the referenced method in an IDE:
Quick peek on live templates:
In your dependencies
block in build.gradle
:
implementation 'io.noties:debug:5.1.0'
To start using this library it must be initialized with desired output:
Debug.init(new AndroidLogDebugOutput(/*isDebug*/true)); // BuildConfig.DEBUG can be used
Debug.init()
takes an array or a Collection of outputs.
Debug.init(new AndroidLogDebugOutput(true), new SystemOutDebugOutput(true));
final List<DebugOutput> outputs = /*obtain desired outputs)*/;
Debug.init(outputs);
If proguard is used this configuration can be used to remove all the log calls:
-assumenosideeffects class io.noties.debug.Debug {
public static *** v(...);
public static *** d(...);
public static *** i(...);
public static *** w(...);
public static *** e(...);
public static *** wtf(...);
public static *** trace(...);
public static *** init(...);
}
Debug has all the default Android log levels: VERBOSE, DEBUG, INFO, WARN, ERROR, WTF. Each level has corresponding Debug method:
Debug.v();
Debug.d();
Debug.i();
Debug.w();
Debug.e();
Debug.wtf();
All methods take optional Throwable
and Object...
int value = -1;
try {
value = /* obtrain value */;
Debug.i("obtained value: %d", value);
} catch (Throwable throwable) {
Debug.e(throwable);
Debug.w(throwable, "Exception executing try code block... value: %d", value);
}
If first argument is a String that has valid String.format
modifiers, than String.format
will be used with first argument as a pattern and all the rest arguments as String.format
arguments
Debug.i("%1$d + %1$d = %2$d", 2, (2 + 2)); // -> "2 + 2 = 4"
Debug.i(throwable, "%1$d + %1$d = %2$d", 2, (2 + 2)); // -> "2 + 2 = 4" + throwable stacktrace
Else all arguments will be concatted into one string:
Debug.i("first", 2, true, "forth", null); // -> "first, 2, true, forth, null"
Will print method chain calls that triggered with log call
Debug.trace();
Debug.trace(int maxStack);
Debug.trace(Level level);
Debug.trace(Level level, int maxStack);
There is a file with basic templates: templates_io_noties_debug.xml.
Learn how to add custom live templates to a IDEA IDE
The shortkeys are (works in Kotlin and Java):
dv [tab] -> Debug.v();
dd [tab] -> Debug.d();
di [tab] -> Debug.i();
dw [tab] -> Debug.w();
de [tab] -> Debug.e();
There are also shortkeys to parse current method arguments and prepare a valid Debug.*
call, for example (works only in Java):
void someMethod(int i, double d, String s) {
// try `dii` shortcut
Debug.i("i : %s, d: %s, s: %s", i, d, s); // here is what will be generated
}
These are as follows:
dvv [tab]
ddd [tab]
dii [tab]
dww [tab]
dee [tab]
public interface DebugOutput {
void log(
@NonNull Level level,
@Nullable Throwable throwable,
@NonNull String tag,
@Nullable String message
);
boolean isDebug();
}
Just implement DebugOutput
and pass an instance of it to the Debug.init
call
ru.noties:debug
-> io.noties:debug
ru.noties.debug
-> io.noties.debug
debug-remove
aftifactArrays.deepToString()
DebugOutputContainer
is initialized once (during creation), if a DebugOutput
returns false
from debug (during configuration) it won't be includedru.noties.Debug
as default tag when caller tag cannot be obtained via reflection (better display in logcat)Debug.init()
now takes an array or a Collection of DebugOutput
's (no need to create a DebugOutputFacade
(which is removed BTW)DebugOutput
's must be flattened
in one DebugOutput
use DebugOutputContainer
(which is used by default if Debug.init
is called with multiple outputs)ru.noties.debug.out
packageru.noties.debug.out.DebugOutput
-> ru.noties.debug.DebugOutput
ru.noties.debug.out.AndroidLogDebugOutput
-> ru.noties.debug.AndroidLogDebugOutput
Debug.*
calls now accept an array of arguments (previously one would have to create a formatted string for that: Debug.i("%d, %d, %d, %d", 2, 3, 4, 5);
, now: Debug.i(2, 3, 4, 5);
io.noties.debug.SystemOutDebugOutput
for writing logs to System.out
& System.err
(can be used in plain Java projects, or in test environment)library-ui
module (discountinued)apt-compiler
& apt-annotations
modules (discontinued) Copyright 2017 Dimitry Ivanov ([email protected])
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.