How to using the log?

Put your problem here if it does not fit any of the other categories.

How to using the log?

Postby devent » Sat Dec 26, 2009 10:00 am

Hi,
I have started an Android project and I can't figure out how to get any log output. My code is simple, like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. private static final String TAG = "gsclock.ConfigHandler";
  3.  
  4.  
  5.  
  6. // ...
  7.  
  8.                 if (Log.isLoggable(TAG, Log.DEBUG)) {
  9.  
  10.                         Log.d(TAG, "Log message");
  11.  
  12.                 }
  13.  
  14.  
  15.  
  16.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


How can I see this log message in logcat? I using the adb logcat like
Code: Select all
adb logcat


But I never see the log output and in the debugger the if statement is not executed, i.e. [font=Courier New]Log.isLoggable(TAG, Log.DEBUG)[/font] is false.
devent
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Oct 24, 2008 3:51 am

Top

Postby hellshady00 » Sun Dec 27, 2009 2:09 am

I havent tried this, but i believe you need to use
adb shell setprop gsclock.ConfigHandler DEBUG

then to get debug messages specficially for your app, use
adb logcat gsclock.ConfigHandler:D

Again, i havent tried this, so sorry if it doesnt work.
User avatar
hellshady00
Developer
Developer
 
Posts: 29
Joined: Thu Dec 24, 2009 11:29 pm
Location: Florida

Postby hellshady00 » Sun Dec 27, 2009 2:24 am

Sorry about that, i just tried it, its actually
adb logcat gsclock.ConfigHandler:D *:S

to filter out everything except logs for that tag. (the *:S silences all other log output)

after using
adb shell setprop gsclock.ConfigHandler DEBUG
you should be able to see your debug output

but it looks like
Log.isLoggable(TAG, Log.DEBUG)
still returns false, so im not sure about that but i cant see why you would want to use that anyway
User avatar
hellshady00
Developer
Developer
 
Posts: 29
Joined: Thu Dec 24, 2009 11:29 pm
Location: Florida

Postby devent » Sun Dec 27, 2009 9:18 am

hellshady00 wrote:but it looks like
Log.isLoggable(TAG, Log.DEBUG)
still returns false, so im not sure about that but i cant see why you would want to use that anyway


I don't know the mechanism of the Android Log, but if you write something like
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Log.v(TAG, "Hi," + xx + ", this is a log of x");
  3.  
  4.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


That you have every time 3 String constructors and one toString() call, etc, even if the log level is off. So you put a if (Log.isLoggable(TAg, Level)) that there is not so much code to execute.
devent
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Oct 24, 2008 3:51 am

Postby devent » Sun Dec 27, 2009 9:20 am

hellshady00 wrote:after using
adb shell setprop gsclock.ConfigHandler DEBUG
you should be able to see your debug output


Can I use [font=Courier New]adb shell setprop gsclock.* DEBUG[/font]?
devent
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Oct 24, 2008 3:51 am

Postby hellshady00 » Sun Dec 27, 2009 9:24 am

I think so, try it. I guess that would be useful if you had multiple log tags in your app
User avatar
hellshady00
Developer
Developer
 
Posts: 29
Joined: Thu Dec 24, 2009 11:29 pm
Location: Florida

Top

Postby devent » Sun Dec 27, 2009 9:37 am

Strange,
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. (Log.isLoggable(TAG, Log.DEBUG)
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
is returning false even if I set the debug. I gues that's a bug or is it intentend?
devent
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Oct 24, 2008 3:51 am

Postby hellshady00 » Sun Dec 27, 2009 9:59 am

Yea, i mentioned that in a previous post, i havent looked at the documentation for that so im not really sure if its acting as intended or not, but again, im not really sure why you would need to do that. If your going to log something just do it lol, you dont need to check if you can or not, because you always can. I believe its safe to assume isLoggable should always be true, if that really is the intended use. So that if statement is fairly useless the way your trying to use it

Edit: Sorry about that, i was posting from my droid & i missedthe post about you saying its less code to execute, that is a good idea but you really shouldnt have any debugging log output in public versions of your apps anyway. But either way, im not sure why it would still be returning false. I googled it & there are few others asking about this sort of thing, im guessing its a bug
User avatar
hellshady00
Developer
Developer
 
Posts: 29
Joined: Thu Dec 24, 2009 11:29 pm
Location: Florida

Postby devent » Sun Dec 27, 2009 10:27 am

hellshady00 wrote:Yea, i mentioned that in a previous post, i havent looked at the documentation for that so im not really sure if its acting as intended or not, but again, im not really sure why you would need to do that. If your going to log something just do it lol, you dont need to check if you can or not, because you always can. I believe its safe to assume isLoggable should always be true, if that really is the intended use. So that if statement is fairly useless the way your trying to use it

Edit: Sorry about that, i was posting from my droid & i missedthe post about you saying its less code to execute, that is a good idea but you really shouldnt have any debugging log output in public versions of your apps anyway. But either way, im not sure why it would still be returning false. I googled it & there are few others asking about this sort of thing, im guessing its a bug


It's how you use a Logger, like the Apache Log or java.utils.Logger. Even from the Android SDK Doc:
http://developer.android.com/reference/ ... l/Log.html
Tip: Don't forget that when you make a call like

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Log.v(TAG, "index=" + i);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


that when you're building the string to pass into Log.d, the compiler uses a StringBuilder and at least three allocations occur: the StringBuilder itself, the buffer, and the String object. Realistically, there is also another buffer allocation and copy, and even more pressure on the gc. That means that if your log message is filtered out, you might be doing significant work and incurring significant overhead.


Because of that you first test if the log level is even enabled and then you can put your own log messages.
devent
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Oct 24, 2008 3:51 am

Postby devent » Sun Dec 27, 2009 10:32 am

hellshady00 wrote:Edit: Sorry about that, i was posting from my droid & i missedthe post about you saying its less code to execute, that is a good idea but you really shouldnt have any debugging log output in public versions of your apps anyway. But either way, im not sure why it would still be returning false. I googled it & there are few others asking about this sort of thing, im guessing its a bug


It really don't matter if you have debug log in the public version. Debug log should be normally deactivated globally and if someone have trouble with your app you can tell him to enable debug output so you can debug the application.

That is why there is a Log.isLoggable() method in the first place. But it doesn't work somehow.
But thank you very much for the tip how to enable the log. I couldn't find that piece of information anywhere.
devent
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Oct 24, 2008 3:51 am

Postby hellshady00 » Sun Dec 27, 2009 10:46 am

No problem, as a workaround you could try using a debug setting in your app to set a variable
then do something like

Code: Select all
if (debugMode == true) {
  Log.d(TAG, "Debug mode enabled");
}
User avatar
hellshady00
Developer
Developer
 
Posts: 29
Joined: Thu Dec 24, 2009 11:29 pm
Location: Florida

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 19 guests