0

I am trying to have two output loggers in my gui. This code correctly outputs to each file.

public static void main(String[] args) {
    try {
        Handler handler = new FileHandler("OutFile.log");
        Logger.getLogger("myApp").addHandler(handler);
        Logger.getLogger("myApp").setUseParentHandlers(false);
        Handler handler2 = new FileHandler("User.log");
        handler2.setFormatter(new SimpleFormatter());
        Logger.getLogger("User").addHandler(handler2);
        Logger.getLogger("User").setUseParentHandlers(false);


        Logger.getLogger("myApp").severe("AppStarting");
        Logger.getLogger("User").severe("UserLogStarting");

But afterwards when I test the same two lines in the gui it does not work anymore. Is my interpretation of the logger wrong or am I doing something wrong?

Instead if I use the below line in the main (so I removed myApp from the string) then the logger works perfectly throughout the app, but everything goes to one file only.

    Logger.getLogger("").addHandler(handler);

P.S. I don't want to use a third party class if I can help it.

xchiltonx
  • 1,946
  • 3
  • 20
  • 18

1 Answers1

1

You need to assign the Logger been returned by Logger.getLogger() to a Logger object, like this:

Logger log = Logger.getLogger("myApp");

and then use log to work. Otherwise Logger.getLogger() will always return a new instance.

Typo
  • 1,875
  • 1
  • 19
  • 32
  • 1
    Thanks, I might have known it'd be something daft I was missing, for others stuck with the same, I added: `private final static Logger userLog = Logger.getLogger("User"); private final static Logger errorLog = Logger.getLogger("myApp"); ` before starting my main – xchiltonx Jan 18 '14 at 17:13