9 Replies Latest reply on Dec 11, 2016 7:59 AM by Bill Shannon-Oracle

    How to set the debug log level in javamail?

    popanz

      Hello,

       

      when activating the debug logging on sending mails with javamail thru property "mail.debug" or thru javax.mail.Session.setDebug(true), you always get at huge debug output, including the whole content of the sent mails, and even including the binary code of all attachments.

      When sending a newsletter with attachments to 20.000 receivers, this output gets insane.

       

      Is there any way to at least skip the log output of the mail contents, or at least of the attachments?

      All I found for influencing the debug log is the on/off switch mentioned before.

      But no way to simply set the log level...?

       

      Thanks for help!

        • 1. Re: How to set the debug log level in javamail?
          Bill Shannon-Oracle

          You can get more fine grained control by using java.util.logging.  The package javadocs for the JavaMail packages list the Loggers that are used for each package.

          • 2. Re: How to set the debug log level in javamail?
            popanz

            Hello Bill,

             

            Thanks for the answer!

            Is there an example somewhere how to do that?

            I have no clue on doing this and can't find any matching example.

            The only thing i found about javamail and setting a logger or a loglevel are examples about com.sun.mail.util.logging.MailHandler, but thats obviously something completely different.

            The simple task of setting a log level seems to become a high skilled challenge... :-(

             

            Thanks!

            • 3. Re: How to set the debug log level in javamail?
              Bill Shannon-Oracle

              The problems is, it depends...

               

              Most people using java.util.logging will configure it using a logging.properties configuration file.  If you already have such a properties file (e.g., because you're running in an application server), you can add lines such as:

               

              com.sun.mail.imap.level = FINE

               

              If you want to do it all in code, you can use something like this:

               

                  Logger logger = Logger.getLogger("com.sun.mail.imap");

                  logger.setLevel(Level.FINE);

                  ConsoleHandler handler = new ConsoleHandler();

                  handler.setFormatter(new SimpleFormatter());

                  handler.setLevel(Level.FINE);

                  logger.addHandler(handler);

               

              You'll need to spend more time with the java.util.logging documentation to improve either of those.

              • 4. Re: How to set the debug log level in javamail?
                popanz

                Hello Bill,

                 

                big thanks for your code example!

                But unfortunately it didn't work with me.

                You should place that code before the javax.mail.Session gets instantiated, right?

                 

                Shouldn't it say Logger.getLogger("com.sun.mail.smtp") or Logger.getLogger("javax.mail") instead of "com.sun.mail.imap"?

                ...Although none of them worked either.

                Just to make sure, I used Level.WARNING, to see a reliable reduction of log entries (which didn't happen).

                 

                And shouldn't the logger get connected to the javax.mail.Session, with a javax.mail.Session.setLogger(Logger) or something?

                (Of course, there is no such method - that would make it too easy.)

                 

                 

                I also found a class in the JavaMail library, which is obviously meant for handling java.util.logging in JavaMail:

                com.sun.mail.util.MailLogger

                 

                But no clue, how to connect this to a javax.mail.Session either and how to set the log level on this class (again no setLevel() method or similar).

                 

                This class is obviously not even documented in the JavaMail javadoc. :-/

                 

                Thanx a lot for your patience! :-)

                • 5. Re: How to set the debug log level in javamail?
                  Bill Shannon-Oracle

                  (I really, really, really hate the editor in this stupid forum...)

                   

                  > You should place that code before the javax.mail.Session gets instantiated, right?

                   

                  Normally, yes, although you can do it at any time.

                   

                  > Shouldn't it say Logger.getLogger("com.sun.mail.smtp") or Logger.getLogger("javax.mail") instead of "com.sun.mail.imap"?

                   

                  Well, it depends on which logger you want to control.  That was just an example.

                   

                  > Just to make sure, I used Level.WARNING, to see a reliable reduction of log entries (which didn't happen).

                   

                  As the javadocs describe, nothing is logged at WARNING level.  The logging occurs at CONFIG, FINE, or FINEST.

                   

                  > And shouldn't the logger get connected to the javax.mail.Session, with a javax.mail.Session.setLogger(Logger) or something?

                   

                  Nope.  The java.util.logging loggers are "global".

                   

                  > I also found a class in the JavaMail library, which is obviously meant for handling java.util.logging in JavaMail:

                  >

                  > com.sun.mail.util.MailLogger

                  >

                  > But no clue, how to connect this to a javax.mail.Session either and how to set the log level on this class (again no setLevel() method or similar).

                  >

                  > This class is obviously not even documented in the JavaMail javadoc. :-/

                   

                  Right, because it's not there for you to use, it's an implementation detail.

                   

                  To be clear, nothing we're talking about above is specific to JavaMail.

                  It's really just all about java.util.logging, for which you can find examples and tutorials.

                  • 6. Re: How to set the debug log level in javamail?
                    popanz

                    > (I really, really, really hate the editor in this stupid forum...)

                     

                    what happened?

                     

                     

                    > > Just to make sure, I used Level.WARNING, to see a reliable

                    > > reduction of log entries (which didn't happen).

                    > As the javadocs describe, nothing is logged at WARNING level. 

                    > The logging occurs at CONFIG, FINE, or FINEST.

                     

                    Thats exactly what I wanted for testing.

                    I have no idea in which way the output is supposed to look different to the standard way, if I use CONFIG, FINE or FINEST.

                    But if the level actually gets adjusted to WARNING, there would be no output - so i know, that my adjustments finally work.

                    And if a have success in this, then I can adjust the level to what brings the best results.

                     

                     

                    BUT... I didn't have success in this at all! :-(

                     

                    And I think I'll give it up, trying to set the bloody log level for the smtp log output.

                    I wasted about 2 days of my lifetime in the frustration of searching how to manage this ridiculous task.

                     

                    I read 3 tutorials about java.util.logging.

                    In each of them, they say it's a "simple" or "lightweight" java feature.

                     

                    When a java expert mentions the word "simple", that already should ring a bell, telling you, that you will have to dedicate the next few months just to find out, how "simple" your problem can be solved...

                     

                     

                    Anyways, I tried your code example (I found the same in the tutorials) with logger of...

                     

                    com.sun.mail.smtp.SMTPTransport

                    com.sun.mail.smtp

                    com.sun.mail

                    com.sun.mail.util.MailLogger

                    javax.mail.Session

                    javax.mail

                     

                    Each of them in distinct attempts, and each of them once with Level.WARNING and once with Level.CONFIG.

                    Nothing made any change to the huge log output.

                     

                    The log output says "DEBUG SMTP" just before the mail content, which should be skipped.

                    Checking the java class sources, this output obviously comes from the logger of com.sun.mail.smtp.SMTPTransport.

                    This logger is a com.sun.mail.util.MailLogger which uses the package of the given class as logger name.

                    So the logger's name of the problematic output should obviously be "com.sun.mail.smtp".

                    Somehow nice to know, but this insight didn't really help at all to success.

                     

                    So I also tried to place settings in the logging.properties...

                     

                    com.sun.mail.smtp.SMTPTransport.level = CONFIG

                    com.sun.mail.smtp.level = CONFIG

                    com.sun.mail.level = CONFIG

                    com.sun.mail.util.MailLogger.level = CONFIG

                    javax.mail.Session.level = CONFIG

                    javax.mail.level = CONFIG

                     

                    ...and also (in another attempt) same with: ....= WARNING

                     

                     

                    Since no tutorial said, which logging.properties (at what location), I tried the one in the tomcat "conf" folder (yes, it's about a web application running in tomcat), and the one in the "lib" folder of the java installation.

                    This little logging.properties from that java-lib-folder, I also tried in the classes- and in the lib-folder of the tomcat common-class-loader, which also loads the mail.jar.

                     

                    I also pointed at the logging.properties thru the tomcat start parameter -Djava.util.logging.config.file .

                    And also pointed to it thru System.setProperty("java.util.logging.config.file", "/<path-to>/logging.properties");

                     

                    None of my attempts made any change to the very extensive log output.

                    Did anyone ever try, if this javamail logging (with this com.sun.mail.util.MailLogger) on certain levels is working correctly?

                    Anyways, I'm giving up... :-(

                     

                    Thanks for your help!

                    • 7. Re: How to set the debug log level in javamail?
                      Bill Shannon-Oracle

                      Are you still turning on JavaMail debug output (e.g., using session.setDebug(true)) at the same time you're configuring java.util.logging?  If you turn on debug output, you get it all.  If you turn it off, and enable java.util.logging, you get finer control over the output.  (Well, really, they're both completely independent, so you can get both or one or neither.)

                       

                      If Tomcat has an existing logging.properties file, you should be able to add the configuration to that file and see the logging output in Tomcat's log file.

                       

                      In the future, if you want faster response to your questions, try sending mail to javamail_ww@oracle.com.  That might help avoid some of the frustration you're feeling.  :-)

                      • 8. Re: How to set the debug log level in javamail?
                        popanz

                        Oh wow, of course I still had session.setDebug(true), and since I switched it off, the java.util.logging suddenly starts to work, thank you! :-)
                        Of course it perfectly makes sense, that session.setDebug(false) enables the logging of the java lib, oh man... :-p

                        And this weird logic is not even mentioned with a single word in the javadoc! :-(

                         

                        Ok, so now quite happy that it finally works!

                        But... I have to admit that using java.util.logging does not really help that much with my problem, since at least the smtp part of javamail just knows two levels of log output: FINE (-> with some base information about the communication with the mailserver) and FINEST (-> with everithing about the communication, including the whole contents of the mail, even with the binary codes of all attachments).

                         

                        FINE is not really enough about the communication - e.g. the mail header would be interesting and especcially the mail transfer result (e.g. the line "250 OK") would be very interesting! (not available at this level!)

                        But FINEST is way too much, blowing up log files with stuff you just would exceptionally need.

                        Why isn't FINER used at all - that would be perfect to log everything except the mail content?


                        I guess I'll write a Formatter now, for filtering out the multipart contents.

                        The java.util.logging.SimpleFormatter is quite annoying anyways, since it always produces 2 lines on every single log output, making a log file quite unreadable and very bad for processing with linux "grep" or something.

                         

                        Anyways, thank you for your help! :-)

                        • 9. Re: How to set the debug log level in javamail?
                          Bill Shannon-Oracle

                          Yes, the documentation assumes a certain familiarity with java.util.logging.

                           

                          The choice of what's logged at what level is largely an artifact of the implementation.

                          All communication with the server is indiscriminately sent through a filter that logs

                          each line at the FINEST level.  Information that's not part of the communication with

                          the server is individually logged at the FINE or CONFIG level.

                           

                          It would not be impossible to separately log each individual command and response,

                          but separating the header of the message from the body of the message and logging

                          it separately seems like more work than it's worth.

                           

                          Perhaps your application should log the message attributes you think are important before

                          sending the message?

                           

                          In any event, feel free to file an enhancement request against JavaMail with more detail

                          about what you would like to see logged.