In this session, we will see how to enable logging for a bundle. We will later create a logger configuration in Felix console and will specify a file to write the logs. Enabling logging in a bundle helps you a lot while debugging. We will use Simple Logging Facade for Java (SLF4J). It’s an abstraction of various logging frameworks based on Java.
Note that it’s a continuation of my previous session. Refer to the previous videos when in doubt. You can download the sample project from GitHub. You may need aem-company-site-1.0.zip and aem-company.zip.
- Let us open the Eclipse project that we have created.
- Locate the HelloServiceImpl.java class.
- Import org.slf4j.Logger and org.slf4j.LoggerFactory.
- Add the following statement.
public static final Logger LOGGER = LoggerFactory.getLogger(HelloServiceImpl.class);
Using the LoggerFactory instance, it creates a logger for the class passed.
- Inside the getRepositoryName() method, add the Logger.Info method.
LOGGER.info("Adding the repository");
- Log into Felix console:
- Go to Sling > Log Support.
- Select Add New Logger.
- Provide the logs name. (logs\aem-company.log)
- Then, select the logger class name. (com.aemcompany.myproject)
- Now access the web page created earlier.
It has a component that uses this class to print the repository name.
Means, when we access the page, the method gets executed – resulting a log entry.
- Go to the crx-quickstart\logs folder.
Note that the aem-company log is created.