0

I have a method that depends on object creation from a different class, as well as some helper methods that come from a third class. The specific needs of this project mean that the logger is required to log errors that we can trace back to the particular method that invoked them (these methods are all invoked at different points in the control flow). In our case, we have a Context object that is being passed down through multiple methods (it's a batch job) which contains the info we need to trace back where a particular log was invoked. Right now we're just passing this along as a parameter, but this is not good practice. I've put some code to illustrate.

public class MainClassOne {
    //...

    //Context is an object that contains a lot of info about the current job being processed
    public void mainMethodOne(Context context) {
        //do stuff
        DomainObject domainObject = new DomainObject(context, ...);
    }
}

//doing this now forces out domain objects to become dependant on the Context object that it doesn't need to know anything about
public class DomainObject {
    public DomainObject(Context invokingMethod,...) {
        //domain logic here
        //log stuff here - we need to know certain information about the context to adequately log
    }
}

This type of scenario is played out in several places all over our code and I'm not sure how to best clean it up. One proposed solution is to use a ThreadLocal map like MDC. This is what I was leaning towards also, but I'm not sure it's the best way forward.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.