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 / January 2006

Tip: Looking for answers? Try searching our database.

logging from multiple points

Thread view: 
Michael Powe - 02 Jan 2006 22:06 GMT
Hello,

I have implemented simple logging using the Logger API.  This logs
just from one class in the package.  I would like to log to the same
file from more than one class in the package, but I am unsure how to
set this up.  

The way I am now doing it is to create a logger with an instance
method.  The resulting logger is referenced in the appropriate methods
of the class.

I've looked at some web resources about logging, but I'm not sure how
to create a package-level logger that could be reached by multiple
classes.  One of my concerns would be that one logger call not step on
another.

Thanks.

mp

Signature

'cat' is not recognized as an internal or external command,
operable program or batch file.

Aray - 03 Jan 2006 08:46 GMT
All you need is a SINGLETON logger. You may google "singleton java" for
details.

The Logger class my like this

/**
* <p>Title: TODO Untitled Document </p>
* <p>Description: TODO No Description Specified </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: ±±¾©¶«·½¹úÐŵç×ÓÓÐÏÞ¹«Ë¾</p>
* @author ÖܺìÑô
* @version 1.0
*/
public class Logger
{
   private static Logger self;

   //key word PRIVATE prevent to invoke this constructor
   private Logger()
   {
   }

   //use this static method to get a instance of  Logger
   public static Logger getInstance()
   {
       if (self == null)
           self = new Logger();
       return self;
   }

   //keyword "synchronized" make this method thread safe, only one thread
can invoke this method at one time
   public synchronized void debug(String msg)
   {
   }

   public synchronized void info(String msg)
   {
   }

   public synchronized void fatal(String msg)
   {
   }

}

--------------------------
an example for  useing the Logger

/**
* <p>Title: TODO Untitled Document </p>
* <p>Description: TODO No Description Specified </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: ±±¾©¶«·½¹úÐŵç×ÓÓÐÏÞ¹«Ë¾</p>
* @author ÖܺìÑô
* @version 1.0
*/
public class LoggerTest
{

   /**
    * @param args
    */
   public static void main(String[] args)
   {
       Logger logger = Logger.getInstance();
       logger.info("info test");
       logger.debug("debug test");
   }

}

"Michael Powe" <michael+gnus@trollope.org> дÈëÏûÏ¢ÐÂÎÅ:usls6i8sd.fsf@trollope.org...
> Hello,
>
[quoted text clipped - 15 lines]
>
> mp
Luke Meyers - 03 Jan 2006 09:01 GMT
If multiple threads are going to access the logger, you'll need
appropriate synchronization constructs to ensure that the singleton
can't get instantiated more than once.  This is non-trivial, but a
solution is available.  Google for it, but beware that there is a
decent amount of inaccurate advice out there on this topic.  In
particular, the double-checked locking pattern is flawed and should not
be used.

I ran into this issue last year and used it as a jumping-off point to
learn a lot about Java synchronization.  I heartily endorse Doug Lea's
book on the topic.  It was one of the best technical books I've read in
years.  It will make you feel smart, so read it.

Luke
Thomas Hawtin - 03 Jan 2006 12:05 GMT
> If multiple threads are going to access the logger, you'll need
> appropriate synchronization constructs to ensure that the singleton
> can't get instantiated more than once.  This is non-trivial, but a
> solution is available.  Google for it, but beware that there is a

The solution is quite trivial - assign the logger during class
initialisation.

    private static final Logger self = new Logger();

> decent amount of inaccurate advice out there on this topic.  In
> particular, the double-checked locking pattern is flawed and should not
> be used.

You can use it in 1.5. Almost certainly it will be a pointless waster of
time and confuse people. Just need to make the reference volatile. In
practice this should also work in 1.4.

    private static volatile Logger self;
    public static Logger getInstance() {
        if (self == null) {
            // WORKS FROM 1.5, BUT NOT RECOMMENDED.
            synchronized (Logger.class) {
                if (self == null) {
                    self = new Logger();
                }
            }
        }
        return self;
    }

> I ran into this issue last year and used it as a jumping-off point to
> learn a lot about Java synchronization.  I heartily endorse Doug Lea's
> book on the topic.  It was one of the best technical books I've read in
> years.  It will make you feel smart, so read it.

Yup.

Concurrent Programming in Java: Design Principles and Patterns, Doug Lea

http://www.amazon.co.uk/exec/obidos/ASIN/0201310090/

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Aray - 03 Jan 2006 15:54 GMT
Thanks Luke Meyers and Thomas Hawtin indicating my bugs

"Thomas Hawtin" <usenet@tackline.plus.com>
??????:43ba667a$0$1468$ed2619ec@ptn-nntp-reader01.plus.net...


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



©2008 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.