Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Springboot - not replace spring propreties in log4j.xml #1267

Closed
schotten opened this issue Feb 6, 2023 · 3 comments
Closed

Springboot - not replace spring propreties in log4j.xml #1267

schotten opened this issue Feb 6, 2023 · 3 comments

Comments

@schotten
Copy link

schotten commented Feb 6, 2023

Description

I use the log4j in the springBoot and i add the artifactId "log4j-spring-boot" for enable the use the springboot properties in the log4j.xml.
When i use inside the File, it's work very well, but when i try use in the File header, the log4j send the error.

In summary, below i pasted the code, but when i try use the ${spring:log4j.solver.service.path}, the system throw the error (log)

Configuration

Version: 2.19.0

Operating system: Windows 10

JDK: Java 17 (Adoptium)

application.properties file:

log4j.solver.service.name=${LOG4J_SOLVER_SERVICE_NAME:solver-service}
log4j.solver.service.version=${LOG4J_SOLVER_SERVICE_VERSION:1}
log4j.solver.service.enviroment=${LOG4J_SOLVER_SERVICE_ENVIROMENT:dev}
log4j.solver.service.path=${LOG4J_SOLVER_SERVICE_PATH:solver-service.log}`

LOG4J.XML file

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <File name="solver-service" fileName="${spring:log4j.solver.service.path}" append="false">
            <EcsLayout serviceName="${spring:log4j.solver.service.name}" serviceVersion="${spring:log4j.solver.service.version}"  serviceEnvironment="${spring:log4j.solver.service.enviroment}"/>
        </File>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%level - %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="propagate-logger" level="INFO" additivity="false">
            <AppenderRef ref="solver-service"/>
        </Logger>
        <Root level="INFO">
            <AppenderRef ref="STDOUT"/>
        </Root>
    </Loggers>
</Configuration>

Logs

Connected to the target VM, address: '127.0.0.1:52527', transport: 'socket'
2023-02-06 09:38:15,994 main ERROR FileManager (${spring:log4j.solver.service.path}) java.io.IOException: A sintaxe do nome do arquivo, do nome do diretório ou do rótulo do volume está incorreta java.io.IOException: A sintaxe do nome do arquivo, do nome do diretório ou do rótulo do volume está incorreta
	at java.base/java.io.WinNTFileSystem.canonicalize0(Native Method)
	at java.base/java.io.WinNTFileSystem.canonicalize(WinNTFileSystem.java:462)
	at java.base/java.io.File.getCanonicalPath(File.java:626)
	at java.base/java.io.File.getCanonicalFile(File.java:651)
	at org.apache.logging.log4j.core.util.FileUtils.makeParentDirs(FileUtils.java:139)
	at org.apache.logging.log4j.core.appender.FileManager$FileManagerFactory.createManager(FileManager.java:436)
	at org.apache.logging.log4j.core.appender.FileManager$FileManagerFactory.createManager(FileManager.java:423)
	at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:144)
	at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:100)
	at org.apache.logging.log4j.core.appender.FileManager.getFileManager(FileManager.java:183)
[application.properties.txt](https://github.com/apache/logging-log4j2/files/10663203/application.properties.txt)

	at org.apache.logging.log4j.core.appender.FileAppender$Builder.build(FileAppender.java:99)
	at org.apache.logging.log4j.core.appender.FileAppender$Builder.build(FileAppender.java:52)
	at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:124)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1138)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1063)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1055)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:664)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:258)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:304)
	at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:621)
	at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:694)
	at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:711)
	at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:253)
	at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:155)
	at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)
	at org.apache.logging.log4j.LogManager.getContext(LogManager.java:196)
	at org.apache.commons.logging.LogAdapter$Log4jLog.<clinit>(LogAdapter.java:155)
	at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:122)
	at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:89)
	at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
	at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59)
	at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:179)
	at net.weg.solverservice.SolverServiceApplication.main(SolverServiceApplication.java:18)

2023-02-06 09:38:15,997 main ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.FileAppender for element File: java.lang.IllegalStateException: ManagerFactory [org.apache.logging.log4j.core.appender.FileManager$FileManagerFactory@2b95e48b] unable to create manager for [${spring:log4j.solver.service.path}] with data [org.apache.logging.log4j.core.appender.FileManager$FactoryData@4a3329b9] java.lang.IllegalStateException: ManagerFactory [org.apache.logging.log4j.core.appender.FileManager$FileManagerFactory@2b95e48b] unable to create manager for [${spring:log4j.solver.service.path}] with data [org.apache.logging.log4j.core.appender.FileManager$FactoryData@4a3329b9]
	at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:146)
	at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:100)
	at org.apache.logging.log4j.core.appender.FileManager.getFileManager(FileManager.java:183)
	at org.apache.logging.log4j.core.appender.FileAppender$Builder.build(FileAppender.java:99)
	at org.apache.logging.log4j.core.appender.FileAppender$Builder.build(FileAppender.java:52)
	at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:124)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1138)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1063)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1055)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:664)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:258)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:304)
	at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:621)
	at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:694)
	at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:711)
	at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:253)
	at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:155)
	at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)
	at org.apache.logging.log4j.LogManager.getContext(LogManager.java:196)
	at org.apache.commons.logging.LogAdapter$Log4jLog.<clinit>(LogAdapter.java:155)
	at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:122)
	at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:89)
	at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
	at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59)
	at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:179)
	at net.weg.solverservice.SolverServiceApplication.main(SolverServiceApplication.java:18)

2023-02-06 09:38:15,998 main ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.FileAppender for element File: java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.FileAppender java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.FileAppender
	at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.findFactoryMethod(PluginBuilder.java:260)
	at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:136)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1138)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1063)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1055)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:664)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:258)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:304)
	at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:621)
	at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:694)
	at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:711)
	at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:253)
	at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:155)
	at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)
	at org.apache.logging.log4j.LogManager.getContext(LogManager.java:196)
	at org.apache.commons.logging.LogAdapter$Log4jLog.<clinit>(LogAdapter.java:155)
	at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:122)
	at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:89)
	at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
	at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59)
	at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:179)
	at net.weg.solverservice.SolverServiceApplication.main(SolverServiceApplication.java:18)

2023-02-06 09:38:16,003 main ERROR Null object returned for File in Appenders.
2023-02-06 09:38:16,008 main ERROR Unable to locate appender "solver-service" for logger config "propagate-logger"

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.3)

INFO - HV000001: Hibernate Validator 6.2.4.Final
INFO - Starting SolverServiceApplication using Java 17.0.4.1 on BRJGSD357212 with PID 73264 (C:\Projetos\solver-service\target\classes started by schotten in C:\Projetos\solver-service)
INFO - The following 1 profile is active: "dev"
INFO - Multiple Spring Data modules found, entering strict repository configuration mode
INFO - Bootstrapping Spring Data JPA repositories in DEFAULT mode.
INFO - Finished Spring Data repository scanning in 16 ms. Found 0 JPA repository interfaces.
INFO - Tomcat initialized with port(s): 8010 (http)
INFO - Initializing ProtocolHandler ["http-nio-8010"]

Reproduction

Configure the springboot app and use the Log4j.xml and applicatio.proprerties informed in this message and reproduce the error.

@ppkarwasz
Copy link
Contributor

@schotten,

Logging in a Spring Boot application is configured at least twice:

  • when the SpringApplication class is loaded (that is what you see in your stacktrace). This process is described in automatic configuration and does not have access to Spring's environment (that didn't start yet),
  • after the ApplicationEnvironmentPreparedEvent is fired (cf. lifecycle of a Spring Boot application). This process is specific to Spring Boot, has access to Spring's environment and uses the log4j2-spring.xml file (falling back on log4j2.xml if such a file does not exist).

If you want to use Spring's enviroment to configure your logging system you have to options:

  1. Create two files: log4j2.xml and log4j2-spring.xml. The first one will be used during the bootstrap phase of your application and can not use Spring's environment. The second one will be used, when your application is up and running.
  2. Stick to a single configuration file and use arbiters to provide multiple versions of your "solver-service" appender:
<Select>
    <SpringProfile name="dev">
        <File name="solver-service" fileName="${spring:log4j.solver.service.path}" append="false">
            <EcsLayout serviceName="${spring:log4j.solver.service.name}" serviceVersion="${spring:log4j.solver.service.version}"  serviceEnvironment="${spring:log4j.solver.service.enviroment}"/>
        </File>
    </SpringProfile>
    <DefaultArbiter>
        <Null name="solver-service"/>
    </DefaultArbiter>
</Select>

@SebHeuze
Copy link

SebHeuze commented Aug 1, 2023

@ppkarwasz I have the same problem since I upgraded from 2.17.2 to 2.19 (Spring boot 2.3.9)
I don't need two log4j files or arbiters to make it work in 2.17.2

But since 2.18 it stopped working because of the new way to load LoggingSystemFactory (spring.factories)
I still don't understand why spring.factories are not loaded

EDIT : It don't work because LoggingSystemFactory does not exist before Spring 2.4, the documentation mention that the lib require Spring boot 2.0.3 or 2.1.1, which is outdated since Log4J 2.18

@ppkarwasz
Copy link
Contributor

I am closing this for lack of activity. I think that:

Fill free to reopen the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants