SpringEnvironmentHolder never gets its Environment initiated

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

SpringEnvironmentHolder never gets its Environment initiated

Buks van der Lingen
Hi,

 

I've been trying to get the Spring environment available in log4j2 for a
couple of weeks now.

Mostly using custom lookups and using the reload mechanism. I haven't fully
got it to work when log4j2 ver 2.13.0 got released so I switched over to try
make that work for me.

What happens is that the SpringEnvironmentHolder never gets initiated with
the Spring Environment.

The condition: [

if (environment == null && LogManager.getFactory() != null &&
LogManager.getFactory().hasContext(SpringEnvironmentHolder.class.getName(),
null, false)) {

]

Is never true, even though the Spring property sources are mostly available:
[

Object obj =
LogManager.getContext(false).getObject(Log4j2CloudConfigLoggingSystem.ENVIRO
NMENT_KEY);

]

 

Any ideas what I am doing wrong here?

My log4j2.properties file looks like this:

status=warn
name=Log4j2PropertiesConfig
packages=za.co.spsi.ice.config

property.log_dateformat_pattern=-yyyy-MM-dd HH:mm:ss.SSS
property.log_level_pattern=-%5p
property.pattern=%d{${log_dateformat_pattern:-yyyy-MM-dd HH:mm:ss.SSS}}
%highlight{${log_level_pattern:-%5p}}{FATAL=red blink, ERROR=red,
WARN=yellow bold, INFO=green, DEBUG=green bold, TRACE=blue}
%style{${sys:PID}}{magenta} [%15.15t] %style{%-40.40C{1.}}{cyan} :
%m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}
property.filename=./poe-buks.log

appender.null.type=Null
appender.null.name=LogNull

appender.console.type=Console
appender.console.name=LogToConsole
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=${pattern}

appender.kafka.type=kafka
appender.kafka.name=LogToKafka
appender.kafka.topic=${spring:kafka.log.topic}
appender.kafka.properties1.type=Property
appender.kafka.properties1.name=bootstrap.servers
appender.kafka.properties1.value=${spring:spring.kafka.bootstrap-servers}
appender.kafka.syncSend=${spring:kafka.log.async}
appender.kafka.layout.type=PatternLayout
appender.kafka.layout.pattern=%d{${log_dateformat_pattern:-yyyy-MM-dd
HH:mm:ss.SSS}} %highlight{${log_level_pattern:-%5p}}{FATAL=red blink,
ERROR=red, WARN=yellow bold, INFO=green, DEBUG=green bold, TRACE=blue}
%style{${sys:PID}}{magenta} [%15.15t] %style{%-40.40C{1.}}{cyan} :
%m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}

# Rotate log file
appender.rolling.type=RollingFile
appender.rolling.name=LogToRollingFile
appender.rolling.fileName=${filename}
appender.rolling.filePattern=${filename}.%d{yyyy-MM-dd}.%i.gz
appender.rolling.layout.type=PatternLayout
appender.rolling.layout.pattern=${pattern}
appender.rolling.policies.type=Policies
appender.rolling.policies.time.type=TimeBasedTriggeringPolicy
appender.rolling.policies.size.type=SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type=DefaultRolloverStrategy
appender.rolling.strategy.max=10

appender.routing.type=Routing
appender.routing.name=LogToRoutingAppender
appender.routing.routes.type=Routes
appender.routing.routes.pattern=${sys:kafka.log.enabled}
appender.routing.routes.route1.type=Route
appender.routing.routes.route1.key=false
appender.routing.routes.route1.ref=LogNull
appender.routing.routes.route2.type=Route
appender.routing.routes.route2.key=true
appender.routing.routes.route2.ref=LogToKafka

logger.kafka.name=org.apache.kafka
logger.kafka.level=error
logger.kafka.additivity=false
logger.kafka.appenderRef.rolling.ref=LogToRollingFile
logger.kafka.appenderRef.routing.ref=LogToRoutingAppender

