Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Logging

  • 15-02-2002 3:46pm
    #1
    Registered Users, Registered Users 2 Posts: 4,676 ✭✭✭


    Howdy,

    I'm looking for a good approach to java logging. I want to log my classes to varying levels. ie set the logging priority to a certain number ranging from light to heavy.

    I've seen a few options out there, namely class libraries etc, but I'd prefer to do my own. At least for the time being.

    I was thinking of creating a logging interface and a logger class.

    every class that wishes to be logged implements the interface. The interface will have simple enough methods like, returning the name of the class etc..

    So then,

    I create a new instance of the logger class and have a number of overloaded methods int it.

    eg, save(logging _class, Exception e)
    save(logging _class, String varold, String varnew)
    save(logging _class, int varold, int varnew)

    So in every method in every class, i call the appropriate logging method. eg for a setXXX() method, i will call it with (this, oldvalue, newvalue )

    Does that make sense ? Anyone got any better approaches ?

    Cheers,
    Gav


    ( in the logger class itself, i can then decide what sort of data i want to log, according to the priority level )


Comments

  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    You do not need to create an interface for Logging. Since every class can potentially use a logger there is no point in creating one. Your best bet is to use the new logging API in jdk1.4. If this is not viable, then use the same approach as Sun have used for their logging API .Use a logger class to do the work for you. In each method that you want to use to print info out, then just call the Logger method

    Logging API in JDK1.4



    public class Logger
    {
    public static final int INFO=0;
    public static final int DEBUG=1;
    public static final int ERROR=2;

    public static void Log(int level,Class c, String s, Exception e)
    {
    System.out.println("Class:"+c.getName());
    if(level==DEBUG)
    System.out.println("Message:"+s);
    if(level==ERROR)
    System.out.println("Exception:"+e.getMessage());
    }
    }

    public MyClass
    {
    ....
    public void foo(String s)
    {
    try{
    ....}catch(Exception e)
    {
    Logger.log(Logger.DEBUG,this,s,e);
    }
    }

    You could even define a 'global' Logging level in the logger class, i.e.

    public static final LEVEL=0;

    and in the log method

    if(LEVEL==0)
    return; //do nothing
    else
    System.out.println......... etc

    Therefore simply changing LEVEL to 0 would stop all log output and changing it back to 1 would enable it in all classes simply by changing the code in one place as opposed to multiple places.

    Hope this helps,
    Lynchie


Advertisement