logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark Masterson" <m.master...@computer.org>
Subject RE: Anyone using SNMPTrapAppender?
Date Sun, 18 Jul 2004 16:01:54 GMT
Hello Bret,

>>how does the logging change from the program's side (i.e. the user of the
Log4J Logger)?<<.

Whoa, nelly.  Bret, here's a serious, if typical, log4j newbie problem --
you haven't yet entirely grasped the nature of log4j.  The concrete answer
to your question is -- you don't change anything in your code.  Ever.  For
any reason.

This is one of the major benefits of using log4j -- you just write
"myLog.fatal("Something bad happened");" and you're done.  You can -- and
must -- then write a configuration file for log4j, that tells it what to do
when your code is invoked.  This configuration file can be arbitrarily
complex, with any given quantity and combination of logging targets
(appenders) and formats (pattern layouts).  Any changes you want to make in
the "how" of logging that affects those targets and/or formats is done in
your configuration file.  You can't find an example of calling code that's
specific to this particular pattern layout (or any other, for that matter)
because there is no such thing.  The code is always the same.

Now, on to the SnmpDelimitedConversionPatternLayout and how to use it.  The
SnmpDelimitedConversionPatternLayout allows you to take each discrete
element of a log4j LoggingEvent (such as the message, the level and so on)
and map it to a particular, distinct OID.  Moreover, when you use this
layout pattern and technique, the appender recognizes that, and appends each
element thus defined as a separate VarBind to the trap PDU, using the
OID-to-LoggingEvent element mapping that you specify in the pattern layout
format string.

Thus, the ultimate answer to your question -- "How do I make this work?" is
the same as the answer to almost every other question I get on the appender:
write a correct configuration file for it.  As such, the effort involved is
really pretty minimal.

Here is an example, in Properties file format, of how to use it:

log4j.appender.SNMP_LOG.layout="org.apache.log4j.ext.SnmpDelimitedConversion
PatternLayout"
log4j.appender.SNMP_LOG.layout.ConversionPattern="p;1.3.6.1.4.1.24.100.1/%m;
1.3.6.1.4.1.24.100.2/%C;1.3.6.1.4.1.24.100.3"

These two properties do the following; they set the layout (of an appender
named SNMP_LOG) to the SnmpDelimitedConversionPatternLayout, and then
configures that layout so that:

-- the "p", or Level, element of the LoggingEvent is associated with the OID
1.3.6.1.4.1.24.100.1
-- the "m", or message, element of the LoggingEvent is associated with the
OID 1.3.6.1.4.1.24.100.2
-- the "C", of fully qualified class name of the caller element of the
LoggingEvent is associated with the OID 1.3.6.1.4.1.24.100.3 (Note: this is
a bad example, and I should fix it on the Web site -- I should really use
the "c" rather than the "C" element, to encourage the associated performance
related log4j best practice...)

In this example, the individual elements are delimited from one another
using "/", and within an element, the LoggingEvent element pattern character
is delimited from the associated OID by a ";".  These are the defaults, they
can be changed like this:

log4j.appender.SNMP_LOG.layout="org.apache.log4j.ext.SnmpDelimitedConversion
PatternLayout"
log4j.appender.SNMP_LOG.layout.ValuePairDelim="\"
log4j.appender.SNMP_LOG.layout.ValuePairDelim=":"
log4j.appender.SNMP_LOG.layout.ConversionPattern="p;1.3.6.1.4.1.24.100.1/%m;
1.3.6.1.4.1.24.100.2/%C;1.3.6.1.4.1.24.100.3"

... which changes the inter-element delimiter to "\" and the intra-element
delimiter to ":".

Note that what you CANNOT do with the SnmpDelimitedConversionPatternLayout
is have "user-defined" VarBinds, where individual "elements" of the
LoggingEvent *message* are parsed and added as separate VarBinds. This
actually seems to be what you want to do with things like the port number
and so forth.  Note that it would not be difficult to DO this -- you would
need a parser class that gets called from the
SnmpDelimitedConversionPatternLayout when it hits the "m" element, and which
breaks the message further down into the user-defined elements.  What is
nearly impossible is to do it *generically*, which is why I can't really do
it for you in the appender.  How could I, as the author of the appender,
possibly know what elements your logging message would have?  But it would
be almost trivial for YOU to take the source code of the appender, write
such a LoggingEventMessageParser (or whatever) and thus get the thing to do
exactly what you want it to...

Hope this helps.

Cheers,
Mark


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message