logger.spring-kafka.name=org.springframework.kafka
logger.spring-kafka.level=error
logger.spring-kafka.additivity=false
logger.spring-kafka.appenderRef.rolling.ref=LogToRollingFile
logger.spring-kafka.appenderRef.routing.ref=LogToRoutingAppender

logger.hibernate.name=org.hibernate
logger.hibernate.level=warn
logger.hibernate.additivity=false
logger.hibernate.appenderRef.rolling.ref=LogToRollingFile
logger.hibernate.appenderRef.routing.ref=LogToRoutingAppender

logger.apache.name=org.apache
logger.apache.level=warn
logger.apache.additivity=false
logger.apache.appenderRef.rolling.ref=LogToRollingFile
logger.apache.appenderRef.routing.ref=LogToRoutingAppender

#Root logger will log to
rootLogger.level=info
rootLogger.appenderRef.stdout.ref=LogToRollingFile
rootLogger.appenderRef.routing.ref=LogToRoutingAppender



 

Kind regards,

Buks

 

 

Reply | Threaded
Open this post in threaded view
|

Re: SpringEnvironmentHolder never gets its Environment initiated

Ralph Goers
I assume this is a Spring Boot app?   The spring-cloud-config-client includes a file named log4j2.system.properties. The values in that file are set as system properties by Log4j 2, so Log4j2CloudConfigLoggingSystem will be used as the LoggingSystem by Spring. Its initialize method sets the Spring Environment into the context, so if that isn’t happening then one of those things isn’t happening properly.


Ralph

