3 Replies Latest reply on Sep 1, 2011 5:51 PM by peterj

    Log file using class name

    iapazmino

      Hello,

       

      I want the log file not to be one, but one for each class which invokes the logger so at the end I have a few files named like this com.example.ClassName.log

       

      How should I setup my log4j.xml?

        • 1. Re: Log file using class name
          peterj

          Create an <appender> for each category, and within each <category> reference the corresponding appender using <appender-ref>. The log4j documentation should show you how to set this up.

           

          This will give you a log file per category.  You can always add additional catagories and appenders to split things out even more.

           

          Personally, this seems like a whole lot of work for no good purpose. I could see that you might want all of the log entries for your code, which is probably using the same package prefix such as com.foo, to go into a separate log file. But I don't see any point to placing each class' log entries into a new log file. You will end up with hundreds of log files.

          1 of 1 people found this helpful
          • 2. Re: Log file using class name
            iapazmino

            Thank you for your reply.

            I've been looking for something easier, like setting a flag to tell the logger to create a new file per class, because as you say this approach of manually adding appenders for categories is over complicated.

             

            The point of this is to have as many log-files as test classes so you get a test-report and a log-file per test-class. Not for production code.

            • 3. Re: Log file using class name
              peterj

              Sorry, but there is no such flag in log4j. Though you might be able to extend one of the file appenders to provide this kind of functionality.