Last Updated Mar 23, 2014 — Enterprise Agile Planning expert
Building VersionOne Integrations with Java (Part 3: Logging)
Enterprise Agile Planning
In this part of the series, we’re going to discuss how to make logging an integral part of your integrations.
Here’s where we are in this series:- Part 1: Introduction
- Part 2: Handling Configurations
- Part 3: Logging (YOU ARE HERE)
- Part 4: Parsing Data
- Part 5: Using a HTTP Client
- Part 6: Getting Data
- Part 7: Submitting Data
- Part 8: Executing Operations
- Part 9: Authenticating with OAuth
- Part 10: Conclusion
# Logger settings. log4j.rootLogger=ALL, file, stdout # Log file message settings. log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=./logs/test.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} %/5p %c{1}:%L / %m%n # Log console message settings. log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} %/5p %c{1}:%L / %m%nThere are a few things going on here. The first is setting the rootLogger, which controls which types of messages actually get logged, and it also specifies the appenders that are used to do the logging. The next two sections describe the appenders and how they are used. Of particular interest is the ConversionPattern property that controls the format of the messages that are logged. Once you have the logger setup within your properties file, using it in code is a matter of instantiating the logger at the class level like so:
static Logger logger = Logger.getLogger(YourClass.class);Notice that you pass the name of your class to the getLogger method. Next you need to tell the logger where to find its configurations, which in this example is the properties contained in the properties file. You do that like this:
PropertyConfigurator.configure("config.properties");Once you have that done, you simply call the logger when you have something that you want logged like this:
logger.info("Application started...");The logger supports different types of log types such as TRACE, DEBUG, INFO, WARN, and ERROR, and it is up to you to decide which type to use and when. But what about integrations? Is there anything specific that you should think about logging? The answer is absolutely. Some example of the types of things that you may want to log include:
- Application initialization
- User authentication
- System state when the integration was launched
- API authorization success/failure
- API call information such as URLs and query parameters
- API call responses with status codes
- And of course, exceptions