-
Need "global" instance. Is this possible?
Hello -
I am working on some classes that provide a logging/tracing infrastructure.
Since every method of every class potentially will call them, they need
to be globally visible. I don't want, for example, to pass in a logging
object to each called method.
one approach would be to use static methods so that calls to the class
can be made, rather than instances. The problem is that the application
will be multi-threaded, and separate logging objects are needed for each
thread...
I could make a layer of logging objects that all the other custom objects
inherit from...but then EVERY instance would have their own logging IV's,
and I don't want that, either--just one per thread.
What I need is to be able to instantiate Logger objects, and then load
them into a static class Map object, with static accessor methods. I suppose
the thread ID would be the key. Then, when a thread logs a msg, it looks
up the Logger object associated with the current thread ID and passes the
msg on to it.
I guess that approach would work, but I'm worried about the performance
hits associated with the complexity. Any better/simpler/faster ways to do
this?
Rich
-
Re: Need "global" instance. Is this possible?
"separate logging objects are needed for each thread..."
If this is so, then you need an object (let's call it LoggedThread) that
extends Thread and contains a logging object. At least, that would be the
straightforward design. But possibly you are writing a support package and
can't tell the users of the package that they need to create LoggedThreads
instead of Threads. In that case you would have to have a map that
associated an instance of the logging object for each Thread, as you
suggest. But you wouldn't use the thread ID as the key, you should use the
thread itself. Remember that the key can be any kind of object.
PC2
"Richard Warner" <richard_warner@nonhp-am.exch.hp.com> wrote in message
news:3aeeddd9$1@news.devx.com...
>
> Hello -
>
> I am working on some classes that provide a logging/tracing
infrastructure.
> Since every method of every class potentially will call them, they need
> to be globally visible. I don't want, for example, to pass in a logging
> object to each called method.
> one approach would be to use static methods so that calls to the class
> can be made, rather than instances. The problem is that the application
> will be multi-threaded, and separate logging objects are needed for each
> thread...
> I could make a layer of logging objects that all the other custom
objects
> inherit from...but then EVERY instance would have their own logging IV's,
> and I don't want that, either--just one per thread.
> What I need is to be able to instantiate Logger objects, and then load
> them into a static class Map object, with static accessor methods. I
suppose
> the thread ID would be the key. Then, when a thread logs a msg, it looks
> up the Logger object associated with the current thread ID and passes the
> msg on to it.
> I guess that approach would work, but I'm worried about the performance
> hits associated with the complexity. Any better/simpler/faster ways to do
>
> this?
>
> Rich
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks