logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ollie Brady <ollie.br...@macalla.com>
Subject Adding an ObjectRenderer
Date Mon, 03 Mar 2003 16:28:31 GMT
Hi,

I'm porting an application from Log4j 1.1.3 to 1.2.8 but am having a problem
adding an ObjectRenderer that I wrote.

The ObjectRenderer, NodeRenderer, is used to dump XML nodes to log without
formatting/altering them.

I would like to add the new renderer without using a config file, i.e. all
in code.

The old code looked like

    static
    {
        Category catRoot = Category.getRoot();
        catRoot.setPriority(Priority.INFO);
        Layout layout = new PatternLayout("%d{dd MMM yyyy HH:mm:ss,SSS} %-5p
%-30.30c %x - %m\n");
        APPENDER = new ConsoleAppender();
        ConsoleAppender ca = (ConsoleAppender)APPENDER;
        ca.setLayout(layout);
        ca.setName("Console Logger");
        ca.setWriter(new java.io.OutputStreamWriter(System.out));
        catRoot.addAppender(APPENDER);

        // create renderer for XML messages
        ObjectRenderer nodeRenderer = new ObjectRenderer()
        {
            public String doRender(Object obj)
            {
                return XMLHelper.nodeToString((Node)obj);
            }
        };
        catRoot.getHierarchy().addRenderer(Node.class, nodeRenderer);
    }



So in the new version I have created two classes..

class NodeRenderSupport
{
    private RendererMap m_map=new RendererMap();

    public RendererMap getRendererMap()
    {
        return m_map;
    }

    public void setRenderer(Class renderedClass, ObjectRenderer renderer)
    {
        m_map.put(renderedClass, renderer);
    }
};

class NodeObjectRenderer implements ObjectRenderer
{
    public String doRender(Object obj)
    {
        return XMLHelper.nodeToString((Node)obj);
    }
};

..and then have a line like this in the application..

    static
    {
        Category catRoot = Category.getRoot();
        catRoot.setPriority(Priority.INFO);
        Layout layout = new PatternLayout("%d{dd MMM yyyy HH:mm:ss,SSS} %-5p
%-30.30c %x - %m\n");
        APPENDER = new ConsoleAppender();
        ConsoleAppender ca = (ConsoleAppender)APPENDER;
        ca.setLayout(layout);
        ca.setName("Console Logger");
        ca.setWriter(new java.io.OutputStreamWriter(System.out));
        catRoot.addAppender(APPENDER);

        NodeRenderSupport nodeRendererSupport=new NodeRenderSupport();
        nodeRendererSupport.setRenderer(Node.class, new
NodeObjectRenderer());
	  
	  // How/where to add the NodeRenderSupport??

    }

..but where then do I then add the nodeRendererSupport??  I cannot find
anywhere to explicitly add the renderer..? 
Any comments welcome.

Regards,
/OB.

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


Mime
View raw message