Reputation: 28608
I want to define SMTPAppender in the logger.xml file, but I do not wish to reference it from any logger.
Then when my application loads, I wish to programmatically add this appender to specific loggers.
However the "getAppender" method is accessible only from Logger.
Is there a way to work around this and get the appender without going through any logger?
Upvotes: 0
Views: 1221
Reputation: 1925
Yes!!! absolutely you can do that but sending every event of level may result in too many emails, cluttering the targeted user's mailbox and it also hamper your performances.
Here are the steps by which you can achieve your task :
Step 1: Add janino jars into your classpath, you can download here http://janino.net/changelog.html
Step 2: Add following evaluator in your SMTPAppender:
<evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
<expression>
(marker == null)
</expression>
</evaluator>
Upvotes: 1
Reputation: 1925
I guess, I am not sure whether I have understand your problem or not but I can give a try :
<evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
<marker>MAIL_LOGS</marker>
</evaluator>
<smtpHost>smtp.gmail.com</smtpHost>
<smtpPort>465</smtpPort>
<SSL>true</SSL>
<username>${username}</username>
<password>${password}</password>
<to>${email1}</to>
<from>${from}</from>
<subject>URGENT: ERROR NOTIFICATION </subject>
<cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTrackerImpl">
<bufferSize>20</bufferSize>
</cyclicBufferTracker>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%date%thread%level%logger%msg</pattern>
</layout>
</encoder>
</appender>
and add marker in your logger like
Marker marker= MarkerFactory.getMarker("MAIL_LOGS");
LOG.error(marker, "send this message");
Here all the logs which are marked as marker will get logged by SMTPAppender.
Upvotes: 1