user1041035
user1041035

Reputation: 31

How to code debug in Java API - programming style

I am writing API that I need to pass to the next developer as a jar file that he/she will use in his program. What is the nice way to write debugg code inside API that will be controlled by the code that is using it.

Thanks a lot.

Upvotes: 0

Views: 158

Answers (3)

Robin
Robin

Reputation: 36611

If you just ship a JAR, it would be so nice to not depend on log4j in the first place, but let users of your plug in their own "LoggerFactory". All the "Loggers" you obtain in your code are obtained through that factory the same way as you would retrieve them from log4j (e.g. by passing the class name), and should have similar levels.

That way customers who want to use log4j still can, and customers who use another logger can plug it in in a jiffy.

Upvotes: 0

James.Xu
James.Xu

Reputation: 8295

I think you are talking about debugging log? use log4j:

if (log.isDebugEnabled()) {
    log.debug("xxxxxxxxxxxxxxxxxxxxxxx");
}

UPDATE: log4j can configure the log level for a specific class: put the following in log4j.properties

log4j.logger.com.test.Test=DEBUG

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160191

  1. Log at different levels: debug level for the nitty-gritty details, info for useful stuff, warn for bad things.
  2. Provide means of testing by exposing your API via interfaces to allow easy mocking. This allows a variety of scenarios to be testing both inside your API, and in the code that's using it.
  3. Provide extension points (using the same interfaces) to allow unforeseen needs to be met as much as possible. (You won't get them all, but that's the way it goes.)
  4. Provide code samples and/or unit tests that exercise the bulk of the API's functionality.
  5. Provide written documentation that explains why things work the way they do. The code explains how it works.

Upvotes: 2

Related Questions