• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using SMTPAppender to attach files

 
Ranch Hand
Posts: 510
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,

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

thanks
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 510
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Yahya Elyasse
Ranch Hand
Posts: 510
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Yahya Elyasse
Ranch Hand
Posts: 510
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 510
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Khushi, did you read John's posts regarding the BufferSize parameter?
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic