Log level in Subject
Status: Beta
Brought to you by:
sullis
Hi,
Hi,
I have made a small change in SMTPHandler.sendBuffer(), so that is somebody uses a string {0} in the subject, e.g.
smtphandler.subject = Log message {0}
then the {0} is replaced by log level of the las log in the cyclic buffer.
// subject
if (subject != null) {
try {
String levelName = cb.getLastAndKeepIt().getLevel().getName();
msg.setSubject(MessageFormat.format(subject, new Object[] {levelName}));
} catch (Exception e) {
reportError("Cannot set subject", e, DEFAULT_ERROR_CODE);
}
}
and CyclicBuffer.java:
public LogRecord getLastAndKeepIt()
{
if (numElems <= 0)
return null;
return records[(last - 1) % maxSize];
}
I have even needed to add the source class and method to the subject. So I have made the following patch. One may set subject as
smtphandler.subject = Log message [{0}] at {1}.{2}
and will get subject like "Log message [SEVERE] at org.mypackage.MyClass.myMethod"
the patch of SMTPHandler.sendBuffer():
// subject
if (subject != null) {
try {
LogRecord lastLogRecord = logBuffer.getLastAndKeepIt();
Object[] subjectParams;
if (lastLogRecord == null) {
subjectParams = new Object[] { };
} else {
subjectParams = new Object[] {
lastLogRecord.getLevel().getName(),
lastLogRecord.getSourceClassName(),
lastLogRecord.getSourceMethodName()};
}
msg.setSubject(MessageFormat.format(subject, subjectParams));
} catch (Exception e) {
reportError("Cannot set subject", e, DEFAULT_ERROR_CODE);
}
}
One more fix, the CyclicBuffer.getLastAndKeepIt() should be like this:
public LogRecord getLastAndKeepIt()
{
if (numElems <= 0)
return null;
return records[(last - 1 + maxSize) % maxSize];
}