> On Dec 31, 2019, at 12:39 AM, [hidden email] wrote:
>
> Hi,
>
>
>
> I've been trying to get the Spring environment available in log4j2 for a
> couple of weeks now.
>
> Mostly using custom lookups and using the reload mechanism. I haven't fully
> got it to work when log4j2 ver 2.13.0 got released so I switched over to try
> make that work for me.
>
> What happens is that the SpringEnvironmentHolder never gets initiated with
> the Spring Environment.
>
> The condition: [
>
> if (environment == null && LogManager.getFactory() != null &&
> LogManager.getFactory().hasContext(SpringEnvironmentHolder.class.getName(),
> null, false)) {
>
> ]
>
> Is never true, even though the Spring property sources are mostly available:
> [
>
> Object obj =
> LogManager.getContext(false).getObject(Log4j2CloudConfigLoggingSystem.ENVIRO
> NMENT_KEY);
>
> ]
>
>
>
> Any ideas what I am doing wrong here?
>
> My log4j2.properties file looks like this:
>
> status=warn
> name=Log4j2PropertiesConfig
> packages=za.co.spsi.ice.config
>
> property.log_dateformat_pattern=-yyyy-MM-dd HH:mm:ss.SSS
> property.log_level_pattern=-%5p
> property.pattern=%d{${log_dateformat_pattern:-yyyy-MM-dd HH:mm:ss.SSS}}
> %highlight{${log_level_pattern:-%5p}}{FATAL=red blink, ERROR=red,
> WARN=yellow bold, INFO=green, DEBUG=green bold, TRACE=blue}
> %style{${sys:PID}}{magenta} [%15.15t] %style{%-40.40C{1.}}{cyan} :
> %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}
> property.filename=./poe-buks.log
>
> appender.null.type=Null
> appender.null.name=LogNull
>
> appender.console.type=Console
> appender.console.name=LogToConsole
> appender.console.layout.type=PatternLayout
> appender.console.layout.pattern=${pattern}
>
> appender.kafka.type=kafka
> appender.kafka.name=LogToKafka
> appender.kafka.topic=${spring:kafka.log.topic}
> appender.kafka.properties1.type=Property
> appender.kafka.properties1.name=bootstrap.servers
> appender.kafka.properties1.value=${spring:spring.kafka.bootstrap-servers}
> appender.kafka.syncSend=${spring:kafka.log.async}
> appender.kafka.layout.type=PatternLayout
> appender.kafka.layout.pattern=%d{${log_dateformat_pattern:-yyyy-MM-dd
> HH:mm:ss.SSS}} %highlight{${log_level_pattern:-%5p}}{FATAL=red blink,
> ERROR=red, WARN=yellow bold, INFO=green, DEBUG=green bold, TRACE=blue}
> %style{${sys:PID}}{magenta} [%15.15t] %style{%-40.40C{1.}}{cyan} :
> %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}
>
> # Rotate log file
> appender.rolling.type=RollingFile
> appender.rolling.name=LogToRollingFile
> appender.rolling.fileName=${filename}
> appender.rolling.filePattern=${filename}.%d{yyyy-MM-dd}.%i.gz
> appender.rolling.layout.type=PatternLayout
> appender.rolling.layout.pattern=${pattern}
> appender.rolling.policies.type=Policies
> appender.rolling.policies.time.type=TimeBasedTriggeringPolicy
> appender.rolling.policies.size.type=SizeBasedTriggeringPolicy
> appender.rolling.policies.size.size=10MB
> appender.rolling.strategy.type=DefaultRolloverStrategy
> appender.rolling.strategy.max=10
>
> appender.routing.type=Routing
> appender.routing.name=LogToRoutingAppender
> appender.routing.routes.type=Routes
> appender.routing.routes.pattern=${sys:kafka.log.enabled}
> appender.routing.routes.route1.type=Route
> appender.routing.routes.route1.key=false
> appender.routing.routes.route1.ref=LogNull
> appender.routing.routes.route2.type=Route
> appender.routing.routes.route2.key=true
> appender.routing.routes.route2.ref=LogToKafka
>
> logger.kafka.name=org.apache.kafka
> logger.kafka.level=error
> logger.kafka.additivity=false
> logger.kafka.appenderRef.rolling.ref=LogToRollingFile
> logger.kafka.appenderRef.routing.ref=LogToRoutingAppender
>
> logger.spring-kafka.name=org.springframework.kafka
> logger.spring-kafka.level=error
> logger.spring-kafka.additivity=false
> logger.spring-kafka.appenderRef.rolling.ref=LogToRollingFile
> logger.spring-kafka.appenderRef.routing.ref=LogToRoutingAppender
>
> logger.hibernate.name=org.hibernate
> logger.hibernate.level=warn
> logger.hibernate.additivity=false
> logger.hibernate.appenderRef.rolling.ref=LogToRollingFile
> logger.hibernate.appenderRef.routing.ref=LogToRoutingAppender
>
> logger.apache.name=org.apache
> logger.apache.level=warn
> logger.apache.additivity=false
> logger.apache.appenderRef.rolling.ref=LogToRollingFile
> logger.apache.appenderRef.routing.ref=LogToRoutingAppender
>
> #Root logger will log to
> rootLogger.level=info
> rootLogger.appenderRef.stdout.ref=LogToRollingFile
> rootLogger.appenderRef.routing.ref=LogToRoutingAppender
>
>
>
>
>
> Kind regards,
>
> Buks
>
>
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: SpringEnvironmentHolder never gets its Environment initiated

Ralph Goers
I should also reiterate that in a Spring Boot app logging is initialized at least 3 times. The first and possibly the second do not have a Spring Environment.

Ralph

