Take a look at the ScriptAppenderSelector - http://logging.apache.org/log4j/2.x/manual/appenders.html#ScriptAppenderSelector <http://logging.apache.org/log4j/2.x/manual/appenders.html#ScriptAppenderSelector> - and see if that does what you want. If not, please let us know.
Ralph > On Oct 26, 2019, at 5:26 AM, Behrang Saeedzadeh <behran...@gmail.com> wrote: > > Let’s say I have three appenders: > > <?xml version="1.0" encoding="UTF-8"?><Configuration status="WARN"> > <Appenders> > <Console name="Console" target="SYSTEM_OUT"> > <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level > %logger{36} - %msg%n"/> > </Console> > > <File name="File" fileName="application.log"> > <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level > %logger{36} - %msg%n" /> > </File> > > <SMTP name="SMTP" > subject="App: Error" > from="lo...@example.com" > to="supp...@example.com" > smtpHost="mailserver" > smtpPort="25" > bufferSize="100"> > </SMTP> > </Appenders></Configuration> > > I want to enable/disable these appenders using system properties in > different environments. > > One option is to control this using scripts: > > <?xml version="1.0" encoding="UTF-8"?><Configuration status="WARN"> > <Scripts> > <Script name="isConsoleAppenderEnabled" language="groovy"><![CDATA[ > return System.getProperty("CONSOLE_APPENDER_ENABLED", > 'true').equalsIgnoreCase('true'); > ]]></Script> > > <Script name="isFileAppenderEnabled" language="groovy"><![CDATA[ > return System.getProperty("FILE_APPENDER_ENABLED", > 'true').equalsIgnoreCase('true'); > ]]></Script> > > <Script name="isSmtpAppenderEnabled" language="groovy"><![CDATA[ > return System.getProperty("SMTP_APPENDER_ENABLED", > 'true').equalsIgnoreCase('true'); > ]]></Script> > </Scripts> > > <Appenders> > <Console name="Console" target="SYSTEM_OUT"> > <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level > %logger{36} - %msg%n"/> > </Console> > > <File name="File" fileName="application.log"> > <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level > %logger{36} - %msg%n" /> > </File> > > <SMTP name="SMTP" > subject="App: Error" > from="lo...@example.com" > to="supp...@example.com" > smtpHost="smtp.example.com" > smtpPort="25" > bufferSize="5"> > </SMTP> > </Appenders> > > <Loggers> > <Root level="info"> > <AppenderRef ref="Console"> > <ScriptFilter onMatch="ACCEPT" onMisMatch="DENY"> > <ScriptRef ref="isConsoleAppenderEnabled"/> > </ScriptFilter> > </AppenderRef> > > <AppenderRef ref="File"> > <ScriptFilter onMatch="ACCEPT" onMisMatch="DENY"> > <ScriptRef ref="isFileAppenderEnabled"/> > </ScriptFilter> > </AppenderRef> > > <AppenderRef ref="SMTP"> > <ScriptFilter onMatch="ACCEPT" onMisMatch="DENY"> > <ScriptRef ref="isSmtpAppenderEnabled"/> > </ScriptFilter> > </AppenderRef> > </Root> > </Loggers></Configuration> > > However, this doesn’t really disable the appender, but computes the value > of the script for each log event and based on that suppresses the event > from being sent to the appender. > > Another option is to write a custom appender that implements this logic and > that can decorate other appenders to achieve this behavior. > > But I was wondering if there’s already a built-in feature available that > can enable/disable appenders based on environment variables and/or system > properties? > > > Best regards, > Behrang Saeedzadeh > blog.behrang.org