mercredi 19 février 2020

Calling methods conditionally from two different classes

I have two classes ExceptionLog and DebugLog

public class ExceptionLog {
    public static String StackTrace {get; set;}
    public static String ClassName {get; set;}
    public static String MethodName {get; set;}
    public static String LogType {get;set;}
    public static Exception ex {get;set;}

    public Static void Debug(Exception ex)
    {
        logType = 'EXCEPTION'; 
        ex = ex;
        log();
    }

    public Static void log()
    {
        try
        {
            extractException(); 
            writeToObject(); 

        }
        catch(Exception e)
        {
            //new ExceptionLog().Module('LogException').log(e);            
        }    
    }

    public static void extractException()
    {
        // Logic here            
    }

    public static void writeToObject()
    {        
        // data save to object logic here       
    }    
}

and

public class DebugLog {
    public static String LogType {get;set;}
    public static String DebugMessage {get;set;} 

    public Static void Debug(String message)
    {
        Debug(null, message);
    }

    public Static void Debug(LoggingLevel level, String message)
    {
        if(level != null )
        {
            LogType = String.valueOf(level);             
        }        

        DebugMessage = message;

        log();
    }

    public Static void log()
    {
        // Log logic here   
    }        

}

What I want to achieve is, write a controller class that will make decision of which debug method needs to be called

public class Log {
    public void Debug(String message)
    {
        DebugLog.Debug(message);
    }
    public void Debug(loggingLevel loggingLevel, String message)
    {
        DebugLog.Debug(loggingLevel, message);    
    }
    public void Debug(Exception ex)
    {
        ExceptionLog.Debug(ex);
    }
}

That is, if I pass Exception in the debug method, it will call the ExceptionLog.Debug(ex) else it will call the debug method from DebugLog class.

I know the the object oriented approach is not good here. How can I design the classes more elegantly or any design pattern fit here?

Aucun commentaire:

Enregistrer un commentaire