Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I wrote this class in order to not having to write tag every time (because of code duplication and risk of typos).

public class TaggedLogger {

    public static final TaggedLogger GLOBAL = new TaggedLogger("Global");

    private final String tag;

    public static TaggedLogger forClass(Class<?> someClass) {
        return new TaggedLogger(someClass.getName());
    }

    public TaggedLogger(String tag) {
        this.tag = tag;
    }

    public void debug(String message) {
        Log.d(tag, message);
    }

    public void info(String message) {
        Log.i(tag, message);
    }

    public void exception(String message) {
        Log.e(tag, message);
    }

    public void exception(String message, Throwable exception) {
        Log.e(tag, message, exception);
    }

    // skipped

}

Using:

public class MainActivity extends Activity {
    private static final TaggedLogger LOGGER = TaggedLogger.forClass(MainActivity.class);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LOGGER.debug("Initialization: start");
        init(savedInstanceState);
    }

    // skipped

}

What do you think? Maybe this class is useless?

Help me choose a more appropriate name for this class (if TaggedLogger isn't good) ?

share|improve this question
    
1. Apart from simons comments I would like to suggest you that your Constructor TaggedLogger() should be private ,So you will force developer to create logger instance via your static forClass() Method 2. Are you using GLOBAL somewhere else in the code ? if not then you can remove it –  Shree Sep 1 at 12:13

1 Answer 1

up vote 2 down vote accepted

I do not consider your class useless, I have actually done a similar thing myself. Very similar actually. Perhaps even exactly the same, pretty much.

Overall, your code is very well-written. You use final and private where you can, which I like.


A few comments:

private static final TaggedLogger LOGGER = TaggedLogger.forClass(MainActivity.class);

Even though it is correct that constants are written in ALL_CAPS. One exception for this is loggers. If you write LOGGER.info and stuff often, that LOGGER will pick on your eyes after a while. I would recommend using the name logger (lowercase) instead.


public static final TaggedLogger GLOBAL = new TaggedLogger("Global");

I am not sure if you really need this one. Especially not inside that class.

It is good that you have not made your class a singleton at least, but I'm not sure if this public static 'global' is really necessary.


I can recommend using log4j for Android for logging. Log4j is a very nice and popular logging framework for Java, and it is possible to get it working on Android.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.