On May 8, 2008, at 8:23 AM, Tasso Angelidis wrote:

So as an example?

class MyClass
{
        Logger eventLogger = Logger.getLogger("MyClass.EventLogger");
        Logger opsLogger = Logger.getLogger("MyClass.OpsLogger");


        void someMethod()
        {
                eventLogeger.warn("Server stoppped");
                opsLogger.warn("Server stoppped");
        }
}


Isn't that double the work now? What if I want to have the EventLogger writting to fileXYZ some "events" and then when I call OpsLogger.Warn() it sends out the e-mail and also writes the message to fileXYZ.

Is this logger nesting? I.e: my OpsLogger should be a child of EventLogger?

Thanks


Like defining a directory structure, you have to choose which organizational principle is more significant.

If you chose a pattern like "operations." + classname, you can route all operations related messages to the same place by attaching an appender to the "operations" logger. If you want all messages related to a single class going to the same place, then you'd need to attach one appender to both "operations.com.example.SomeClass" and "com.example.SomeClass".

If you chose a pattern like classname + ".operations" or ".OpsLogger", you can route all messages related to a specific class by attaching an appender to the classname. However, routing all operations related messages to the same place would require attaching an appender at multiple places in the hierarchy (com.example.SomeClass.operations, com.example.OtherClass.operations, etc).

I'm assuming that operations related messages are far less frequent that other log messages and the people consuming the diagnostic logs would not be offended to see the operations related messages. That would suggest attaching a file appender to the root logger which would receive all log messages, operations or otherwise, and an SMTP appender that would only process messages logged to an "operations" logger or child.

In your example, eventLogger and opsLogger are siblings, both share the same parent the logger named "MyClass".

There is no need to log the same message twice. You can design the names of the loggers you use so that one message is processed by two different appenders.

So I would suggest:

class MyClass {
    Logger logger = Logger.getLogger(MyClass.class);
Logger opsLogger = Logger.getLogger("operations." + MyClass.class.getName());

   void shutdown() {
        opsLogger.warn("Server stopped");
   }

   void start() {
        opsLogger.info("Server started");
   }

   void other renderPage() {
        logger.info("Rendering page");
        ...
   }
}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to