Question: Within a java application, I have several Classes and Threads here and there. I would like them all to write their logs to one single file. How can I achieve that?

Also here's some more questions:

1. Do I receive the same instance of a logger if I define the same name for them even in different classes. To be clear, are the following two loggers the same:

```
class MyClass1 {
    Logger logger1 = Logger.getLogger("theSameName");
}
class MyClass2{
    Logger logger2 = Logger.getLogger("theSameName");
}

```
2. What happens if I define two of the `FileHandler` in two separate Threads that have the same file name?
Do both of them write to the same file?
If yes, what happens if the file is already opened by the first one, when the second one tries to open and write to it?
3. What happens if I try to add the same FielHandler to a logger more than once as in:

```
logger.addHandler(myFileHandler); // in Thread one
...
logger.addHandler(myFileHandler); // in Thread two

```

Will I receive a `RuntimeException` because of the two `FileHandler` may try to open the same file simultaneously?

I'm using `java-util-logging` not `log4j`.

Answer: 
> 
> Do I receive the same instance of a logger if I define the same name for them even in different classes.
> 
> 
> 

I don't believe it is defined anywhere, but most like it is the same object and it shouldn't matter. The libraries don't determine which file to write to based on Logger instances.

> 
> What happens if I define two FileHandlers in two separate Threads that have the same file name? 
> 
> 
> 

Most like you will get two logging handlers. This means everything will be logged twice unless you attempt to write to the same file in which case it is likely to get corrupted.

There is no good reason to do this. Just define your configuration on start up, once.

> 
> What happens if I try to add the same FielHandler to a logger more than once as in:
> 
> 
> 

It is unlikely the underlying library will notice and it will corrupt the file. If you really need to know, I suggest you try it but don't do this in production.

> 
> Will I receive a RuntimeException or something because two FileHandler may try to open the same file simultaneously?
> 
> 
> 

This is not what FileOutputStream does or the OS. You just get a corrupted file.
```
package javaloggers;

import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
//singleton class logger wrapper
public class LogWrap {
    public static final Logger aLogger = Logger.getLogger("myLogger");
    private static LogWrap instance = null;
    public static LogWrap getInstance(){
        if(instance==null){
            getLoggerReady();
            instance = new LogWrap();
        }
        return instance;
    }
    private static void getLoggerReady(){
        try{
            FileHandler fh = new FileHandler("test-log.txt");
            fh.setFormatter(new SimpleFormatter());
            aLogger.addHandler(fh);
            aLogger.setUseParentHandlers(false);
            aLogger.setLevel(Level.ALL);
        } catch(Exception e){
            System.out.print("Error: Logger creation issue: "+e);
        }           
    }
}
class test {
    public static void main(String args[]){
        //usage
        LogWrap logWrap = LogWrap.getInstance();
        logWrap.aLogger.severe( "Hello world!" );
    }
}

```