开发者

Define a logger for a specific call hierarchy

开发者 https://www.devze.com 2022-12-24 20:02 出处:网络
Currently i am working on log4j stuff for my application. I have a specific requirement of logging. I need to define a logger for a specific call hierarchy.

Currently i am working on log4j stuff for my application. I have a specific requirement of logging. I need to define a logger for a specific call hierarchy. Meaning all the log messages of a specific call hierarchy should go to a specific appeneder.

Example

AddFormAction(method1) ---|--- FormBusinessObject(method4) --|-- FormDAOObject(method5)

EditFormAction(method2) -----|---- FormBusinessObject(method4) --|-- FormDAOObject(method5)

DeleteFormAction(method3)----|--- FormBusinessObject(method4) --|-- FormDAOObject(method5)

I want to define a 开发者_开发问答logger for a specific call hierarchy 1 - 4 -5. These messages should goto addform.log.

I don't want the messages from 2-4-5 or 3-4-5 call hierarchies to go to addform.log.

Hope iam clear on my requirement. Any help is highly appreciated. Thanks all in advance for your replies.


I don´t know that much about log4j, but this sounds like a perfect case for AspectJ.


One method would be to use injection to give the dependencies their logger instance.

 public bool addFormAction( .... form data .... ) {
     Logger logger = LogManager.getLogger( "AddFormLogger" );
     FormBusinessObject form = new FormBusinessObject( logger );
     form.setProperty1( parameter1 );
     form.save();
 }

...

 public class FormBusinessObject
 {
      private Logger logger;

      public FormBusinessObject() {
          logger = LogManager.GetLogger( "DefaultLogger" );
      }

      public FormBusinessObject( Logger logger ) {
          logger = logger;
      }

      ...

      public void save() {
           logger.info( "Saving form" );
           ...
      }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消