SnobbyVirus1973 commented on issue #2766:
URL: 
https://github.com/apache/logging-log4j2/issues/2766#issuecomment-2272703864

   The reason I code like `MongoDb4Provider.newBuilder()` is I found something 
like on [Log4j2 
Manual](https://logging.apache.org/log4j/2.x/manual/customconfig.html#initialize-log4j-by-combining-configuration-file-with-programmat).
   
   In my program, I want to implement the following functions:
   1. My program is based on SpringBoot.
   2. I wrote a core project, and multiple projects in our company will 
reference my program.
   3. Each referenced project can have a `log4j2.xml` configuration file in the 
resource directory, or it can have none. If there is no configuration file, I 
will generate a default log configuration. If a configuration file is written, 
I will add the configuration in the configuration file to the default 
configuration.
   4. The above default configuration will output logs to the console and disk 
files. There is another jar package that can output logs to MongoDB. If the 
project reference this jar package, the default configuration will be modified 
and MongoDB's Appender will be added.
   
   My current implementation is:
   
   1. In the core project, a `Plugin` is written`@Plugin(name = 
"ConfigurationFactory", category = ConfigurationFactory.CATEGORY)`
   2. The `getSupportedTypes` method of this `Plugin` returns `[".xml", "*"]`
   3. The `getConfiguration` method of this `Plugin` will create an inner class 
that inherits `XmlConfiguration`
   4. The inner class overrides the doConfigure method, and the code inside is 
similar to this [reference 
code](https://logging.apache.org/log4j/2.x/manual/customconfig.html#initialize-log4j-by-combining-configuration-file-with-programmat).
 The difference is that the location of the configuration file is determined in 
the `doConfigure` method. If it is `log4j2.xml` in the resource directory of 
the project, `super.doConfigure()` will be called, otherwise it will not be 
called.
   5. In another jar package, I wrote a class and handed it to Spring for 
initialization. During the initialization process, the following code is 
executed
   ```
   LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
   Configuration config = ctx.getConfiguration();
   // Append MongoDB's Appender
   ctx.updateLoggers();
   ```
   I handed it to Spring for initialization because I wanted to configure the 
connection address of MongoDB in the `application.yml` file.
   
   Is there a better way to implement it? Please tell me☺️
   
   Forgive my poor English, English is not my native language.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to