This question aroused my curiosity, so I researched it a bit. I have not
tried this, but it looks like the direction you would need to go.

You need to think of it not as I am adding additional levels. You are adding
a class that defines additional logging levels. Below I call this class
PhilLevels. Come up with a better name.

First off you will want to assign numeric values for your levels so that
they fall into order with the predefined levels. Not sure if your additional
debug levels are meant to be above or below the normal DEBUG level. Here are
the relevant levels now:

                    INFO_INT = 20000,
                    DEBUG_INT = 10000,
                    TRACE_INT = 5000,

I assume your new levels would either be between 5000 and 10000 or between
10000 and 20000 depending on where you want them to fall. The numbers
provide the ordering so that when I set the level to n then I see messages
that have a level >=n.

You then need to declare class that defines your levels:

classs PhilLevels
{
    DECLARE_LOG4CXX_LEVEL( PhilLevels ):

     enum
     {
         DEBUG_1_INT = 11000 /* And so forth for the other levels */
     }

     static LevelPtr getDebug1(); /* and so forth */

     // You must provide this, see the one in Logger.cpp
     // If it doesn't match one of your levels you should probably
     // call Logger::toLevel
     static LevelPtr toLevel(int val);

     // You must provide this, see the one in Logger.cpp
     // If it doesn't match one of your levels you should probably
     // call Logger::toLevelLS
     static LevelPtr toLevelLS(const LogString& sArg);
};

In a .cpp file somewhere you need to add:

    IMPLEMENT_LOG4CXX_LEVEL( PhilLevels );

To log using this level you can use

    LOG4CXX_LOG( logger, PhilLevels::getDebug1(), "message" );

But you will probably want to add macros like:

#define LOG4CXX_DEBUG_1(logger, message) { \
        if (LOG4CXX_UNLIKELY(logger->isEnabledFor(
PhilLevels::getDebug1()))) {\
           ::log4cxx::helpers::MessageBuffer oss_; \
           logger->forcedLog(PhilLevels::getDebug1(), oss_.str(oss_ <<
message), LOG4CXX_LOCATION); }}

To specify them in your configuration file you have to use a slightly
different syntax. To set the logging level to DEBUG_1 you would say
something like:

   log4j.rootLogger = DEBUG_1#PhilLevels, myAppender

The # specifies the class to look in to look up the level name. It will call
toLevelLS on your class with an argument of "DEBUG_1"

Note that this is actually pretty much the same way it is done in Log4J with
some additional issues due to C++ vs. Java.


On Thu, Jun 18, 2009 at 12:05 PM, Phil Longstaff <plongst...@sandvine.com>wrote:

>  There’s an item in the FAQ about adding logging levels which says it is
> possible but that you probably don’t want to.  Well, I do want to.  I want
> to replace a home-grown logging system which has 5 DEBUG levels, so I want
> to augment log4cxx by adding DEBUG_1 through DEBUG_5.  However, I can’t find
> documentation on how to do this.
>
>
>
> How do I add new levels?
>
>
>
> Phil Longstaff
>
> Senior Software Engineer
>
> Direct: 519-880-2400 ext 2904
>
> www.sandvine.com
>
>
>



-- 
Dale King

Reply via email to