This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Other JSE/JEE APIs and the fly likes Using SMTPAppender to attach files  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Using SMTPAppender to attach files  " Watch "Using SMTPAppender to attach files  " New topic
Author

Using SMTPAppender to attach files

Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Hello guys,

i couldn't find any param for SMTPAppender file attachement.
Is it possible to attach files using SMTPAppender ?

thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You mean the SMTPAppender that comes as part of log4j, or something else? If that's what you mean then the answer is you are correct. You didn't find a method to send attachments, and therefore there is no way to send attachments.

I can't imagine what you would send in these attachments, since all you've got is a log entry, but you could certainly write a subclass of SMTPAppender that did have such a method.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Originally posted by Paul Clapham:
You mean the SMTPAppender that comes as part of log4j, or something else? If that's what you mean then the answer is you are correct. You didn't find a method to send attachments, and therefore there is no way to send attachments.

I can't imagine what you would send in these attachments, since all you've got is a log entry, but you could certainly write a subclass of SMTPAppender that did have such a method.


Thank you for the hint.
i wrote my own SMTPAppender and use it in place of default log4j SMTPAppender.
it works good now & i can send email attachement.

if someone is interested in this new feature of SMTPAppender file attachement ,just drop me a message.

thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41592
    
  55
I'm curious - like Paul I have problems imagining what might go into those attachments. Could you describe to us the use case you're trying to cover?


Ping & DNS - my free Android networking tools app
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Originally posted by Ulf Dittmer:
I'm curious - like Paul I have problems imagining what might go into those attachments. Could you describe to us the use case you're trying to cover?

Well SMTPAppender just sends an email with the log of original error. it only log events of LEVEL ERROR and higher. but log event's with level WARN, DEBUG ,INFO etc are just lost with an SMTPAppender. am i right ? (well i tried it & it just behave like i described)
so i'm using a second appender to log event's other than Error & Fatal level .then send this log as attachement to email sent by SMTPAppender.
i'm not sure if i have to do that. if default log4j SMTPAppender can do that (i.e send email with all log LEVEL events)than i won't need the attachment.

what you think ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41592
    
  55
I think you have explained why you need a custom version of SMTPAppender, but not why you need it to use attachments. For short one-line emails like these attachments seem like overkill - that's an extra mouse click in most email clients click to open them, and if you set the level to INFO OR DEBUG, you will generally be getting a LOT of emails. (That of course is the reason that SMTPAppender only sends ERROR messages, and nothing lower - it's just not practical.)

By the way, if you configure SMTPAppender programmatically (and not through a config file) you can indeed set the level to anything you want by not using the default constructor but the other one.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

SMTPAppender has two constructors - one without, and one with a TriggeringEventEvaluator

Now as specified in the documentation,
The default constructor will instantiate the appender with a TriggeringEventEvaluator that will trigger on events with level ERROR or higher.

So all you need to do is create an implementation of TriggeringEventEvaluator whose isTriggeringEvent returns true for other levels as well. An example:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

May be i need to explain myself more :
I need my application to send email if a fatal error occurs . (be carefull i don't want to trigger email for logs lower than level error !)
beside, i need the email to contain more descriptive information so i can track what's happening inside the app before the fatal error occured.
But SMPTAppender only logs and sends events of level error and higher. however i'm also interested to get other logs (debug,warn ,info) b/c these infos will help me track the issue in my app .
does this makes sense ?

i think the best slution is to use another appender to log events lower than error level; then send theses logs as attachement to error email.

I'm not sure if default SMPTAppender will be sufficient for requirements i described above ..am i wrong ?
thanks
John Norris
Greenhorn

Joined: Jul 22, 2008
Posts: 14
But SMPTAppender only logs and sends events of level error and higher. however i'm also interested to get other logs (debug,warn ,info) b/c these infos will help me track the issue in my app .

The default behavior of log4j will handle this. The parm <param name="BufferSize" value="1" /> is what you want to set. The value is the number of messages that will be buffered until an ERROR or FATAL message is received.
If you have BufferSize set to 10 and you get 20 messages of info, debug or warn before the error occurs then the last 10(including the error message) will be included in the email.

HTH
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Originally posted by John Norris:

The default behavior of log4j will handle this. The parm <param name="BufferSize" value="1" /> is what you want to set. The value is the number of messages that will be buffered until an ERROR or FATAL message is received.
If you have BufferSize set to 10 and you get 20 messages of info, debug or warn before the error occurs then the last 10(including the error message) will be included in the email.

HTH


thanks ,
your solution seem to be a clean and simple one. i think i'll give it a try . it's true i managed to solve the problem with another work around ;but I'm curious to try your solution and use it if it prove to be simpler and cleaner.

thanks.
Khushi Naik
Greenhorn

Joined: Sep 22, 2008
Posts: 2
Hi,
I am also using SMTpAppender to send mail. That working.
But now i want to confirm is there any way tht we can set threashold level in log4j smtpappender.
for e.g i want to send email only after 10 fatal error instead of each fatal error to consider performance issue.
so is it possible in log4j?
please let me know.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41592
    
  55
Khushi, did you read John's posts regarding the BufferSize parameter?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using SMTPAppender to attach files