Menu

#3 Log level in Subject

open
nobody
None
5
2009-06-05
2009-06-05
No

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];
}

Discussion

  • Ondrej Medek

    Ondrej Medek - 2009-06-05
    • summary: several changes --> Log level in Subject
     
  • Ondrej Medek

    Ondrej Medek - 2009-06-09

    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);
    }
    }

     
  • Ondrej Medek

    Ondrej Medek - 2009-06-09

    One more fix, the CyclicBuffer.getLastAndKeepIt() should be like this:

    public LogRecord getLastAndKeepIt()
    {
    if (numElems <= 0)
    return null;

    return records[(last - 1 + maxSize) % maxSize];
    }

     

Log in to post a comment.