Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro Java
|
Pro Java Expert level Java questions not about a specific book. Please indicate your version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Java section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 19th, 2006, 07:54 AM
Registered User
 
Join Date: Apr 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to albinjoseph Send a message via MSN to albinjoseph Send a message via Yahoo to albinjoseph
Default Log4j - Enable/Disable logging programmatically

hi experts

I am totally new to Log4J. I am facing problem in Log4J. I want to configure Log4J programmatically. I don't want to use any configuration files for configuring Log4J. I want to enable/disable logging facility at runtime from my program. Sometime I may require to turn on only DEBUG, sometimes DEBUG and INFO may be required. How can I do that? Please help me. Any help would be much valuable.

Thanks
Albin Joseph

Thanks and Regards

Albin Joseph
http://www.javacertificate.net/
 
Old July 19th, 2006, 05:53 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Let me try to convince you otherwise first :). Why would you want to do this? If you want to control logging level based on where you are in your application, you can do this with log4j.properties. It allows you to specify certain packages and classes to be more verbose than others. If you can do what you want to do with log4j.properties, I highly recommend it! When it comes to deployment it will be much easier to turn up the logging level without pushing a new build.

But to answer your question -- yes, you can do this. You should write your own appender. It's actually very simple, you just extend the AppenderSkeleton abstract class and implement a few functions. You can also add your own static functions for modifying the current logging level from within your code. Then specify your new appender in your log4j.properties and you're good to go.

I reiterate, though, that you should use log4j.properties to control your logging level. But the option of creating your own appender is always available.

Jon Emerson
http://blogs.adobe.com/jon.emerson/
 
Old July 19th, 2006, 11:26 PM
Registered User
 
Join Date: Apr 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to albinjoseph Send a message via MSN to albinjoseph Send a message via Yahoo to albinjoseph
Default

Hi Emerson,

Thanks for your reply. First let me explain why I need this. I want to use this logging facility in a web application ( A mock exam in site http://www.javacertificate.net/) . Here I would write an admin page where I can disable/enable logging simply by clicking some checkboxes. The advantage of this is that when I use in production environment, if I want to enable logging at any time, I don't want to change properties file from local machine, then upload it. So I think this approach offers me a great flexibility.

Let me try your suggestion. Thanks for your valuable input.


Thanks and Regards

Albin Joseph
http://www.javacertificate.net/
 
Old July 20th, 2006, 01:19 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Albin,

Interesting idea! On our production system we're not allowed to upload files either, so having an administration console that can toggle logging would be excellent. I'll have to see what my coworkers think of it :).

Jon Emerson
http://blogs.adobe.com/jon.emerson/
 
Old July 20th, 2006, 09:23 AM
Registered User
 
Join Date: Apr 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to albinjoseph Send a message via MSN to albinjoseph Send a message via Yahoo to albinjoseph
Default

Thanks Emerson, if you get any idea for that please let me know. What exactly I am looking is some code like

public class MyClass extends SomeClassFromLogginAPI {
    public void enableInfo(boolean flag) {
       super.flagForEnablingInfo = boolean;
    }

    public void enableDebug(boolean flag) {
       super.flagForEnablingDebug = flag;
    }
}


MyClass obj = new MyClass();
obj.enableInfo(true);


Thanks a lot
Albin Joseph
http://www.javacertificate.net
 
Old July 20th, 2006, 07:20 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Something like this:

import org.apache.log4j.*;

public class MyClass extends org.apache.log4j.FileAppender
{
    private static Priority minimumPriority = Priority.ERROR;

    public void append(LoggingEvent event)
    {
         if (event.isGreaterOrEqual(minimumPriority))
         {
              super.append(event);
         }
    }

    public static void setMinimumPriority(Priority p)
    {
        this.minimumPriority = p;
    }

    public static void enableInfo()
    {
        this.setMinimumPriority(Priority.INFO);
    }

    public static void enableDebug()
    {
        this.setMinimumPriority(Priority.DEBUG);
    }
}

Make sure that in your log4j.properties, you include this appender just like a FileAppender. Set the default priority in log4j.property to INFO so that it receives all events and can filter them on its own.

Jon Emerson
http://www.jonemerson.net/
 
Old July 25th, 2006, 11:36 PM
Registered User
 
Join Date: Apr 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to albinjoseph Send a message via MSN to albinjoseph Send a message via Yahoo to albinjoseph
Default

Thanks Emerson

Thanks and Regards

Albin Joseph
http://www.javacertificate.net/
 
Old July 26th, 2006, 05:15 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm glad you got it working :).

Jon Emerson
http://www.jonemerson.net/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using JS to enable/disable?? crabjoe Javascript 4 November 10th, 2008 06:48 PM
Disable and Enable at runtime Hammam ASP.NET 1.0 and 1.1 Professional 0 May 17th, 2006 12:40 AM
dynamically enable/disable radio btns kanoorani Javascript How-To 3 October 17th, 2005 10:16 AM
Disable/Enable elements ldoodle Javascript How-To 3 June 7th, 2005 05:48 AM
Logging with Log4j - ERROR pedersen JSP Basics 1 October 1st, 2003 08:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.