3

I have 2 static initializers trying to call each other:

  • one in a Configuration class, that reads configuration parameters from a properties file;
  • one in a LoggerFactory class, which sets up the default logging parameters.

The problem is they reference each other:

  • the Configuration class needs the logger to log potential I/O issues when reading the properties file.
  • the LoggerFactory needs the Configuration file to setup the log file name, log patterns and default log level.

This is obviously not right and I get some NPE here and there.

Is there a standard way of dealing with that kind of situation?

assylias
  • 321,522
  • 82
  • 660
  • 783

3 Answers3

3

Use a default logger that requires no configuration and is replaced when the configurable logger is loaded. That way you can resolve the circular dependency with ease in a simple manner.

obataku
  • 29,212
  • 3
  • 44
  • 57
1

You can separate the configuration for the logger and have it initialize before the configuration class. Even though this is not ideal, I've seen this done on several projects.

Alex Ciminian
  • 11,398
  • 15
  • 60
  • 94
1

You should create a logger that loads when the program starts but then have it later overridden when the logger than can be configured is created.

Dylan
  • 189
  • 12