May 22, 2020
Estimated Post Reading Time ~

How to use and configure logback in Adobe CQ5 projects?

I newbie in CQ5. I started my first project in CQ and I want to write any exceptions and debug info of project's components, services and servlets (in bundles) to log files in crx-repo (...\crx-quickstart\logs).

I want to use slf4j with logback implementation in my project.

I tried below steps:
Add dependencies in project's pom.xml

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.7</version>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.3</version>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.1.3</version>
</dependency>

Add logback.xml with configuration (loggers and appenders) to the project's bundle resource package.

Try to instantiate new logger in service (for example):

 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 .....

 @Component(metatype = true, label = "Some", description = "Sone service" , 
 immediate = true)
 @Service(SomeService.class)

 public class SomeService {        

     private final Logger logger = LoggerFactory.getLogger(MyDao.class);
     .....
     logger.debug("debug");
     logger.info("info");
     logger.error("error");
     ......
  }

These steps (It doesn't work, of course) I gave an example because I don't know how really work with logback in Adobe CQ5. Any suggestions? I would be grateful for any help!

Best How To:
CQ does log to crx-quickstart/logs/error.log with its default configuration, and the underlying Apache Sling framework provides the necessary bridge so you just need to acquire an slf4j Logger and write to that.

The Java code of your SomeService example looks correct to me but in the pom you only need the slf4j-api dependency, with scope provided, as API and implementation packages are provided by the CQ runtime.

You can also have a look at a Sling sample like Slingbucks which will log to that error.log if installed on a default CQ instance.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.