orhanobut / logger Goto Github PK
View Code? Open in Web Editor NEW✔️ Simple, pretty and powerful logger for android
License: Apache License 2.0
✔️ Simple, pretty and powerful logger for android
License: Apache License 2.0
Process: com.jason.loggertest, PID: 31611
Theme: themes:{}
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jason.loggertest/com.jason.loggertest.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Throwable.getMessage()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Throwable.getMessage()' on a null object reference
at com.orhanobut.logger.LoggerPrinter.json(LoggerPrinter.java:169)
at com.orhanobut.logger.Logger.json(Logger.java:86)
at com.jason.loggertest.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
1.public void json(String json){
add if (settings.getLogLevel() == LogLevel.NONE) {
return;
}
.........
}
public void xml(String xml){
add if (settings.getLogLevel() == LogLevel.NONE) {
return;
}
.........
}
2.public enum LogLevel {
FULL,
NONE
+add DEBUG
+add ERROR
.............
}
3.........
I must init tag firstly, is that right?
Logger.init("TAG");
Or, I will get exception of java.lang.NullPointerException
Can you make min sdk to 1? Thanks!
I'm using the method call Logger.t("tag", 0).json(jsonString)
but nothing prints in the logcat.
In case it helps I'm using the following JSON
{"coord":{"lon":-121.49,"lat":38.58},"sys":{"message":0.007,"country":"United States of America","sunrise":1432039821,"sunset":1432091674},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"base":"stations","main":{"temp":279.79,"temp_min":279.79,"temp_max":279.79,"pressure":1018.82,"sea_level":1031.88,"grnd_level":1018.82,"humidity":91},"wind":{"speed":1.51,"deg":167.501},"clouds":{"all":64},"dt":1432033881,"id":5389489,"name":"","cod":200}
I updated to 1.5, in our gradle files (since we need the stack-trace crash fix), but it does not recognise the classes from the library.
Even if I put the .aar
locally in the project it does not work.
1.4 works perfectly, something wrong with the build that were uploaded to mavenCentral?
Consider making the API compatible with android.util.Log
so that switching to this library doesn't involve making too many changes to existing code. For example, consider adding the following even if you don't use the tag
parameter:
public static void d(String tag, String message);
Of course one might argue that users of this library should wrap the call to Logger.d()
within their own utility class if they want to seamlessly switch to it from android.util.Log
. But maybe it is easier if the library provides this out of the box.
As an example, take a look at this Logging library that can be used just by changing the import statements and no other code changes.
i setting it in application oncreate method
Logger.init("MeetingPac"); // default PRETTYLOGGER or use just init()
Logger.d("testa");
the show just is : "09-17 17:15:19.276 11859-11859/com.sqlview.meetingpac D/MeetingPac﹕ ║ testa"
so the thread message?
First of all, congratulations for the library, veeeery awesome :)
Can you add an option to change the TAG only for one log? Something like:
Logger.e("TAG", "Message");
Thank you!
please change android:allowBackup="true" to android:allowBackup="false"
I see that there's a single printer instance saved on a static member in logger.java
so when i do this simultaneously from 2 threads:
thread1 - logger.t("tag1").i("log1")
thread2 - logger.t("tag2").i("log2")
it seems very likely that logs will be written with wrong tags.
or am i missing something?
Hi,
I want to suggest a feature I would like before I completely agree on migrating to Logger
. I think you could implement varargs feature in message string. For example, in Timber, I used to do something like that:
Timber.d("Data: %d %d %d %d %s", left, right, top, bottom, input);
这句代码导致的
private String createMessage(String message, Object... args) {
return args.length == 0 ? message : String.format(message, args);
}
Hi Orhan,
Thats a great project. Cong. I am trying to import this project as jar. Is it possible? Can you provide a jar for lastest version?
Thanks.
Good luck
private void log(int logType, String msg, Object... args) {
if (settings.getLogLevel() == LogLevel.NONE) {
return;
} else {
_log(logType, msg, args)
}
}
/**
* This method is synchronized in order to avoid messy of logs' order.
*/
private synchronized void _log(int logType, String msg, Object... args) {
String tag = getTag();
String message = createMessage(msg, args);
int methodCount = getMethodCount();
logTopBorder(logType, tag);
logHeaderContent(logType, tag, methodCount);
//get bytes of message with system's default charset (which is UTF-8 for Android)
byte[] bytes = message.getBytes();
int length = bytes.length;
if (length <= CHUNK_SIZE) {
if (methodCount > 0) {
logDivider(logType, tag);
}
logContent(logType, tag, message);
logBottomBorder(logType, tag);
return;
}
if (methodCount > 0) {
logDivider(logType, tag);
}
for (int i = 0; i < length; i += CHUNK_SIZE) {
int count = Math.min(length - i, CHUNK_SIZE);
//create a new String with system's default charset (which is UTF-8 for Android)
logContent(logType, tag, new String(bytes, i, count));
}
logBottomBorder(logType, tag);
}
invalid on latest SDK
Love the look of this library. Would be awesome to use with @JakeWharton's Timber library https://github.com/JakeWharton/timber.
alert error When use Logger.d("fdjal")
Attempt to invoke virtual method 'com.orhanobut.logger.LogLevel com.orhanobut.logger.Settings.getLogLevel()' on a null object reference
version: 1.12
Does it only work in AndroidStudio?
Excuse me, would you please tell me how to use this package, how to output the ArrayList and HashMap,
7683-7683/com.guoyoujin.gz.gz E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.util.UnknownFormatConversionException: Conversion: '
at java.util.Formatter$FormatToken.unknownFormatConversionException(Formatter.java:1397)
at java.util.Formatter$FormatToken.checkFlags(Formatter.java:1334)
at java.util.Formatter.transform(Formatter.java:1440)
at java.util.Formatter.doFormat(Formatter.java:1079)
at java.util.Formatter.format(Formatter.java:1040)
at java.util.Formatter.format(Formatter.java:1009)
at java.lang.String.format(String.java:1992)
at java.lang.String.format(String.java:1966)
at com.orhanobut.logger.LoggerPrinter.createMessage(LoggerPrinter.java:346)
at com.orhanobut.logger.LoggerPrinter.log(LoggerPrinter.java:211)
at com.orhanobut.logger.LoggerPrinter.i(LoggerPrinter.java:138)
at com.orhanobut.logger.Logger.i(Logger.java:61)
at com.guoyoujin.gz.gz.fragment.tabfragment.Fragment1$GetNewsDataThread$1.run(Fragment1.java:129)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5463)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:854)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
at dalvik.system.NativeStart.main(Native Method)
private ArrayList list;
public class NewsVo {
private String newsId;
private String title;
private String content;
private String videoAndriodURL;
private String videoIphoneURL;
private NewsImageVo image;
private String date;
public String getNewsId() {
return newsId;
}
public void setNewsId(String newsId) {
this.newsId = newsId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getVideoAndriodURL() {
return videoAndriodURL;
}
public void setVideoAndriodURL(String videoAndriodURL) {
this.videoAndriodURL = videoAndriodURL;
}
public String getVideoIphoneURL() {
return videoIphoneURL;
}
public void setVideoIphoneURL(String videoIphoneURL) {
this.videoIphoneURL = videoIphoneURL;
}
public NewsImageVo getImage() {
return image;
}
public void setImage(NewsImageVo image) {
this.image = image;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "NewsVo{" +
"newsId='" + newsId + '\'' +
", title='" + title + '\'' +
", content='" + content + '\'' +
", videoAndriodURL='" + videoAndriodURL + '\'' +
", videoIphoneURL='" + videoIphoneURL + '\'' +
", image=" + image +
", date='" + date + '\'' +
'}';
}
}
When using Logger.d("Some text",exception) I'm getting the following error:
java util.Formatter.getArgument (Formatter.java:1111)
java.lang.String.format (String.java:1973)
com.orhanobut.logger.LoggerPrinter.createMessage (LoggerPrinter.java:346)
com.orhanobut.logger.LoggerPrinter.log (LoggerPrinter.java:211)
com.orhanobut.logger.LoggerPrinter.d (LoggerPrinter.java:109)
com.orhanobut.logger.Logger.d (Logger.java:49)
Hi there,
The title is pretty obvious. I am be able to print log to console with e.printStackTrace()
when running unit test in android studio but Logger.e(JsonUtils.class.getName(), jsonString, e)
.
Thanks in advance!
As you know, some user really need print the list directory, please add this feature.
Hi thanks very much for this library.
is it possible to write all log to a file along with the standard output?
Thanks
Hey there
I've tried many time to add library into my project but I got below error:
Gradle sync failed: Could not find com.github.User:Repo:Tag.
For the stack trace on the logs I would love to be able to filter them by package (or a substring of the package)
I am thinking a very simple setting call like setStackTraceFilter("org.example.app") then only show stack trace items that contain that string.
/**
* Formats the json content and print it
*
* @param json the json content
*/
@Override
public void json(String json) {
String tag = getTag();
int methodCount = getMethodCount();
if (TextUtils.isEmpty(json)) {
d(tag, "Empty/Null json content", methodCount);
return;
}
try {
if (json.startsWith("{")) {
JSONObject jsonObject = new JSONObject(json);
String message = jsonObject.toString(JSON_INDENT);
d(tag, message, methodCount);
return;
}
if (json.startsWith("[")) {
JSONArray jsonArray = new JSONArray(json);
String message = jsonArray.toString(JSON_INDENT);
d(tag, message, methodCount);
}
} catch (JSONException e) {
d(tag, e.getCause().getMessage() + "\n" + json, methodCount);
}
}
大神,我刚好想下载你的代码学习下RxJava 但是发现已经运行不起来了:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.malin.rengwuxianrxjava, PID: 15271 java.lang.VerifyError: com/malin/rengwuxianrxjava/activity/MainActivity$AjcClosure1 at com.malin.rengwuxianrxjava.activity.MainActivity.rxJavaSolveMiZhiSuoJinAndNestedLoopAndCallbackHell(MainActivity.java:334) at com.malin.rengwuxianrxjava.activity.MainActivity.onCreate(MainActivity.java:133) at android.app.Activity.performCreate(Activity.java:5303) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1091) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2165) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255) at android.app.ActivityThread.access$800(ActivityThread.java:142) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1213) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5217) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:818) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634) at dalvik.system.NativeStart.main(Native Method)
Relates to #28
Is there a way to allow the json string to be logged into a different log level?
Yes, I agree this is a really cool logger. Before I can make the switch I would love to see something like Jake Wharton's Timber's approach:
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
} else {
Timber.plant(new CrashReportingTree());
}
The other suggestion is kind of silly, like most of us I am very lazy and typing
L.v("test2"); or even
Lg.v("test2"); is faster/simpler than typing
Logger.v("test2");
code below in LoggerPrinter.logMethod throws ArrayIndexOutOfBoundsException when methodCount + stackOffset == trace.length
if (methodCount + stackOffset > trace.length) {
methodCount = trace.length - stackOffset - 1;
}
...
builder.append("║ ")
.append(level)
.append(getSimpleClassName(trace[stackIndex].getClassName())) // ArrayIndexOutOfBoundsException
.append(".")
.append(trace[stackIndex].getMethodName())
.append(" (")
.append(trace[stackIndex].getFileName())
.append(":")
.append(trace[stackIndex].getLineNumber())
.append(")");
possible solution
methodCount + stackOffset > trace.length
may be
methodCount + stackOffset >= trace.length
What is latest gradle version of lib? 1.12 does not work
thank you for this lib!
this makes logcat 1000000x better.
is there anyway I can get this same look and feel remotely?
thanks man!
please, sometimes the json and xml can be loooooooong
I'm using https://github.com/JakeWharton/timber, then I have a question.
The log :
RequestListener.onRequestSuccess (RequestListener.java:46)
║ Timber.d (Timber.java:23)
║ Timber$1.d (Timber.java:136)
║ Timber$Tree.d (Timber.java:245)
║ Timber$Tree.prepareLog (Timber.java:319)
║ PrettyDebugTree.log (PrettyDebugTree.java:33)
How to get rid of :
║ Timber.d (Timber.java:23)
║ Timber$1.d (Timber.java:136)
║ Timber$Tree.d (Timber.java:245)
║ Timber$Tree.prepareLog (Timber.java:319)
║ PrettyDebugTree.log (PrettyDebugTree.java:33)
I just want : RequestListener.onRequestSuccess (RequestListener.java:46)
java.lang.NoClassDefFoundError: com.orhanobut.logger.LoggerPrinter
at com.orhanobut.logger.Logger.(Logger.java:11)
at com.llkj.youban.MyApplication.onCreate(MyApplication.java:26)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4394)
at android.app.ActivityThread.access$1600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1273)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5072)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Thanks very much for this library.
Is it possible to support slf4j?
Thanks
public void getBonusData() {
String url = HttpUrl.CLIST;
Map<String, String> args = new HashMap<>();
args.put("type", "1");
HttpRequest httpRequest = new HttpRequest("CLIST_BONUS");
httpRequest.postRequest(url, args, new HttpResponse() {
@Override
public void onSucceedResponse(String response) {
Logger.json(response);
}
@Override
public void onErrorResponse(VolleyError error) {
Logger.t("CLIST_BONUS").d(error.toString());
}
});
}
12-22 16:05:11.341 12284-12284/com.hangzhuangyuan.employee E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hangzhuangyuan.employee, PID: 12284
java.lang.NullPointerException
at com.orhanobut.logger.LoggerPrinter.log(LoggerPrinter.java:204)
at com.orhanobut.logger.LoggerPrinter.d(LoggerPrinter.java:110)
at com.orhanobut.logger.LoggerPrinter.json(LoggerPrinter.java:160)
at com.orhanobut.logger.Logger.json(Logger.java:86)
at com.hangzhuangyuan.employee.fragment.BonusFragment$1.onSucceedResponse(BonusFragment.java:52)
at com.hangzhuangyuan.employee.network.HttpRequest$1.onResponse(HttpRequest.java:33)
at com.hangzhuangyuan.employee.network.HttpRequest$1.onResponse(HttpRequest.java:30)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5094)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:702)
at dalvik.system.NativeStart.main(Native Method)
Error:(8, 9) Execution failed for task ':app:processDebugManifest'.
Manifest merger failed : Attribute application@allowBackup value=(false) from AndroidManifest.xml:8:9
is also present at com.orhanobut:logger:1.10:11:18 value=(true)
Suggestion: add 'tools:replace="android:allowBackup"' to element at AndroidManifest.xml:7:5 to override
Error:Artifact 'logger.aar (com.github.orhanobut:logger:v1.8-tag)' not found.
Searched in the following locations:
https://jitpack.io/com/github/orhanobut/logger/v1.8-tag/logger-v1.8-tag.aar
I'm getting an issue where, because a lot my classes extend some base class, most of my logs from Logger are reported with the base class name, which is not very helpful.
Please let me know if there's any easy way around this
For the t("") function it should use the the user defined value and not the default value of 2
Currently implementation only shows the reason of the cause. But It should show more information regarding to the issue. Stacktrace can be printed in a propery way.
Hi Orhan,
I need a proguard config. Without it, cant use library because Logger.init() function throws method not found exception. Can you provide one?
Thanks Emre
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.