> On Dec 31, 2019, at 8:39 AM, Ralph Goers <[hidden email]> wrote:
>
> I assume this is a Spring Boot app?   The spring-cloud-config-client includes a file named log4j2.system.properties. The values in that file are set as system properties by Log4j 2, so Log4j2CloudConfigLoggingSystem will be used as the LoggingSystem by Spring. Its initialize method sets the Spring Environment into the context, so if that isn’t happening then one of those things isn’t happening properly.
>
>
> Ralph
>
>> On Dec 31, 2019, at 12:39 AM, [hidden email] wrote:
>>
>> Hi,
>>
>>
>>
>> I've been trying to get the Spring environment available in log4j2 for a
>> couple of weeks now.
>>
>> Mostly using custom lookups and using the reload mechanism. I haven't fully
>> got it to work when log4j2 ver 2.13.0 got released so I switched over to try
>> make that work for me.
>>
>> What happens is that the SpringEnvironmentHolder never gets initiated with
>> the Spring Environment.
>>
>> The condition: [
>>
>> if (environment == null && LogManager.getFactory() != null &&
>> LogManager.getFactory().hasContext(SpringEnvironmentHolder.class.getName(),
>> null, false)) {
>>
>> ]
>>
>> Is never true, even though the Spring property sources are mostly available:
>> [
>>
>> Object obj =
>> LogManager.getContext(false).getObject(Log4j2CloudConfigLoggingSystem.ENVIRO
>> NMENT_KEY);
>>
>> ]
>>
>>
>>
>> Any ideas what I am doing wrong here?
>>
>> My log4j2.properties file looks like this:
>>
>> status=warn
>> name=Log4j2PropertiesConfig
>> packages=za.co.spsi.ice.config
>>
>> property.log_dateformat_pattern=-yyyy-MM-dd HH:mm:ss.SSS
>> property.log_level_pattern=-%5p
>> property.pattern=%d{${log_dateformat_pattern:-yyyy-MM-dd HH:mm:ss.SSS}}
>> %highlight{${log_level_pattern:-%5p}}{FATAL=red blink, ERROR=red,
>> WARN=yellow bold, INFO=green, DEBUG=green bold, TRACE=blue}
>> %style{${sys:PID}}{magenta} [%15.15t] %style{%-40.40C{1.}}{cyan} :
>> %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}
>> property.filename=./poe-buks.log
>>
>> appender.null.type=Null
>> appender.null.name=LogNull
>>
>> appender.console.type=Console
>> appender.console.name=LogToConsole
>> appender.console.layout.type=PatternLayout
>> appender.console.layout.pattern=${pattern}
>>
>> appender.kafka.type=kafka
>> appender.kafka.name=LogToKafka
>> appender.kafka.topic=${spring:kafka.log.topic}
>> appender.kafka.properties1.type=Property
>> appender.kafka.properties1.name=bootstrap.servers
>> appender.kafka.properties1.value=${spring:spring.kafka.bootstrap-servers}
>> appender.kafka.syncSend=${spring:kafka.log.async}
>> appender.kafka.layout.type=PatternLayout
>> appender.kafka.layout.pattern=%d{${log_dateformat_pattern:-yyyy-MM-dd
>> HH:mm:ss.SSS}} %highlight{${log_level_pattern:-%5p}}{FATAL=red blink,
>> ERROR=red, WARN=yellow bold, INFO=green, DEBUG=green bold, TRACE=blue}
>> %style{${sys:PID}}{magenta} [%15.15t] %style{%-40.40C{1.}}{cyan} :
>> %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}
>>
>> # Rotate log file
>> appender.rolling.type=RollingFile
>> appender.rolling.name=LogToRollingFile
>> appender.rolling.fileName=${filename}
>> appender.rolling.filePattern=${filename}.%d{yyyy-MM-dd}.%i.gz
>> appender.rolling.layout.type=PatternLayout
>> appender.rolling.layout.pattern=${pattern}
>> appender.rolling.policies.type=Policies
>> appender.rolling.policies.time.type=TimeBasedTriggeringPolicy
>> appender.rolling.policies.size.type=SizeBasedTriggeringPolicy
>> appender.rolling.policies.size.size=10MB
>> appender.rolling.strategy.type=DefaultRolloverStrategy
>> appender.rolling.strategy.max=10
>>
>> appender.routing.type=Routing
>> appender.routing.name=LogToRoutingAppender
>> appender.routing.routes.type=Routes
>> appender.routing.routes.pattern=${sys:kafka.log.enabled}
>> appender.routing.routes.route1.type=Route
>> appender.routing.routes.route1.key=false
>> appender.routing.routes.route1.ref=LogNull
>> appender.routing.routes.route2.type=Route
>> appender.routing.routes.route2.key=true
>> appender.routing.routes.route2.ref=LogToKafka
>>
>> logger.kafka.name=org.apache.kafka
>> logger.kafka.level=error
>> logger.kafka.additivity=false
>> logger.kafka.appenderRef.rolling.ref=LogToRollingFile
>> logger.kafka.appenderRef.routing.ref=LogToRoutingAppender
>>
>> logger.spring-kafka.name=org.springframework.kafka
>> logger.spring-kafka.level=error
>> logger.spring-kafka.additivity=false
>> logger.spring-kafka.appenderRef.rolling.ref=LogToRollingFile
>> logger.spring-kafka.appenderRef.routing.ref=LogToRoutingAppender
>>
>> logger.hibernate.name=org.hibernate
>> logger.hibernate.level=warn
>> logger.hibernate.additivity=false
>> logger.hibernate.appenderRef.rolling.ref=LogToRollingFile
>> logger.hibernate.appenderRef.routing.ref=LogToRoutingAppender
>>
>> logger.apache.name=org.apache
>> logger.apache.level=warn
>> logger.apache.additivity=false
>> logger.apache.appenderRef.rolling.ref=LogToRollingFile
>> logger.apache.appenderRef.routing.ref=LogToRoutingAppender
>>
>> #Root logger will log to
>> rootLogger.level=info
>> rootLogger.appenderRef.stdout.ref=LogToRollingFile
>> rootLogger.appenderRef.routing.ref=LogToRoutingAppender
>>
>>
>>
>>
>>
>> Kind regards,
>>
>> Buks
>>
>>
>>
>>
>>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: SpringEnvironmentHolder never gets its Environment initiated

