| java.lang.Object | ||
| ↳ | java.util.logging.Handler | |
| ↳ | com.google.code.com.sun.mail.util.logging.MailHandler | |
Handler that formats log records as an email message.
This Handler will store a fixed number of log records used to
generate a single email message. When the internal buffer reaches capacity,
all log records are formatted and placed in an email which is sent to an
email server. The code to manually setup this handler can be as simple as
the following:
Properties props = new Properties();
props.put("mail.smtp.host", "my-mail-server");
props.put("mail.to", "me@example.com");
props.put("verify", "local");
MailHandler h = new MailHandler(props);
h.setLevel(Level.WARNING);
Configuration:
The LogManager must define at least one or more recipient addresses and a
mail host for outgoing email. The code to setup this handler via the
logging properties can be as simple as the following:
#Default MailHandler settings.
com.sun.mail.util.logging.MailHandler.mail.smtp.host = my-mail-server
com.sun.mail.util.logging.MailHandler.mail.to = me@example.com
com.sun.mail.util.logging.MailHandler.level = WARNING
com.sun.mail.util.logging.MailHandler.verify = local
All mail properties documented in the Java Mail API cascade to the
LogManager by prefixing a key using the fully qualified class name of this
MailHandler dot mail property. If the prefixed property is not
found, then the mail property itself is searched in the LogManager.
By default each MailHandler is initialized using the following
LogManager configuration properties. If properties are not defined,
or contain invalid values, then the specified default values are used.
Sorting: All LogRecord objects are ordered prior to formatting if this Handler has a non null comparator. Developers might be interested in sorting the formatted email by thread id, time, and sequence properties of a LogRecord. Where as system administrators might be interested in sorting the formatted email by thrown, level, time, and sequence properties of a LogRecord. If comparator for this handler is null then the order is unspecified.
Formatting:
The main message body is formatted using the Formatter returned by
getFormatter(). Only records that pass the filter returned by
getFilter() will be included in the message body. The subject
Formatter will see all LogRecord objects that were
published regardless of the current Filter. The MIME type of the
message body can be overridden
by adding a MIME MimetypesFileTypeMap entry using the simple
class name of the body formatter as the file extension. The MIME type of the
attachments can be overridden by changing the attachment file name extension
or by editing the default MIME entry for a specific file name extension.
Attachments: This Handler allows multiple attachments per each email. The attachment order maps directly to the array index order in this Handler with zero index being the first attachment. The number of attachment formatters controls the number of attachments per email and the content type of each attachment. The attachment filters determine if a LogRecord will be included in an attachment. If an attachment filter is null then all records are included for that attachment. Attachments without content will be omitted from email message. The attachment name formatters create the file name for an attachment. Custom attachment name formatters can be used to generate an attachment name based on the contents of the attachment.
Push Level and Push Filter: The push method, push level, and optional push filter can be used to conditionally trigger a push at or prior to full capacity. When a push occurs, the current buffer is formatted into an email and is sent to the email server. If the push method, push level, or push filter trigger a push then the outgoing email is flagged as high importance with urgent priority.
Buffering: Log records that are published are stored in an internal buffer. When this buffer reaches capacity the existing records are formatted and sent in an email. Any published records can be sent before reaching capacity by explictly calling the flush, push, or close methods. If a circular buffer is required then this handler can be wrapped with a java.util.logging.MemoryHandler typically with an equivalent capacity, level, and push level.
Error Handling:
If the transport of an email message fails, the email is converted to
a javax.mail.internet.MimeMessage#writeTo raw
string
and is then passed as the msg parameter to
reportError along with the exception
describing the cause of the failure. This allows custom error managers to
store, javax.mail.internet.MimeMessage#MimeMessage(
javax.mail.Session, java.io.InputStream) reconstruct, and resend the
original MimeMessage. The message parameter string is not a raw email
if it starts with value returned from Level.SEVERE.getName().
Custom error managers can use the following test to determine if the
msg parameter from this handler is a raw email:
public void error(String msg, Exception ex, int code) {
if (msg == null || msg.length() == 0 || msg.startsWith(Level.SEVERE.getName())) {
super.error(msg, ex, code);
} else {
//The 'msg' parameter is a raw email.
}
}
| Fields | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| attachmentFilters | Holds the filters for each attachment. | ||||||||||
| attachmentFormatters | Holds the formatters that create the content for each attachment. | ||||||||||
| attachmentNames | Holds the formatters that create the file name for each attachment. | ||||||||||
| auth | Holds the authenticator required to login to the email server. | ||||||||||
| capacity | The maximum number of log records to format per email. | ||||||||||
| comparator | Used to order all log records prior to formatting. | ||||||||||
| contentTypes | Used to override the content type for the body and set the content type for each attachment. | ||||||||||
| data | Holds all of the log records that will be used to create the email. | ||||||||||
| isWriting | Determines if we are inside of a push. | ||||||||||
| mailProps | Holds all of the email server properties. | ||||||||||
| offValue | Cache the off value. | ||||||||||
| pushFilter | Holds the push filter for trigger conditions requiring an early push. | ||||||||||
| pushLevel | Holds the push level for this handler. | ||||||||||
| sealed | Used to turn off security checks. | ||||||||||
| session | Holds the session object used to generate emails. | ||||||||||
| size | The number of log records in the buffer. | ||||||||||
| subjectFormatter | Holds the formatter used to create the subject line of the email. | ||||||||||
|
[Expand]
Inherited Fields | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.util.logging.Handler
| |||||||||||
| Public Constructors | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
Creates a MailHandler that is configured by the
LogManager configuration properties.
| |||||||||||
Creates a mail handler with the specified capacity.
| |||||||||||
Creates a mail handler with the given mail properties.
| |||||||||||
| Public Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
Prevents any other records from being published.
| |||||||||||
Pushes any buffered records to the email server as normal priority.
| |||||||||||
Gets the attachment filters.
| |||||||||||
Gets the attachment formatters.
| |||||||||||
Gets the attachment name formatters.
| |||||||||||
Gets the Authenticator used to login to the email server.
| |||||||||||
Gets the number of log records the internal buffer can hold.
| |||||||||||
Gets the comparator used to order all LogRecord objects prior
to formatting.
| |||||||||||
Gets a copy of the mail properties used for the session.
| |||||||||||
Gets the push filter.
| |||||||||||
Gets the push level.
| |||||||||||
Gets the formatter used to create the subject line.
| |||||||||||
Check if this Handler would actually log a given
LogRecord into its internal buffer.
| |||||||||||
Stores a LogRecord in the internal buffer.
| |||||||||||
Pushes any buffered records to the email server as high importance with
urgent priority.
| |||||||||||
Sets the attachment filters.
| |||||||||||
Sets the attachment Formatter object for this handler.
| |||||||||||
Sets the attachment file name for each attachment.
| |||||||||||
Sets the attachment file name formatters.
| |||||||||||
Sets the Authenticator used to login to the email server.
| |||||||||||
Sets the comparator used to order all LogRecord objects prior
to formatting.
| |||||||||||
Set the log level specifying which message levels will be
logged by this Handler.
| |||||||||||
Sets the mail properties used for the session.
| |||||||||||
Sets the push filter.
| |||||||||||
Sets the push level.
| |||||||||||
Sets the subject formatter for email.
| |||||||||||
Sets a literal string for the email subject.
| |||||||||||
| Protected Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
Protected convenience method to report an error to this Handler's
ErrorManager.
| |||||||||||
|
[Expand]
Inherited Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.util.logging.Handler
| |||||||||||
From class
java.lang.Object
| |||||||||||
Holds the filters for each attachment. Filters are optional for each attachment.
Holds the formatters that create the content for each attachment. Each formatter maps directly to an attachment. The formatters getHead, format, and getTail methods are only called if one or more log records pass through the attachment filters.
Holds the formatters that create the file name for each attachment. Each formatter must produce a non null and non empty name. The final file name will be the concatenation of one getHead call, plus all of the format calls, plus one getTail call.
The maximum number of log records to format per email. Used to roughly bound the size of an email. Every time the capacity is reached, the handler will push. The capacity will be negative if this handler is closed. Negative values are used to ensure all records are pushed.
Used to order all log records prior to formatting. The main email body and all attachments use the order determined by this comparator. If no comparator is present the log records will be in no specified order.
Used to override the content type for the body and set the content type for each attachment.
Holds all of the log records that will be used to create the email.
Determines if we are inside of a push. Makes the handler properties read-only during a push.
Holds all of the email server properties.
Cache the off value.
Holds the push filter for trigger conditions requiring an early push. Only gets called if the given log record is greater than or equal to the push level and the push level is not Level.OFF.
Holds the push level for this handler. This is only required if an email must be sent prior to shutdown or before the buffer is full.
Used to turn off security checks.
Holds the session object used to generate emails. Sessions can be shared by multiple threads. See BUGID 6228391
The number of log records in the buffer.
Holds the formatter used to create the subject line of the email. A subject formatter is not required for the email message. All published records pass through the subject formatter.
Creates a MailHandler that is configured by the LogManager configuration properties.
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
|---|
Creates a mail handler with the specified capacity.
| capacity | Of the internal buffer. |
|---|
| IllegalArgumentException | if capacity less than one. |
|---|---|
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
Creates a mail handler with the given mail properties. The key/value pairs are defined in the Java Mail API documentation. This Handler will also search the LogManager for defaults if needed.
| props | A non null properties object. |
|---|
| NullPointerException | if props is null. |
|---|---|
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
Prevents any other records from being published. Pushes any buffered records to the email server as normal priority. The internal buffer is then cleared. Once this handler is closed it will remain closed.
If this Handler is only implicitly closed by the LogManager, then verification should be turned on.
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
|---|
Pushes any buffered records to the email server as normal priority. The internal buffer is then cleared. Does nothing if called from inside a push.
Gets the attachment filters. If the attachment filter does not allow any LogRecord to be formatted, the attachment may be omitted from the email.
Gets the attachment formatters. This Handler is using attachments only if the returned array length is non zero.
Gets the attachment name formatters. If the attachment names were set using explicit names then the names can be returned by calling toString on each attachment name formatter.
Gets the Authenticator used to login to the email server.
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
|---|
Gets the number of log records the internal buffer can hold. When capacity is reached, Handler will format all LogRecord objects into one email message.
Gets the comparator used to order all LogRecord objects prior to formatting. If null then the order is unspecified.
Gets a copy of the mail properties used for the session.
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
|---|
Gets the push filter. The default is null.
Gets the push level. The default is Level.OFF meaning that this Handler will only push when the internal buffer is full.
Gets the formatter used to create the subject line. If the subject was created using a literal string then the toString method can be used to get the subject line.
Check if this Handler would actually log a given LogRecord into its internal buffer.
This method checks if the LogRecord has an appropriate level and whether it satisfies any Filter including any attachment filters. However it does not check whether the LogRecord would result in a "push" of the buffer contents.
| record | A LogRecord |
|---|
Stores a LogRecord in the internal buffer.
The isLoggable method is called to check if the given log record is loggable. If the given record is loggable, it is copied into an internal buffer. Then the record's level property is compared with the push level. If the given level of the LogRecord is greater than or equal to the push level then the push filter is called. If no push filter exists, the push filter returns true, or the capacity of the internal buffer has been reached then all buffered records are formatted into one email and sent to the server.
| record | Description of the log event. |
|---|
Pushes any buffered records to the email server as high importance with urgent priority. The internal buffer is then cleared. Does nothing if called from inside a push.
Sets the attachment filters.
| filters | A non null array of filters. A null index value is allowed. A null value means that all records are allowed for the attachment at that index. |
|---|
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
|---|---|
| NullPointerException | if filters is null |
| IndexOutOfBoundsException | if the number of attachment name formatters do not match the number of attachment formatters. |
| IllegalStateException | if called from inside a push. |
Sets the attachment Formatter object for this handler. The number of formatters determines the number of attachments per email. This method should be the first attachment method called. To remove all attachments, call this method with empty array.
| formatters | A non null array of formatters. |
|---|
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
|---|---|
| NullPointerException | if the given array or any array index is null. |
| IllegalStateException | if called from inside a push. |
Sets the attachment file name for each attachment. The caller must ensure that the attachment file name does not contain any line breaks. This method will create a set of custom formatters.
| names | An array of names. |
|---|
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
|---|---|
| IndexOutOfBoundsException | if the number of attachment names do not match the number of attachment formatters. |
| IllegalArgumentException | if any name is empty. |
| NullPointerException | if any given array or name is null. |
| IllegalStateException | if called from inside a push. |
Sets the attachment file name formatters. The format method of each attachment formatter will see only the LogRecord objects that passed its attachment filter during formatting. The format method will typically return an empty string. Instead of being used to format records, it is used to gather information about the contents of an attachment. The getTail method should be used to construct the attachment file name and reset any formatter collected state. The formatter must ensure that the attachment file name does not contain any line breaks. The toString method of the given formatter should be overridden to provide a useful attachment file name, if possible.
| formatters | And array of attachment name formatters. |
|---|
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
|---|---|
| IndexOutOfBoundsException | if the number of attachment name formatters do not match the number of attachment formatters. |
| NullPointerException | if any given array or name is null. |
| IllegalStateException | if called from inside a push. |
Sets the Authenticator used to login to the email server.
| auth | An Authenticator object or null if none is required. |
|---|
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
|---|---|
| IllegalStateException | if called from inside a push. |
Sets the comparator used to order all LogRecord objects prior to formatting. If null then the order is unspecified.
| c | The LogRecord comparator. |
|---|
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
|---|---|
| IllegalStateException | if called from inside a push. |
Set the log level specifying which message levels will be logged by this Handler. Message levels lower than this value will be discarded.
| newLevel | The new value for the log level |
|---|
| NullPointerException | if newLevel is null. |
|---|---|
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
Sets the mail properties used for the session. The key/value pairs are defined in the Java Mail API documentation. This Handler will also search the LogManager for defaults if needed.
| props | A non null properties object. |
|---|
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
|---|---|
| NullPointerException | if props is null. |
| IllegalStateException | if called from inside a push. |
Sets the push filter. This filter is only called if the given LogRecord level was greater than the push level. If this filter returns true, all pending records are formatted and sent to the email server. When the push filter triggers a send, the resulting email is flagged as high priority.
| filter | Push filter or null |
|---|
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
|---|---|
| IllegalStateException | if called from inside a push. |
Sets the push level. This level is used to trigger a push so that all pending records are formatted and sent to the email server. When the push level triggers a send, the resulting email is flagged as high priority.
| level | Level object. |
|---|
| NullPointerException | if level is null. |
|---|---|
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
| IllegalStateException | if called from inside a push. |
Sets the subject formatter for email. The format method of the subject formatter will see all LogRecord objects that were published to this Handler during formatting and will typically return an empty string. This formatter is used to gather information to create a summary about what information is contained in the email. The getTail method should be used to construct the subject and reset any formatter collected state. The formatter must ensure that the subject line does not contain any line breaks. The toString method of the given formatter should be overridden to provide a useful subject, if possible.
| format | The subject formatter. |
|---|
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
|---|---|
| NullPointerException | if format is null. |
| IllegalStateException | if called from inside a push. |
Sets a literal string for the email subject. The caller must ensure that the subject line does not contain any line breaks.
| subject | A non null string. |
|---|
| SecurityException | if a security manager exists and the caller does not have LoggingPermission("control"). |
|---|---|
| NullPointerException | if subject is null. |
| IllegalStateException | if called from inside a push. |
Protected convenience method to report an error to this Handler's ErrorManager. This method will prefix all non null error messages with Level.SEVERE.getName(). This allows the receiving error manager to determine if the msg parameter is a simple error message or a raw email message.
| msg | A descriptive string (may be null) |
|---|---|
| ex | An exception (may be null) |
| code | An error code defined in ErrorManager |