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 ?

2 Answers 2

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 ***");
   }
}
Sign up to request clarification or add additional context in comments.

8 Comments

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.
There is no need to change anything. You can start using SLF4J with Log4J and simply bridge JUL to SLF4J. More Information: slf4j.org/legacy.html#jul-to-slf4j
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?
Yes, If you add multiple FileAppenders to a single Logger then your message will go to all the files.
My intent is to write to multiple files.
|
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.

2 Comments

Create a map of the file handlers? Should this be static and passed into my threads somehow?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.