This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Logging - How many instances of File Handler? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Logging - How many instances of File Handler?" Watch "Logging - How many instances of File Handler?" New topic
Author

Logging - How many instances of File Handler?

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Good Morning Guys and Gals,

I know its overkill but for my own practive I want to have good logging in the B&S application.

I plan to read a logging level from the properties file and then create a file handler. (http://www.roseindia.net/java/example/java/util/WriteRecordsToLogFile.shtml)

My questions are:

1 - should each class create its own instance of the file handler or should one instance of the file handler be created and passed to each class?

2 - if no file location is specified for the log-file.txt in the properties file, should i just create one at the root directory or simply print all my logs to the console?

Thanks,

G


OCPJP 6, OCMJD
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hi Glen,
Glen Iris wrote:1 - should each class create its own instance of the file handler or should one instance of the file handler be created and passed to each class?

Since there will be only one log file (I hope so), it makes more sense to have a universal file handler (maybe a single instance throughout the application).
Glen Iris wrote:2 - if no file location is specified for the log-file.txt in the properties file, should i just create one at the root directory or simply print all my logs to the console?

Creating a file at root level is risky. What if the application is run by a non-root user, or what if application is run on Windows OS? It would be safe to create log file in same directory(as application), or create log dir etc.

However, please note that logging is not a 'must' requirement for OCMJD, and you won't get any extra points by implementing logging. However, you may loose points if logging causes any issue in application.

By the way, I did not implemented any logging (just print statements during development).

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 739
System.getProperty("user.dir") returns the current working directory.
Create your properties file in that directory and save your database file location there.
The study guide shows an example.

System.getProperty("user.dir") is platform independent.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5218
    
  12

Because logging is not a must requirement, there is no need for logging. I used logging for debugging purposes, so I left these statements in my code, but disabled all logging when I submitted (I also mentioned this one in choices.txt). And I kept it simple by just using a ConsoleHandler (just because to avoid all issues with writing rights).


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

Joined: Jul 13, 2011
Posts: 164

Thank you chaps
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Good Morning Again guys,

I've decided to use logging during development and then switch it off before submission.

At the moment my FileHandler is printing out the log files in xml format to my log.txt file. How can I get them to print out to the file in the format that they print to the console?

Also, how can I get them to not print to the console?



G
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5218
    
  12

Glen Iris wrote:At the moment my FileHandler is printing out the log files in xml format to my log.txt file. How can I get them to print out to the file in the format that they print to the console?

Did you look at FileHandler API? I don't think so, because it's clearly stated. And I really don't like a developer who's too lazy to search for himself. If you don't know it, doing a bit of investigation yourself (google, java api,...) should always be an innate reflex! Please ShowSomeEffort.

Glen Iris wrote:how can I get them to not print to the console?

Again Google to the rescue. The first hit of googling with "java logging turning off console" will solve your problem (and gives you better insight in how the logging capabilities are setup).
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 739
Do you have the SCJD study guide?
By the way, you may find the Denny's DVD source code from Apress's web site.
Try log.log(....) and see if the log prints out on the console.
You may not need LogFileHandler.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5218
    
  12

Himai Minh wrote:You may not need LogFileHandler.

If he wants to log to a file (like he indicates in his first post), he absolutely needs a FileHandler.
Dennis Grimbergen
Ranch Hand

Joined: Nov 04, 2009
Posts: 140

For this exam....forget about logging, as it is not needed.
You have enough other things to focus on!


SCJP, SCWCD, SCJD
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Hey Guys,

Thanks again for the advice. Hi Dennis, I know I dont need logging for the SCJD but its been something I have been meaning to master for a while now so I think this is a good time. I will probably switch it off before submission anyway.

Roel, come on man, I get up at 6am each morning now to work on this assignment before I go to work. I spent quite a while looking for the info I requested. I found solutions here and there and was afraid my code was a pick and mix of things that didnt make sense. Thats why I posted my code with my question. I understand that people can abuse these forums but I am not one of them. I do appreciate your help and am very grateful to have you help me out. Even by you posting "java logging turning off console" has helped me because sometimes half the battle is knowing what to google.

Yes Himai Minh, I do have the study guide and log.log does print to the console. When I master the solution I will update this thread so others can use it too if they wish.

G
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5218
    
  12

Glen Iris wrote:its been something I have been meaning to master for a while now so I think this is a good time.

I don't think you want to master JUL (java util logging), because it's (like you already discovered) a very hard to use API, not very intuitive (and as far as I know hardly used in real development). Learning (and getting familiar with) the concepts of logging (parent loggers, different handlers,...) is a good thing, because you have the same (or similar) ones with the logging frameworks (commons-logging, log4j,...) but these ones you can't use for the assignment (because these are not part of the SDK).

Glen Iris wrote:Roel, come on man, I get up at 6am each morning now to work on this assignment before I go to work. I spent quite a while looking for the info I requested.

Getting up early, not my cup of tea (I'm not an early-bird at all but a sometimes very late-night bird). If the answer to a question can be found in the javadoc of the class itself, I get a bit suspicious of laziness (and then I have to give the poste a little encouragement ) But other questions you post show indeed that you are putting in effort and not just being lazy and abusing the forum.

Glen Iris wrote:Even by you posting "java logging turning off console" has helped me because sometimes half the battle is knowing what to google.

So true! Sometimes I spent more than half an hour just trying to find the best search query on google
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

As it turns out, I removed all logging because, as the guys say above, KISS!

I did intend to learn how to do java logging properly, alas the 6 month time constraint put paid to things that were 'nice to haves'.

Thanks for your help above guys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Logging - How many instances of File Handler?