log - a drop-in replacement for android Log class
source link: https://zserge.com/posts/log/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
log - a drop-in replacement for android Log class
Once upon a time I was making a pretty large app for Android. When everything was almost finished I wondered how shall I remove all my logs before publishing the app? Looks like many other developers are interested in the same question as well.
Surprisingly, the recommented way out is to strip off the Log class from the production builds using ProGuard. Maybe Proguard is nice, but to me it’s really weird why instead of having a nice out-of-box API for logging I need to modify my byte code with some 3rd party tool? I’m not the only one thinking like that, Jake Wharton also seems to dislike Proguard.
meet trikita.log
So I decided to make a tiny logger that would be backwards compatible with android.util.Log but provided some extra functionality, common for other modern loggers. You may look at the results here:
You can build it from the sources, or install it from Maven Central or from JCenter.
Features
First, you can intergrate it into your current code with just replacing the imports (from android.util.Log to trikita.log.Log). AFAIK, no other logger can do this.
Next, you can control your log levels and output mechanisms. Log.level(level)
sets the minimally allows logging level. Log.useLog()
enables/disabled
logging output via Android Log class. Log.usePrintln()
uses
System.out.println() for output, which makes it compatible with normal
non-android JVMs.
Finally, you can log multiple values without the annoying string concatenation. You can log using commas or using the format string:
Log.d(tag, "user", user, "action", userAction);
Log.d(tag, "user %s, action code %d", user, actionCode);
Of course ‘tag’ is optional, by default it will be set to the class name, or to the string value of the tag field of the class, so the above can be written as:
Log.d("user", user, "action", userAction);
Log.d("user %s, action code %d", user, actionCode);
Talking of tag fields, if your class has a field called “tag” or “TAG” - it
will be used to provide the tag name. If you have a different tag field - you
can add it to the list using Log.useTags
.
What about Timber?
I like squareup tools. But timber was not as easy to migrate to as trikita.log.Log is, also timber only allowed formatted strings, while I personally prefer comma-separated values.
How to start using it with Android Studio?
In the project pane right-click on the app/java directory (or any other directory containing your java classes). In the popup menu select “Replace in Path…” option.
‘Text to find’ should be “android.util.Log”. ‘Replace with’ should be “trikita.log.Log”.
Click Find, click All files. You’re almost done.
Your imports are correct, but the library is not added in Gradle (unless you did it manually). Add this to your app’s build gradle:
dependencies {
compile 'co.trikita:log:1.1.1'
}
Now it should work, enjoy!
What’s next?
Log is stable enough, you can start using it right now. It’s distributed under MIT license, which means it’s absolutely free.
I’ve recently added the support of long messages (over 4K), so you can now dump your large JSONs or HTTP responses and they won’t be trimmed.
Maybe I shall make the “tag” field more customizable, like it could also be a method, then it would be evaluated on each call letting you print file names, line numbers and any other relevant information you need.
Also if you have any feature suggestions or found a bug - please report it to the issue tracker.
I hope you’ve enjoyed this article. You can follow – and contribute to – on Github, Twitter or subscribe via rss.
Jun 14, 2015
Recommend
-
13
README.md A drop-in, lightweight gRPC replace...
-
5
trash-d A near drop-in replacement for rm that uses the Freedesktop trash bin. Written in the D programmi...
-
4
Stream now supports SRT as a drop-in replacement for RTMP 03/10/2022
-
27
passive Nmap like scanner built with shodan.io Smap is a replica of Nmap which uses shodan.io's free API for port scanning. It takes same command line arguments as Nmap and produces the same output which makes it a drop-in rep...
-
7
About What are Bunny Fonts? Bunny Fonts is an open-source, privacy-first web font platform designed to put privacy back...
-
3
StyleSheet React-native-dark aims to be a slim, drop-in replacement for React Native's StyleSheet.create method Jul 13, 2022
-
9
JCOF: JSON-like Compact Object Format A more efficient way to represent JSON-style objects. About JCOF tries to be a drop-in replacement for JSON, with most of the same semantics, but with a much more compact representatio...
-
2
nix-serve-ng: A faster, more reliable, drop-in replacement for nix-serve nix-serve-ng Our team at Arista Networks is happy to an...
-
5
Open source FerretDB offers ‘drop-in replacement’ for MongoDB FerretDB 1.0 is a stateless proxy that converts MongoDB protocol queries to SQL, using Post...
-
2
Freenet 2023: a Drop-in Decentralized Replacement for the Web B...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK