If you use the "FQCN stuff" and wrap the generic log method the yes, the
module, method, and line number traces will work as expected.

Does anyone know if there is a way for a new thread inherit the NDC from its
parent?
When I create a new thread it doesn't contain any NDC - documentation says
the NDC is
on a per thread basis and that is a great idea but I'd like to be able to
"initialize"
it so to speak.

Craig

-----Original Message-----
From: Mark Womack [mailto:[EMAIL PROTECTED]]
Sent: Thursday, August 23, 2001 5:35 PM
To: 'LOG4J Users Mailing List'
Subject: RE: log4j wrapper


Craig, et al,

My wrapper is almost exactly the same, except I do not have the FQCN stuff.
Is this what people are talking about having so that the detailed logging
information (stack trace, etc) will work properly?

Here are some excerpts from my class that might interest people.  It has a
static initializer that looks for a property.  If the initialization does
not work, for whatever reason, it initializes to a console at level "WARN".
I'm pretty sure I have posted bits of it before.


        public static final int DEBUG   = Priority.DEBUG_INT;
        public static final int INFO    = Priority.INFO_INT;
        public static final int WARN    = Priority.WARN_INT;
        public static final int ERROR   = Priority.ERROR_INT;
        public static final int FATAL   = Priority.FATAL_INT;

    public static final String tracePropertyName = "trace.properties";

    private static HashMap instances = new HashMap();

        static

                // if it appears that log4j is not configured, then set up
                // a default configuration so that at least warning or above
