2

I have an application which keeps track of multiple students. When processing infromation on that student, I want their log messages to go into that log file.

logs/system.log
logs/abby.log
logs/brett.log
logs/catherine.log

The system can add more students dynamically, so I can't specify each student in my log config file. How can I, at runtime, specify that a logger should write information to catherine.log ?

Dave
  • 2,735
  • 8
  • 40
  • 44

2 Answers2

0

Which Logging Framework are you using? Here is an example if you are using Log4j:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.FileAppender;

public class MyTestClass {
   /* Logger Instance should always be kept static final */
   private static final Logger LOGGER = Logger.getLogger(MyTestClass.class);

   public static void main(String args[]) {

      /* Create Dynamic FileAppender */
      SimpleLayout myLayout = new SimpleLayout();    
      FileAppender nwAppender = new FileAppender(myLayout,"file_nm",false);    

      LOGGER.addAppender(nwAppender);

      LOGGER.setLevel((Level) Level.INFO);

      /* Write Level : Debug */
      LOGGER.debug("*** DEBUG ***");
      /* Write Level : Info */
      LOGGER.info("*** INFO ***");
      /* Write Level : Error */
      LOGGER.info("*** ERROR ***");
   }
}
user2004685
  • 9,548
  • 5
  • 37
  • 54
  • Thanks for your response. I'm using java.util.logging. If I need to use a different framework in order to get this to work I will. Otherwise I'm reluctant to change the logging throughout my program. – Dave Jan 26 '16 at 15:04
  • There is no need to change anything. You can start using SLF4J with Log4J and simply bridge JUL to SLF4J. More Information: http://www.slf4j.org/legacy.html#jul-to-slf4j – user2004685 Jan 26 '16 at 15:08
  • In your example code, would I need to create a new logger for each file I wished to write to? If I simply added multiple FileAppenders to a single logger the log messages would go to all files, no? – Dave Jan 26 '16 at 15:10
  • Yes, If you add multiple FileAppenders to a single Logger then your message will go to all the files. – user2004685 Jan 26 '16 at 15:13
  • My intent is to write to multiple files. – Dave Jan 26 '16 at 15:15
  • Same content to multiple files or different content to different-different files? – user2004685 Jan 26 '16 at 15:17
  • My program spins up lots of threads at once. At any point, multiple threads could be working on one student. I tried this approach and two things don't work for me: 1) Messages for abby.log are simultaneously written to the system log and to abby.log. 2) If multiple threads are processing abby, it will open up abby-1, abby-2, etc. – Dave Jan 26 '16 at 15:28
  • Log4j is the probably the best practice for multi threaded applications. http://stackoverflow.com/questions/565893/best-practices-for-java-logging-from-multiple-threads – user2004685 Jan 26 '16 at 15:35
0

How can I, at runtime, specify that a logger should write information to catherine.log ?

There is no logging.properties option to enable this behavior. You have to write code to create a logger (strongly referenced) and installed a FileHandler on that logger.

Messages for abby.log are simultaneously written to the system log and to abby.log.

You should create a logger namespace such that the system file handler is installed on the root logger and each student is a child logger with setUseParentHandlers set to false.

If multiple threads are processing abby, it will open up abby-1, abby-2

That is because you are created multiple FileHandlers with the same file name. Create a Map and remember what you have opened.

jmehrens
  • 10,580
  • 1
  • 38
  • 47
  • Create a map of the file handlers? Should this be static and passed into my threads somehow? – Dave Jan 26 '16 at 15:51
  • Right. Otherwise, if each student has a unique logger then check it if it has a file handler installed on the logger by calling logger.getHandlers. – jmehrens Jan 26 '16 at 15:54