Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Log to file problem

Thread view: 
Petterson Mikael - 21 Jun 2007 11:59 GMT
Hi,

In a class we have a constant pointing out path to store a log file.

 public static final String LOG = "<path to log dir>";

The path is valid only in the context of the target environment and when
we are testing that path is not valid. So when we run the test we get:

java.io.FileNotFoundException: <path to log file> (No such file or
directory) at java.io.FileOutputStream.open

Log dir is created in a "static initializer".

Any ideas how I can change the path depending if it is a test or class
is running in target environment.

What do you think about checking if the path exists and if not change it
 to a test directory.

All hints appreciated,

cheers,

//mikael
Roedy Green - 21 Jun 2007 12:38 GMT
On Thu, 21 Jun 2007 12:59:55 +0200, Petterson Mikael
<mikael@no.reply.to.se> wrote, quoted or indirectly quoted someone who
said :

>The path is valid only in the context of the target environment and when
>we are testing that path is not valid. So when we run the test we get:

I think what you mean is the file moves, in one place for testing and
in another for production.

I would do something like this:

public class Config

public static final PRODUCTION = true;

logURL = PRODUCTION ? "C:/productionplace/log.txt" :
"C:/temp/log.txt";

You must modify PRODUCTION before compiling for production.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Lew - 21 Jun 2007 14:18 GMT
> On Thu, 21 Jun 2007 12:59:55 +0200, Petterson Mikael
> <mikael@no.reply.to.se> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 16 lines]
>
> You must modify PRODUCTION before compiling for production.

A cleaner approach is to put the location in a resource file and read in the
Properties at run time, or to use relative paths to emplace the log at a known
and controlled location relative to the application class path.

Signature

Lew

TechBookReport - 21 Jun 2007 14:54 GMT
>> On Thu, 21 Jun 2007 12:59:55 +0200, Petterson Mikael
>> <mikael@no.reply.to.se> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 21 lines]
> the Properties at run time, or to use relative paths to emplace the log
> at a known and controlled location relative to the application class path.

Agreed - possibly with the production path set as the default value
which is used if the property isn't set explicitly.

Pan

--
TechBookReport Java - http://www.techbookreport.com/JavaIndex.html
EricF - 22 Jun 2007 04:02 GMT
>>> On Thu, 21 Jun 2007 12:59:55 +0200, Petterson Mikael
>>> <mikael@no.reply.to.se> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 26 lines]
>
>Pan

Also agreed. Just did this and used the classloader to load the properties.
Wish I knew a bit of Spring - with all the exceptions that could be thrown and
wanting to default to the production value if things went wrong, the little
bit of code I wrote was very ugly. It would be nice to inject the value,
though Spring would likely be overkill for this.

Eric
Lew - 22 Jun 2007 04:33 GMT
> Also agreed. Just did this and used the classloader to load the properties.
> Wish I knew a bit of Spring - with all the exceptions that could be thrown and
> wanting to default to the production value if things went wrong, the little
> bit of code I wrote was very ugly. It would be nice to inject the value,
> though Spring would likely be overkill for this.

If you think handling Exceptions is ugly, try not handling them.

I swear half my production code is comments, half of what remains is safety
checking for things like argument boundaries or wild calculations, half of
what's left after that is exception handling, half of what remains after that
is logging, and only the bit that remains after that actually does the
"happy-path" logic.

The "real world" is fraught with conditions for which your code must be on
guard.  Just because it's lengthy doesn't make it ugly.  Just because it's
hard work doesn't mean you should skimp on it.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.