Ralph Goers
Buks,

Where you able to find the problem?

Ralph

> On Dec 31, 2019, at 9:11 AM, Ralph Goers <[hidden email]> wrote:
>
> I should also reiterate that in a Spring Boot app logging is initialized at least 3 times. The first and possibly the second do not have a Spring Environment.
>
> Ralph
>
>> On Dec 31, 2019, at 8:39 AM, Ralph Goers <[hidden email]> wrote:
>>
>> I assume this is a Spring Boot app?   The spring-cloud-config-client includes a file named log4j2.system.properties. The values in that file are set as system properties by Log4j 2, so Log4j2CloudConfigLoggingSystem will be used as the LoggingSystem by Spring. Its initialize method sets the Spring Environment into the context, so if that isn’t happening then one of those things isn’t happening properly.
>>
>>
>> Ralph
>>
>>> On Dec 31, 2019, at 12:39 AM, [hidden email] wrote:
>>>
>>> Hi,
>>>
>>>
>>>
>>> I've been trying to get the Spring environment available in log4j2 for a
>>> couple of weeks now.
>>>
>>> Mostly using custom lookups and using the reload mechanism. I haven't fully
>>> got it to work when log4j2 ver 2.13.0 got released so I switched over to try
>>> make that work for me.
>>>
>>> What happens is that the SpringEnvironmentHolder never gets initiated with
>>> the Spring Environment.
>>>
>>> The condition: [
>>>
>>> if (environment == null && LogManager.getFactory() != null &&
>>> LogManager.getFactory().hasContext(SpringEnvironmentHolder.class.getName(),
>>> null, false)) {
>>>
>>> ]
>>>
>>> Is never true, even though the Spring property sources are mostly available:
>>> [
>>>
>>> Object obj =
>>> LogManager.getContext(false).getObject(Log4j2CloudConfigLoggingSystem.ENVIRO
>>> NMENT_KEY);
>>>
>>> ]
>>>
>>>
>>>
>>> Any ideas what I am doing wrong here?
>>>
>>> My log4j2.properties file looks like this:
>>>
>>> status=warn
>>> name=Log4j2PropertiesConfig
>>> packages=za.co.spsi.ice.config
>>>
>>> property.log_dateformat_pattern=-yyyy-MM-dd HH:mm:ss.SSS
>>> property.log_level_pattern=-%5p
>>> property.pattern=%d{${log_dateformat_pattern:-yyyy-MM-dd HH:mm:ss.SSS}}
>>> %highlight{${log_level_pattern:-%5p}}{FATAL=red blink, ERROR=red,
>>> WARN=yellow bold, INFO=green, DEBUG=green bold, TRACE=blue}
>>> %style{${sys:PID}}{magenta} [%15.15t] %style{%-40.40C{1.}}{cyan} :
>>> %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}
>>> property.filename=./poe-buks.log
>>>
>>> appender.null.type=Null
>>> appender.null.name=LogNull
>>>
>>> appender.console.type=Console
>>> appender.console.name=LogToConsole
>>> appender.console.layout.type=PatternLayout
>>> appender.console.layout.pattern=${pattern}
>>>
>>> appender.kafka.type=kafka
>>> appender.kafka.name=LogToKafka
>>> appender.kafka.topic=${spring:kafka.log.topic}
>>> appender.kafka.properties1.type=Property
>>> appender.kafka.properties1.name=bootstrap.servers
>>> appender.kafka.properties1.value=${spring:spring.kafka.bootstrap-servers}
>>> appender.kafka.syncSend=${spring:kafka.log.async}
>>> appender.kafka.layout.type=PatternLayout
>>> appender.kafka.layout.pattern=%d{${log_dateformat_pattern:-yyyy-MM-dd
>>> HH:mm:ss.SSS}} %highlight{${log_level_pattern:-%5p}}{FATAL=red blink,
>>> ERROR=red, WARN=yellow bold, INFO=green, DEBUG=green bold, TRACE=blue}
>>> %style{${sys:PID}}{magenta} [%15.15t] %style{%-40.40C{1.}}{cyan} :
>>> %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}
>>>
>>> # Rotate log file
>>> appender.rolling.type=RollingFile
>>> appender.rolling.name=LogToRollingFile
>>> appender.rolling.fileName=${filename}
>>> appender.rolling.filePattern=${filename}.%d{yyyy-MM-dd}.%i.gz
>>> appender.rolling.layout.type=PatternLayout
>>> appender.rolling.layout.pattern=${pattern}
>>> appender.rolling.policies.type=Policies
>>> appender.rolling.policies.time.type=TimeBasedTriggeringPolicy
>>> appender.rolling.policies.size.type=SizeBasedTriggeringPolicy
>>> appender.rolling.policies.size.size=10MB
>>> appender.rolling.strategy.type=DefaultRolloverStrategy
>>> appender.rolling.strategy.max=10
>>>
>>> appender.routing.type=Routing
>>> appender.routing.name=LogToRoutingAppender
>>> appender.routing.routes.type=Routes
>>> appender.routing.routes.pattern=${sys:kafka.log.enabled}
>>> appender.routing.routes.route1.type=Route
>>> appender.routing.routes.route1.key=false
>>> appender.routing.routes.route1.ref=LogNull
>>> appender.routing.routes.route2.type=Route
>>> appender.routing.routes.route2.key=true
>>> appender.routing.routes.route2.ref=LogToKafka
>>>
>>> logger.kafka.name=org.apache.kafka
>>> logger.kafka.level=error
>>> logger.kafka.additivity=false
>>> logger.kafka.appenderRef.rolling.ref=LogToRollingFile
>>> logger.kafka.appenderRef.routing.ref=LogToRoutingAppender
>>>
>>> logger.spring-kafka.name=org.springframework.kafka
>>> logger.spring-kafka.level=error
>>> logger.spring-kafka.additivity=false
>>> logger.spring-kafka.appenderRef.rolling.ref=LogToRollingFile
>>> logger.spring-kafka.appenderRef.routing.ref=LogToRoutingAppender
>>>
>>> logger.hibernate.name=org.hibernate
>>> logger.hibernate.level=warn
>>> logger.hibernate.additivity=false
>>> logger.hibernate.appenderRef.rolling.ref=LogToRollingFile
>>> logger.hibernate.appenderRef.routing.ref=LogToRoutingAppender
>>>
>>> logger.apache.name=org.apache
>>> logger.apache.level=warn
>>> logger.apache.additivity=false
>>> logger.apache.appenderRef.rolling.ref=LogToRollingFile
>>> logger.apache.appenderRef.routing.ref=LogToRoutingAppender
>>>
>>> #Root logger will log to
>>> rootLogger.level=info
>>> rootLogger.appenderRef.stdout.ref=LogToRollingFile
>>> rootLogger.appenderRef.routing.ref=LogToRoutingAppender
>>>
>>>
>>>
>>>
>>>
>>> Kind regards,
>>>
>>> Buks
>>>
>>>
>>>
>>>
>>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: SpringEnvironmentHolder never gets its Environment initiated

