logging from multiple points

M

Michael Powe

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
 
A

Aray

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");
}

}
 
L

Luke Meyers

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
 
T

Thomas Hawtin

Luke said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top