On Jun 6, 2007, at 9:59 AM, Naider Chen wrote:
Hi: Can any one confirm to me it's working to call doRender() method. I'll appreciate if you can provide me a successful example if you do make it work. I use log4j-1.2.14 in RAD6 and put log4j.xml in the WEB-INF/classes folder and I'm sure the added <renderer> has been loaded.
There is an ORTestCase in the test suite (and which currently passes), but the tests are inadequate to ensure that doRender is being called in normal processing.
I tried the complete example fromhttp://www.java2s.com/Code/Java/Language-Basics/ log4jAcompleteexample.htmbut the doRender() was never got called and it's not calling to my own test to add <renderer> to the log4j.xml and extend ObjectRenderer in my render class with doRender() method. I just have to know if this is the right way to call doRender() and capture the message to change it.
There are a multitude of errors in that sample, but if you fix all of them, the OrderRenderer.doRender is called using log4j 1.2.14. To get the sample to run, I needed to:
Remove non-breaking spaces from the sourceMake all the classes "public" and place them in separate files named after the classes. Extract the sample configuration file from the end of the sample and name it log4j.xml
Remove the asterisks used to comment out the configuration file remove the space between <renderer> and </renderer>Change the name of the logger in the configuration from "com.apress.logging.log4j" to "name" Remove all the com.apress.* package names from the configuration file since the Java source did not have any package statements.
If you did that, the output of the sample program was: main INFO name - Beer-101-20 main INFO name - Chocolate-223-5 If you commented out the renderer element, you got: main INFO name - [EMAIL PROTECTED] main INFO name - [EMAIL PROTECTED]
I tried what Ron has suggested me to use LoggingEvent object to filter the message to do getMessage() and getRenderedMessage(). but it's not good to me since there are no setRenderedMessage() to alter the message that I want to return.
--- Naider Chen <[EMAIL PROTECTED]> wrote:Thanks Ron: Thank you for your reply and I really appreciate it. May be The getRenderedMessage method in the LoggingEvent is another place that I can intercept the log message. But I'm just beginning to look how to use log4j and I need to do some more research to find out how do I overwrite the getRenderedMessage method to scan and pre-process the message. Can I extend the LoggingEvent and do something to have log4j use my child LoggingEvent method so that I can do what I need to do with the message. What I really need to do is to create a utilities to be used to plug in to the application code so that all the log message can be scanned/pre-processed before sending to the Appender. And of course the application code need to do the little the better.
You needs would appear to be better served with either a Filter or Layout. An object renderer would only get invoked when a object other than a String is passed as the message and custom renderer is associated with the type. Filters or Layouts would see all logging requests, an ObjectRenderer would only see the subset that uses your special type.
I'm sure the added <renderer> tag in my log4j.xml is loaded in my server but what I don't understand is that the doRender method is not get called. Could that be because a String object is been bypassed because it's already a String so that there is no need to render it again? I thought the doRender(object o) will catch all the object. Is this right? It this doRender approach the deadend to intercept the message or I can still catch a String object in doRender?
There is a check in LoggingEvent.getRenderedMessage() that bypasses rendering if the message is already a string. That behavior can't be changed at this date. You can't use an ObjectRenderer to manipulate the message, however you may be able to use a Layout to get what you want.
I have changed the renderedClass="java.lang.Object" to Object instead of String but it's the same thing doRender method print statement can't be found in my consoleAppender.
The rendered class is used as a key into a map of renders in org.apache.log4j.or.RendererMap.findAndRender(). When searching for a renderer the value from msg.getClass() is used on the lookup, so a Renderer will only match precisely the class specified as the rendered class. So if a renderer is registered for "java.lang.Object", the renderer would only get invoked if you were making calls like:
logger.debug(new Object());
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true"> <renderer renderedClass="java.lang.Object" renderingClass="test.log4j.LogCatcher"/> Here's the ocnsole log that shows the new <renderer> tag showned in the logger. [6/1/07 15:28:09:453 EDT] 0000002a SystemOut O log4j: Rendering class: [test.log4j.LogCatcher], Rendered class: [java.lang.Object]. [6/1/07 15:28:09:453 EDT] 0000002a SystemOut O log4j: Level value for root is [debug]. [6/1/07 15:28:09:453 EDT] 0000002a SystemOut O log4j: root level set to DEBUG [6/1/07 15:28:09:469 EDT] 0000002a SystemOut O log4j: Class name: [org.apache.log4j.ConsoleAppender] [6/1/07 15:28:09:500 EDT] 0000002a SystemOut O log4j: Setting property [threshold] to [DEBUG]. [6/1/07 15:28:09:500 EDT] 0000002a SystemOut O log4j: Parsing layout of class: "org.apache.log4j.PatternLayout" [6/1/07 15:28:09:657 EDT] 0000002a SystemOut O log4j: Setting property [conversionPattern] to [%d %-5p [%t] %C{2} (%F:%L) - %m%n]. [6/1/07 15:28:09:657 EDT] 0000002a SystemOut O log4j: Adding appender named [STDOUT] to category [root]. [6/1/07 15:28:09:672 EDT] 0000002a ServletWrappe A SRVE0242I: [TestCsfEAR] [/x] [EntitlementServlet]: Initialization successful. [6/1/07 15:28:09:704 EDT] 0000002a SystemOut O 2007-06-01 15:28:09,704 ERROR [WebContainer : 1] log4j.EntitlementServlet (EntitlementServlet.java:59) - log this info !!@@##$$%%^^&&**()++ --- "Gallagher, Ron" <[EMAIL PROTECTED]> wrote:Naider -- The purpose of Renderers is to generate a String representation of an Object. Since a String is already a String,there'sno need to do any additional rendering. The getRenderedMessage method in the LoggingEventiswhat is responsible for calling any defined renderers. This methodhasan explicit check for situations where the object that's about be rendered is an instance of String. In that case, the Object to render is simply cast into a String and used as-is. Ron Gallagher, AT&T Mobility
That's right.
-----Original Message----- From: Naider Chen [mailto:[EMAIL PROTECTED] Sent: Thursday, May 31, 2007 5:26 PM To: Log4J Users List Subject: Unable to call doRender. Am I missing anything? I'm trying to intercept the logging message to filter some keyword before forwarding to the destination.I tested it in a windows environment using RAD6 I add the new <renderer> tag entry to log4j.xmlandput both log4j.dtd and log4j.xml in the WEB-INF/classes folder but after I re-compile and bounce the server the doRender method was nevergetcalled. Am I missing snything? Please help. Thanks. <renderer renderedClass="java.lang.String" renderingClass="test.log4j.LogCatcher"/> I create a new class test.log4j.LogCatcher as follows: package test.log4j; import org.apache.log4j.*; import org.apache.log4j.spi.*; import org.apache.log4j.or.ObjectRenderer; public class LogCatcher implements ObjectRenderer{public String doRender(Object arg0) { String st = arg0.toString(); System.out.println("LogCatcher doRender**********log string catched = *" + st + "*"); return st; } }
Might be good if you try to explain exactly what you are trying to do. From what you describe, it seems like a Filter or Layout is much better fit than an ObjectRenderer.
smime.p7s
Description: S/MIME cryptographic signature
