Thanks for answering my question - I'm glad to hear that at least
theoretically doing this should work.

However, I already had my loggingServlet changing the level of the logger,
and then verifying that the change took effect.  What happens is that after
I start Tomcat, I run my application code and it logs correctly and
as-expected.  Then when I use my loggingServlet to change the level of the
customLogger to set it's level to OFF, I correctly see in the loggingServlet
that log calls to the customLogger are ignored (also what I want, and
as-expected).  However, when subsequent calls are made in the application
code to customLogger, those log lines ARE written to the logfile (exactly
what I don't want to have happen).

Here's some code from my loggingServlet, my application code, and then my
log4j.xml - any help you can provide is greatly appreciated!



    <from my loggingServlet>

    protected Logger customLogger = LogManager.getLogger(CustomLogger.class
);
    ...........

    customLogger.setLevel(Level.FATAL);
    // these log calls never get written to the logs, which is exactly what
I expect and what I want
    customLogger.debug("***************[DEBUG] sending log message from
loggingServlet after setting customLogger level to FATAL - YOU SHOULD NOT
SEE THIS");
    customLogger.info("***************[INFO] sending log message from
loggingServlet after setting customLogger level to FATAL - YOU SHOULD NOT
SEE THIS");
    customLogger.warn("***************[WARN] sending log message from
loggingServlet after setting customLogger level to FATAL - YOU SHOULD NOT
SEE THIS");
    customLogger.error("***************[ERROR] sending log message from
loggingServlet after setting customLogger level to FATAL - YOU SHOULD NOT
SEE THIS");

    customLogger.setLevel(Level.OFF);
    // these log calls never get written to the logs, which is exactly what
I expect and what I want
    customLogger.debug("***************[DEBUG] sending log message from
loggingServlet after setting customLogger level to OFF - YOU SHOULD NOT SEE
THIS");
    customLogger.info("***************[INFO] sending log message from
loggingServlet after setting customLogger level to OFF - YOU SHOULD NOT SEE
THIS");
    customLogger.warn("***************[WARN] sending log message from
loggingServlet after setting customLogger level to OFF - YOU SHOULD NOT SEE
THIS");
    customLogger.error("***************[ERROR] sending log message from
loggingServlet after setting customLogger level to OFF - YOU SHOULD NOT SEE
THIS");



    <from my application code>

    protected Logger customLogger = LogManager.getLogger(CustomLogger.class
);
    ...........

    // this log line WILL get written to the log file, even after changing
the customLogger level
    // to be OFF or FATAL through the loggingServlet - how can I get this to
not print to the log?
    customLogger.debug("something happened here, so log it")


    <from my log4j.xml>

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

    <log4j:configuration debug="false" xmlns:log4j="
http://jakarta.apache.org/log4j/";>

        <appender name="applicationLog" class="
org.apache.log4j.DailyRollingFileAppender">
            <param name="File" value="application.log"/>
            <param name="Threshold" value="INFO"/>
            <param name="Append" value="false"/>
            <param name="DatePattern" value="'.'yyyy-MM-dd"/>
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%d{DATE} [%t] %-5p %l
- %m%n"/>
            </layout>
        </appender>

        <appender name="customLog" class="
org.apache.log4j.DailyRollingFileAppender">
            <param name="File" value="custom.log"/>
            <param name="Threshold" value="DEBUG"/>
            <param name="Append" value="false"/>
            <param name="DatePattern" value="'.'yyyy-MM-dd"/>
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%d{DATE} [%t] %-5p %l
- %m%n"/>
            </layout>
        </appender>

        <appender name="accessLog" class="
org.apache.log4j.DailyRollingFileAppender">
            <param name="File" value="access.log"/>
            <param name="Threshold" value="INFO"/>
            <param name="Append" value="false"/>
            <param name="DatePattern" value="'.'yyyy-MM-dd"/>
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%d{DATE} [%t] %m%n"/>
            </layout>
        </appender>

        <root>
            <priority value="debug"/>
            <appender-ref ref="customLog"/>
        </root>

        <category name="org.apache.cxf.aegis" additivity="true">
            <priority value="debug"/>
            <appender-ref ref="customLog"/>
        </category>

        <category name="com.myApp.common.logging.CustomLogger"
additivity="true">
            <priority value="info"/>
            <appender-ref ref="applicationLog"/>
            <appender-ref ref="customLog"/>
        </category>

        <category name="com.myApp.common.interceptors.AccessLogInterceptor"
additivity="false">
            <priority value="info"/>
            <appender-ref ref="accessLog"/>
        </category>

    </log4j:configuration>


On Dec 12, 2007 5:39 PM, Matthew Kemp <[EMAIL PROTECTED]> wrote:

> To expand on Orko's comment, the code to do this would look something
> like:
>
> // to get a logger level as a string
> public String getLoggerLevel(String loggerName) {
>  return Logger.getLogger(loggerName).getLevel().toString();
> }
>
> // to set a logger level as a string
> public void setLoggerLevel(String loggerName, String levelName) {
>  Level level = Level.tolevel(levelName);
>  Logger.getLogger(loggerName).setLevel(level);
> }
>
>
> On Dec 12, 2007 4:26 PM, orko <[EMAIL PROTECTED]> wrote:
>
> > There is a method called setLevel in Category. You may want to getLevel
> of
> > the logger and then set it up to a new log level.
> >
> >
> > David Killeffer <[EMAIL PROTECTED]> wrote: I'm trying to write a servlet
> > that will let me change the log level (and
> > thereby optionally turn the logger on/off) of a logger that is running
> in
> > my
> > application code somewhere else.  Currently I have logging running in my
> > application fine, and I have been able to get information on the logger
> > and
> > appenders that are running, but changing the level of the logger doesn't
> > stop the application code from continuing to log.
> >
> > Is it possible to change the log level of a logger in memory
> > (programmatically) by way of a servlet that would then change logging
> for
> > my
> > application code?
> >
> > - David
> >
> >
>



-- 
Best Regards,
David Killeffer

Reply via email to