aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Logging - entering method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Logging - entering method" Watch "Logging - entering method" New topic
Author

Logging - entering method

Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Has anyone used the entering method from the java.util.logging.Logger class? Should it not behave just the same as other methods such as the info method? It doesn't appear to be behaving the same for me.

When I run my sample code below, the info method call outputs the message to the console, but the entering method call does not output anything to the console. Any ideas why not?

I expected to see a message in my console for the call log.entering("TestLogging", "main").

The JavaDoc for the entering method simply has the following:
This is a convenience method that can be used to log entry to a method. A LogRecord with message "ENTRY", log level FINER, and the given sourceMethod and sourceClass is logged.

So with my log level set to ALL I expected to see a message here (also tried setting the level to FINER, but ALL should log everything!).


SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4907
    
  10

The Logging API in Java is one of the most counter-intuitive APIs I have seen (and worked with). If you add a handler with a level of ALL (or FINEST) you'll see the message you expect to see. When adding logging to my application I also struggled with it a lot. It's not behaving like you expect (as you experienced already).

Just add this snippet to your code (before the call to the entering-method) and be amazed


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Ah, thanks Roel! Now I see the problem. This also works and highlights my misunderstanding:

The root logger has a ConsoleHandler by default. This is what I was relying on to output my message to the console, as all logs go through the root logger. But what I failed to spot was that the ConsoleHandler associated with the root logger is not set up to accept message that are at the level of FINER.

The log message for the entering method is at the level FINER. So whilst my log message for the call to entering made it to the root logger, the ConsoleHandler associated with the root logger just ignored it as it was not set up to accept messages at this log level (it's probably set up to accept messages at INFO level and above - haven't checked!).

My little example above works because of the following. It gets the Handler associated with the root logger, this is the ConsoleHandler (it gets the root logger by passing the empty string into getLogger). It then sets this Handler to accept log messages at ALL log levels.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4907
    
  10

Ah, didn't think about the root logger You would expect in the API something like Logger.getRootLogger instead of Logger.getLogger("")
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Interesting article located here about logging.
Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
just curious is there a easier way to do logging without declaring it at every class start?

Logger log = Logger.getLogger("TestLogging"); <<-- irritating
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4907
    
  10

You can create yourself a utility class which has some convenience methods for logging.
Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160


Example?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4907
    
  10

Are you kidding, dude? You are trying to get the Developer Certification, so you have to show the skills of a true developer. Until now you have shown no skill at all. I'm even doubting if you got the required SCJP certification in a legal manner (you have proven a few times that you don't have any knowledge about the standard Java API)
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Roel De Nijs wrote:You can create yourself a utility class which has some convenience methods for logging.


This is the route I went down too. I created a utility class.
Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160


just curious why can't we do it this way, it prints on the console too

and how do I view the log files ?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4907
    
  10

My utility class just logs to the console, not to seperate files because that might be tricky (which folder do you use, will you have sufficient permission to write to that folder,...)
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Ixus See wrote:and how do I view the log files ?


You mentioned that you are a team lead - I can't believe you asked that question???

Do you not encourage your team members to research, use their intuitive, try and work things out themselves. I hope you do. But I would expect a team lead to lead by example.

It quite obvious that you have made 0% effort in working this out yourself. Logging to a log file is about as fundamental a thing you can get with logging. It's like "Hello, World" in a programming language.

I found the answer in a few seconds by simply searching on Google : java.util.logging "log file". Couldn't you have done the same with this and many of your other posts?

As a bare minimum you should be searching, either on a search engine like Google or the one on the site, before you ask a question.

Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
Sean Keane wrote:
Ixus See wrote:and how do I view the log files ?


You mentioned that you are a team lead - I can't believe you asked that question???

Do you not encourage your team members to research, use their intuitive, try and work things out themselves. I hope you do. But I would expect a team lead to lead by example.

It quite obvious that you have made 0% effort in working this out yourself. Logging to a log file is about as fundamental a thing you can get with logging. It's like "Hello, World" in a programming language.

I found the answer in a few seconds by simply searching on Google : java.util.logging "log file". Couldn't you have done the same with this and many of your other posts?

As a bare minimum you should be searching, either on a search engine like Google or the one on the site, before you ask a question.



ever heard of information explosion or overload? don't assume I didn't Google... I'm asking because I'm looking for answers related to this assignment rather than real world implementation, is very different maybe you are still doing school projects, you will understand my dilemma if you are in my position.

For me at work, we have a module entirely devoted to logging. The developers just need to register its class with the logger and logs its message according and it will save into a log file of its choosing.

Anyway If i do this assignment like how I code during work, I would not have pass because I will check for everything and overload everything. Maybe I'm more careful? so watch your tongue.


Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
Roel De Nijs wrote:My utility class just logs to the console, not to seperate files because that might be tricky (which folder do you use, will you have sufficient permission to write to that folder,...)


Thanks actually this is the answer, I am looking for ;)

Maybe I should ask If i should save the log into log file. =x
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4907
    
  10

Ixus See wrote:Anyway If i do this assignment like how I code during work, I would not have pass because I will check for everything and overload everything.

I assume with "overload" you mean adding extra functionality (and not using overloaded methods). Why do you think you won't pass when you check and overload everything? You spend more time than necessary, but if everything works like it should, you'll certainly pass.
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Ixus See wrote:ever heard of information explosion or overload? don't assume I didn't Google...


Find any tutorial on logging with java.util.logging and you will find the answer to the question you asked. Google for the two most obvious terms which I previously posted and you will find the answer to the question you asked. Nothing to do with information overload. You just didn't look very hard for the answer - if you did you would have found it within seconds.

Ixus See wrote:I'm asking because I'm looking for answers related to this assignment rather than real world implementation


There's no "special" version of java.util.logging for this assignment. If you want to log to a log file in this assignment using java.util.logging you do it precisely the same way that anyone in the "real world" would do it. So that is no excuse for making 0% effort.

Ixus See wrote:Iis very different maybe you are still doing school projects, you will understand my dilemma if you are in my position. For me at work, we have a module entirely devoted to logging. The developers just need to register its class with the logger and logs its message according and it will save into a log file of its choosing.


For you at work, someone else developed a logging API, they did the research and figuring out, you simply use it without having to understand how it works. Yes, I understand that.

Your problem here is that you are doing a developer certification. You are meant to be able to do the research and figure this out for yourself.

Apologies if you are offended. But it is blatantly obvious you made no effort in figuring out how to log to a file. You wanted to know how to do it and the first thing you did was post here to find out how to do it without figuring out yourself. This is how many of your posts come across, I'm sure others would agree.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Logging - entering method
 
Similar Threads
Doubt in Andrew's book
Logger
How to findout invoking class
Reading from XML file