Last active
April 20, 2020 17:57
-
-
Save mzelzoghbi/b5ab4a96f8434f27ef706a943722e82b to your computer and use it in GitHub Desktop.
Simple logger class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.instabug.my_awesome_library; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
/** | |
* Basic class for logging instead of using Log class directly. | |
*/ | |
public class Logger { | |
private static LoggerImpl logger; | |
public static boolean isEnabled = true; | |
static { | |
logger = new LoggerImpl(); | |
} | |
/** | |
* Public api to enable user to print messages to logcat | |
* | |
* @param tagOrMsg you can pass here tag if you want to customize the tag string or directly | |
* pass the message and the tag in this case will be {MyLogger} | |
* @param msg If you passed a tag in tagOrMsg, you will need to pass here msg. | |
* <p> | |
* Usage Example: | |
* Logger.d("Network", "Network called started"); -> this will print a normal log | |
* msg to logcat with a tag Network and msg "Network called started" | |
* <p> | |
* Logger.d("call method called"); -> this will print a log msg with tag "MyLogger" | |
* and msg "call method called". | |
* <p> | |
* Logger.d("tag" , "msg1", "msg2"); -> this will print a log msg with tag | |
* "tag" and messages "msg1 - msg2" | |
*/ | |
public static void d(@NonNull String tagOrMsg, @Nullable String... msg) { | |
if (isEnabled && logger != null) { | |
logger.logd(tagOrMsg, msg); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.instabug.my_awesome_library; | |
import android.util.Log; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
class LoggerImpl { | |
private final String TAG = "MyLogger"; | |
public void logd(@NonNull String tagOrMsg, @Nullable String... msgs) { | |
if (msgs == null) { | |
Log.d(TAG, tagOrMsg); | |
} else if (msgs != null) { | |
StringBuilder messageBuilder = new StringBuilder(""); | |
for (int i = 0; i < msgs.length; i++) { | |
messageBuilder.append(msgs[i]); | |
if (msgs.length - 1 > i) | |
messageBuilder.append(" - "); | |
} | |
Log.d(tagOrMsg, messageBuilder.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment