Discussions

EJB design: Using log4j for ejb logging

  1. Using log4j for ejb logging (1 messages)

    I just read the EJB cook book review chapter on EJB logging (posted on theserverside.com.

    I have a question regarding use of log4j for EJB logging.
    log4j logs messages asynchronously i.e. it must be using java threads to log the messages.

    Won't it interfere with the container's ejb life cycle management? Won't it violate the EJB specifications.
    Regards
    Anil
  2. Using log4j for ejb logging[ Go to top ]

    If the parts of log4j you are using use threads or any other restricted practice then yes they'll be breaking the J2EE spcifications.

    Don't let this put you off completely though.

    It is *generally* accepted to do this when it is done from proven software, so it would be okayish from log4j, but not from your own code. This is simply because it is thought that log4j is more reliable and probably proven through use in many other projects.

    However, parts of log4j flout J2EE restrictions completely, but only because it wasn't designed for the J2EE container, or more specially the EJB container.

    You should thread, access files or use sockets from the EJB container yet among others, SocketAppender, FileAppender and AsyncAppender do this.

    A design I've used before is to have only JMSAppender ever used from within the EJB container. You then write a simple listener that processes your log messages and sens them on to any other appenders. Don't forget that this JMS listener will run outside of the container and so is free to do anything.

    Log4j provides sample code for this.

    Back in the pragmatic world, most people use FileAppender directly from the container and get away with it. But your code won't necessarily be portable across other containers.

    There is a good article in WLDJ about the container restrictions and the reasons for not doing it.