Buks van der Lingen
Hi Ralph,
About to follow up on your initial advice.
I have noticed that the first 2 rounds don’t have my application spring context resolved. The code does however try its darndest to utilize the SpringEnvironmentHolder even for the earlier 2 rounds of initialization. Without diving into the reason for that I suspect it might be for the proper external config properties the spring cloud env caters for. Bootstrap.properties.
In my case all I am interested in are the property sources local to my application normally available through the spring Environment. The bootstrap.properties found externally through the spring config client would form part of the Environment anyway.
Watching the SpringEnvironmentHolder I saw that this environment is actually available but is never used for initialization.
I will let you know.
Regards,
Buks
-----Original Message-----
From: Ralph Goers <[hidden email]>
Sent: Thursday, 02 January 2020 02:06
To: Log4J Users List <[hidden email]>
Subject: Re: SpringEnvironmentHolder never gets its Environment initiated

Buks,

Where you able to find the problem?

Ralph

> On Dec 31, 2019, at 9:11 AM, Ralph Goers <[hidden email]> wrote:
>
> I should also reiterate that in a Spring Boot app logging is initialized at least 3 times. The first and possibly the second do not have a Spring Environment.
>
> Ralph
>
>> On Dec 31, 2019, at 8:39 AM, Ralph Goers <[hidden email]> wrote:
>>
>> I assume this is a Spring Boot app?   The spring-cloud-config-client includes a file named log4j2.system.properties. The values in that file are set as system properties by Log4j 2, so Log4j2CloudConfigLoggingSystem will be used as the LoggingSystem by Spring. Its initialize method sets the Spring Environment into the context, so if that isn’t happening then one of those things isn’t happening properly.
>>
>>
>> Ralph
>>
>>> On Dec 31, 2019, at 12:39 AM, [hidden email] wrote:
>>>
>>> Hi,
>>>
>>>
>>>
>>> I've been trying to get the Spring environment available in log4j2
>>> for a couple of weeks now.
>>>
>>> Mostly using custom lookups and using the reload mechanism. I
>>> haven't fully got it to work when log4j2 ver 2.13.0 got released so
>>> I switched over to try make that work for me.
>>>
>>> What happens is that the SpringEnvironmentHolder never gets
>>> initiated with the Spring Environment.
>>>
>>> The condition: [
>>>
>>> if (environment == null && LogManager.getFactory() != null &&
>>> LogManager.getFactory().hasContext(SpringEnvironmentHolder.class.get
>>> Name(),
>>> null, false)) {
>>>
>>> ]
>>>
>>> Is never true, even though the Spring property sources are mostly available:
>>> [
>>>
>>> Object obj =
>>> LogManager.getContext(false).getObject(Log4j2CloudConfigLoggingSyste
>>> m.ENVIRO
>>> NMENT_KEY);
>>>
>>> ]
>>>
>>>
>>>
>>> Any ideas what I am doing wrong here?
>>>
>>> My log4j2.properties file looks like this:
>>>
>>> status=warn
>>> name=Log4j2PropertiesConfig
>>> packages=za.co.spsi.ice.config
>>>
>>> property.log_dateformat_pattern=-yyyy-MM-dd HH:mm:ss.SSS
>>> property.log_level_pattern=-%5p
>>> property.pattern=%d{${log_dateformat_pattern:-yyyy-MM-dd
>>> HH:mm:ss.SSS}} %highlight{${log_level_pattern:-%5p}}{FATAL=red
>>> blink, ERROR=red, WARN=yellow bold, INFO=green, DEBUG=green bold,
>>> TRACE=blue} %style{${sys:PID}}{magenta} [%15.15t] %style{%-40.40C{1.}}{cyan} :
>>> %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}
>>> property.filename=./poe-buks.log
>>>
>>> appender.null.type=Null
>>> appender.null.name=LogNull
>>>
>>> appender.console.type=Console
>>> appender.console.name=LogToConsole
>>> appender.console.layout.type=PatternLayout
>>> appender.console.layout.pattern=${pattern}
>>>
>>> appender.kafka.type=kafka
>>> appender.kafka.name=LogToKafka
>>> appender.kafka.topic=${spring:kafka.log.topic}
>>> appender.kafka.properties1.type=Property
>>> appender.kafka.properties1.name=bootstrap.servers
>>> appender.kafka.properties1.value=${spring:spring.kafka.bootstrap-ser
>>> vers} appender.kafka.syncSend=${spring:kafka.log.async}
>>> appender.kafka.layout.type=PatternLayout
>>> appender.kafka.layout.pattern=%d{${log_dateformat_pattern:-yyyy-MM-d
>>> d HH:mm:ss.SSS}} %highlight{${log_level_pattern:-%5p}}{FATAL=red
>>> blink, ERROR=red, WARN=yellow bold, INFO=green, DEBUG=green bold,
>>> TRACE=blue} %style{${sys:PID}}{magenta} [%15.15t]
>>> %style{%-40.40C{1.}}{cyan} :
>>> %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}
>>>
>>> # Rotate log file
>>> appender.rolling.type=RollingFile
>>> appender.rolling.name=LogToRollingFile
>>> appender.rolling.fileName=${filename}
>>> appender.rolling.filePattern=${filename}.%d{yyyy-MM-dd}.%i.gz
>>> appender.rolling.layout.type=PatternLayout
>>> appender.rolling.layout.pattern=${pattern}
>>> appender.rolling.policies.type=Policies
>>> appender.rolling.policies.time.type=TimeBasedTriggeringPolicy
>>> appender.rolling.policies.size.type=SizeBasedTriggeringPolicy
>>> appender.rolling.policies.size.size=10MB
>>> appender.rolling.strategy.type=DefaultRolloverStrategy
>>> appender.rolling.strategy.max=10
>>>
>>> appender.routing.type=Routing
>>> appender.routing.name=LogToRoutingAppender
>>> appender.routing.routes.type=Routes
>>> appender.routing.routes.pattern=${sys:kafka.log.enabled}
>>> appender.routing.routes.route1.type=Route
>>> appender.routing.routes.route1.key=false
>>> appender.routing.routes.route1.ref=LogNull
>>> appender.routing.routes.route2.type=Route
>>> appender.routing.routes.route2.key=true
>>> appender.routing.routes.route2.ref=LogToKafka
>>>
>>> logger.kafka.name=org.apache.kafka
>>> logger.kafka.level=error
>>> logger.kafka.additivity=false
>>> logger.kafka.appenderRef.rolling.ref=LogToRollingFile
>>> logger.kafka.appenderRef.routing.ref=LogToRoutingAppender
>>>
>>> logger.spring-kafka.name=org.springframework.kafka
>>> logger.spring-kafka.level=error
>>> logger.spring-kafka.additivity=false
>>> logger.spring-kafka.appenderRef.rolling.ref=LogToRollingFile
>>> logger.spring-kafka.appenderRef.routing.ref=LogToRoutingAppender
>>>
>>> logger.hibernate.name=org.hibernate
>>> logger.hibernate.level=warn
>>> logger.hibernate.additivity=false
>>> logger.hibernate.appenderRef.rolling.ref=LogToRollingFile
>>> logger.hibernate.appenderRef.routing.ref=LogToRoutingAppender
>>>
>>> logger.apache.name=org.apache
>>> logger.apache.level=warn
>>> logger.apache.additivity=false
>>> logger.apache.appenderRef.rolling.ref=LogToRollingFile
>>> logger.apache.appenderRef.routing.ref=LogToRoutingAppender
>>>
>>> #Root logger will log to
>>> rootLogger.level=info
>>> rootLogger.appenderRef.stdout.ref=LogToRollingFile
>>> rootLogger.appenderRef.routing.ref=LogToRoutingAppender
>>>
>>>
>>>
>>>
>>>
>>> Kind regards,
>>>
>>> Buks
>>>
>>>
>>>
>>>
>>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]




---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]