end
                // up in the console.
                if (!isConfigured())

                        doConfigure();
                }
        }

        private Category category;

        protected Logger(Category _category)
        {
                category = _category;
        }

        public int getPriority() {
                return category.getPriority().toInt();
        }

        public void setPriority(int priority) {
                category.setPriority(Priority.toPriority(priority));
        }

        public static TraceOutput getInstance(Class classToUseAsCategory)
        {
                return getInstance(classToUseAsCategory.getName());
        }

        public static TraceOutput getInstance(String nameToUseAsCategory)
        {
                TraceOutput retTrace = null;

                synchronized(instances) {
                        retTrace =
(TraceOutput)instances.get(nameToUseAsCategory);
                        if (retTrace == null) {
                                retTrace = new
TraceOutput(Category.getInstance(nameToUseAsCategory));
                                instances.put(nameToUseAsCategory,
retTrace);
                        }
                }

                return retTrace;
        }

        /**
                This code (from Ceki Gülcü) checks to see if there are any
appenders defined for log4j
                which is the definitive way to tell if log4j is already
initialised
        **/
        private static boolean isConfigured() {
                Enumeration enum = Category.getRoot().getAllAppenders();
                if (!(enum instanceof
org.apache.log4j.helpers.NullEnumeration)) {
                        return true;
                }
                else {
                        Enumeration cats =  Category.getCurrentCategories();
                        while (cats.hasMoreElements()) {
                                Category c = (Category) cats.nextElement();
                                if (!(c.getAllAppenders() instanceof
org.apache.log4j.helpers.NullEnumeration))
                                        return true;
                        }
                }
                return false;
        }

        private static void doBaseConfigure() {
                // set up the default configuration
                Layout layout = new PatternLayout("%d{ABSOLUTE} %-5p %c{1}
%x : %m%n");
                Appender appender = new ConsoleAppender(layout);
                Category log4jRoot = Category.getRoot();
                log4jRoot.addAppender(appender);
                log4jRoot.setPriority(Priority.WARN);

                getRoot().forceLog(INFO, "base configuration
(ConsoleAppender/WARN) applied to root category");
        }

        private static void doConfigure() {

        try {
                String traceSettings =
System.getProperty(tracePropertyName);

                URL traceURL = new URL(traceSettings);

                // is it an xml file?
                if (traceSettings.endsWith(".xml")) {
                        DOMConfigurator configurator = new
DOMConfigurator();
                        configurator.configure(traceURL);
                }
                // otherwise, must be a property file
                else {
                        PropertyConfigurator configurator = new
PropertyConfigurator();
                        configurator.configure(traceURL);
            }

                // if still not configured at this point, then throw and
exception to
                // force base configuration
                if (!isConfigured())
                        throw new Exception("TraceOuput configuration
exception");

                        getRoot().forceLog(INFO, "configuration applied from
" + tracePropertyName + " property");

        } catch (Exception e) {
                doBaseConfigure();
                return;
        }
        }

-----Original Message-----
From: Craig Newlander [mailto:[EMAIL PROTECTED]]
Sent: Thursday, August 23, 2001 11:49 AM
To: LOG4J Users Mailing List
Subject: RE: log4j wrapper


Ok - here is my wrapper class.

Feel free to critqiue it as long offer imporvements!

Craig


/*
 * Logger.java
 *
 * Created on August 23, 2001, 9:00 AM
 */

package "put your package name here";

import org.apache.log4j.*;

/**
 * @author cnewlander
 * @version 1.0
 */

public class Logger {

    /** This is a wrapper class for log4j
     */
    Category c;

    /** The name of this wrapper class
     */
    static String FQCN = Logger.class.getName();

    /** Constructor which returns the root content
     */
    public Logger() {
        c = Category.getRoot();
    }

    /** creates a {@link Category} object
     * @param name name reference for this category to create
     */
    public Logger(Class name) {
        c = Category.getInstance(name.getName());
    }

    /** Clear diagnostics stack
     */
    public static void Remove()

        NDC.remove();
    }

    /**  pop diagnostics stack
     */
    public static void Pop() {
        NDC.pop();
    }

    /** push a msg onto diagnostics stack
     * @param msg msg to push onto the NDC
     */
    public static void Push( String msg ) {
        NDC.push( msg );
    }

    /**  get a String of the diagnostics stack
     * @return see NDC
     */
    public static String Get() {
        return NDC.get();
    }

    /** Configure
     * @param propsFile full path name of properties file
     * @param refreshDwell in miliseconds
     */
    public static void configureAndWatch( String propsFile, int
refreshDwell ) {
        PropertyConfigurator.configureAndWatch( propsFile, refreshDwell );
    }

    /** log an debug message
     * @param msg the message
     */
    public void debug(String msg) {
        c.log(FQCN, Priority.DEBUG, msg, null);
    }

    /** log an debug message
     * @param msg Wrapper
     * @param t a Throwable
     */
    public void debug(String msg, Throwable t) {
        c.log(FQCN, Priority.DEBUG, msg, t );
    }

    /** log an error message
     * @param msg Wrapper
     */
    public void error(String msg) {
        c.log(FQCN, Priority.ERROR, msg, null);
    }

    /** log an error message
     * @param msg Wrapper
     * @param t a Throwable
     */
    public void error(String msg, Throwable t) {
        c.log(FQCN, Priority.ERROR, msg, t);
    }

    /** log an warn message
     * @param msg Wrapper
     */
    public void warn(String msg) {
        c.log(FQCN, Priority.WARN, msg, null);
    }

    /** log an warn message
     * @param msg Wrapper
     * @param t a Throwable
     */
    public void warn(String msg, Throwable t) {
        c.log(FQCN, Priority.WARN, msg, t);
    }

    /** log an info message
     * @param msg Wrapper
     */
    public void info(String msg) {
        c.log(FQCN, Priority.INFO, msg, null);
    }

    /** log an info message
     * @param msg Wrapper
     * @param t a Throwable
     */
    public void info(String msg, Throwable t) {
        c.log(FQCN, Priority.INFO, msg, t);
    }

    /** is debugging enabled (use this before debug() )
     * @return Wrapper
     */
    public boolean isDebugEnabled() {
        return c.isDebugEnabled();
    }
}



-----Original Message-----
From: Don Taylor [mailto:[EMAIL PROTECTED]]
Sent: Thursday, August 23, 2001 1:38 PM
To: LOG4J Users Mailing List
Subject: Re: log4j wrapper


That's why you need to provide the fully-qualified classname. If you
use the wrapper I showed, you will not have this problem. That is, the
class and method name of the wrapper caller, not the wrapper itself,
will be logged. The code which determines the logging method looks one
past the fully-qualified classname in the callstack. Check it out, I
thought it was rather ingenious myself.

-- Don

--- [EMAIL PROTECTED] wrote:
> One piece of warning about wrapping the log4j api's.  The code which
> determines the logging method is dependent on that method being the
> last
> method in the call stack prior to the Category logging method.
> Consequently,
> if you wrap the log4j calls, then every logging message will appear
> to be
> coming from the wrapper methods rather than the method requesting the
> log
> message.
> This is only an issue if you want to have access to the extended
> runtime
> information
> (class, method, line number, etc) in your logs.
>
> -Mike Wolf
>
>
>
>
>
> "Craig Newlander" <[EMAIL PROTECTED]>
> 08/23/01 11:27 AM
> Please respond to "LOG4J Users Mailing List"
>
>
>         To:     "LOG4J Users Mailing List"
> <[EMAIL PROTECTED]>
>         cc:
>         Subject:        log4j wrapper
>
>
> Hello,
>
>    I'd like to absract log4j from my appilication so I don't have to
> do a
> import org.apache.log4j.* throughout my source files and be dependant
> on
> the
> Category class.   What is a good method to employ here?
>
> Craig
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to