📜 ⬆️ ⬇️

A simple and obvious replacement for android.util.Log

Once I wrote one project. The project turned out to be big and I wrote it for a long time.
There was everything that could be crammed in - and retrolambda / java8, and a couple of dozen
other libraries (the customer’s greed for new features knew no bounds, and therefore
the number of dependencies grew).

But it is not even about that. It's time to make a release. And it turned out that the project
a lot of logs and it would be nice to remove them from the release build. A well-known
The ProGuard method did not work the first time. With each new "-keep"
The application fell in some new place. So ProGuard had to disable
until better times.

All this time I have not had a feeling that log level management c
using bytecode changes is ridiculous. And then in half an hour I wrote my
primitive logger.
')
Usually, I rarely bring my projects to the end, but here it was so simple - that now it lies on the githaba , if you wish, use it.

Compatible API - the key to simple migration


One of the reasons why I didn't use Timber is because my fingers
used to print Log.d. I also called my class “Log”, and made the methods compatible with “android.util.Log”.

This means that if you want to jump off the standard logger - just
replace imports. This can be done with sed, or with your favorite
IDE.

And what is the advantage?


Well, here are some code snippets showing the main features of the logger:

//          Log.d(tag, "X equals " + x); //          Log.d(tag, "X", x) 


 //     ,        class Foo { public void foo() { Log.d("Hello"); //  'D/Foo: Hello' } } //        "tag" ( "TAG") class Foo { private final static String TAG = "Bar"; public void foo() { Log.d("Hello"); //  'D/Bar: Hello' Log.d(TAG, "Hello"); //  'D/Bar: Hello',  'D/Bar: Bar Hello' } } // ,           class Foo { static { Log.useTags(new String[]{"tag", "TAG", "MYTAG", "_TAG", "iLoveLongFieldName"}); } private final static String _TAG = "Bar"; ... } 


 //   ,     - Exception e = new Exception("foo"); Log.d("Something bad happened", someObject, "error:", e); //       Log .d("First") .d("Second") .d("Third line") //      -     Log.level(Log.I); Log.d("foo"); //    //    ,   String.format() Log.useFormat(true); Log.d("X equals %d", x); //  'X equals 42' //       -      Log.d("Value of X", x); //  'Value of X 42' //     '\n'.    4000 //       .   //   JSON-  HTML Log.d("Hello\nworld"); //  'D/SomeTag: Hello',  'D/SomeTag: world' //     ,    JVM. //       ,    - //  System.out.println //       Log.usePrinter(Log.SYSTEM.true).usePrinter(Log.ANDROID, false).d("hello"); //  System.out,       //      "",   Crashlytics Log.usePrinter(mCrashlyricsPrinter, true); 


Persuaded where to get?


Github sources: github.com/zserge/log

In build.gradle, the library is connected as usual:

 repositories { jcenter() //  mavenCentral() } dependencies { compile 'co.trikita:log:1.1.5' } 

This is how import can be replaced:

 $ find -name "*.java" -type f -exec sed -i 's/import android.util.Log/import trikita.log.Log/g' {} \; 


Logger under the MIT license, use on health. There's one class without
dependencies, only 250 lines, so that your project will not be harder / slower.

Any wishes or bug reports (especially with patches) are welcome!

UPD: Thank you all for the code review and constructive comments. Thanks to you posted version 1.1.5.

Source: https://habr.com/ru/post/263419/


All Articles