Expand my Community achievements bar.

SOLVED

Can we achieve logging for AEM servlet methods?

Avatar

Community Advisor

Like we can log different results and entry points for OSGi services & components Can we achieve the same for servlets?

 

I know putting it simply in the doGet/doPost method won't work. Any suggestions?

 

Code:

  
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

... 

    @Override
    protected void doGet(final SlingHttpServletRequest req,
            final SlingHttpServletResponse resp) throws ServletException, IOException {
    ...  
        logger.debug(...);
    }
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Anmol_Bhardwaj,

From the code perspective in Servlet class,

  • cross check if you are using org.slf4j 
    • import org.slf4j.Logger;
    • import org.slf4j.LoggerFactory;
  • Log Level used in the code matters - debug/info which is related to the Logger config you created for your project. 

From Config standpoint,

  • if you are using project created using AEM project Maven archetype, you would have logger Factory config instance for your project (Core package name in general + log level + file name)
  • Given that you could see log messages for OSGi components/Services, I suggest to check the Servlet package family is part of the same and that is part of the Logger config and finally you are checking the respective file you are logging to. 

The config screenshot you have shared is OOTB entry for error.log file.

 

Highlighted in red is the factory config instance identifier for my project which logs (Information messages - log.info) all Java classes part of the Logger highlighted in green - com.aem.demoproject to the file named project-demoproject.log

Vijayalakshmi_S_0-1625585955775.png

 

View solution in original post

10 Replies

Avatar

Community Advisor

Hi @Anmol_Bhardwaj ,

 

We can put logs inside servlets also (which are nothing but Java class only), and you can put them anywhere inside method. I am not sure why you mentioned that putting inside doGet and doPost method it will not work because it will. Are you facing any issue while putting inside those methods?

 

 

 

Avatar

Community Advisor

Hi @Ritesh,

I tried putting logger inside my doGet method and was not able to see anything in the log files. I have used similar logger in OSGi components and it worked. I can see that the servlet is working properly but just the logs are not displayed in the log files.

I have checked the log levels and they are also matching.

Avatar

Community Advisor

Hi @Anmol_Bhardwaj ,

 

Are you sure your changed code is in sync with AEM server. Try debugging it though putting debug point and then load the servlet (or introduce some error in your servlet so that it will break, this way you will be sure your changes are reflecting).

Also, just make sure you are initializing the logger object with right class  name, for example-

 

private static final Logger log = LoggerFactory.getLogger(YourFileName.class);

 

Avatar

Community Advisor

Hi @Ritesh, I have checked that my servlet is in sync with the AEM server. Also I have checked and I am using the correct initialization of the logger.
private final Logger logger = LoggerFactory.getLogger(this.getClass());

Avatar

Employee Advisor

Hi @Anmol_Bhardwaj ,

 

A servlet is also a java class and we can put loggers in it too.

Can you explain more about what error/issue you are getting when adding loggers in servlet.

 

Thanks 

Avatar

Community Advisor

HI @Anmol_Bhardwaj 

  Logger will work in servlet also same as any other Java class

you can also validate your logger osgi configuration<Apache Sling Logging Logger> may be you have explicitly added your OSGI service java package and hence your servlet logs not getting printed on specific log file

Avatar

Community Advisor

Hi @Dipti_Chauhan, Thanks. I have checked and I can say that I have not added any OSGi service package explicitly.

This is the exact code I have been using

 

 private final Logger logger = LoggerFactory.getLogger(this.getClass()); 

...

 @Override
    protected void doGet(final SlingHttpServletRequest req,
            final SlingHttpServletResponse resp) throws ServletException, IOException {
...

 logger.debug(...)

}

 

 

Avatar

Community Advisor
Hi @Anmol_Bhardwaj Can you share screen shot for Apache Sling Logging Logger configiuration?

Avatar

Community Advisor

Adding screenshot for Logging logger configuration
Screenshot 2021-07-02 at 15-55-15 Adobe Experience Manager Web Console - Configuration.png

Avatar

Correct answer by
Community Advisor

Hi @Anmol_Bhardwaj,

From the code perspective in Servlet class,

  • cross check if you are using org.slf4j 
    • import org.slf4j.Logger;
    • import org.slf4j.LoggerFactory;
  • Log Level used in the code matters - debug/info which is related to the Logger config you created for your project. 

From Config standpoint,

  • if you are using project created using AEM project Maven archetype, you would have logger Factory config instance for your project (Core package name in general + log level + file name)
  • Given that you could see log messages for OSGi components/Services, I suggest to check the Servlet package family is part of the same and that is part of the Logger config and finally you are checking the respective file you are logging to. 

The config screenshot you have shared is OOTB entry for error.log file.

 

Highlighted in red is the factory config instance identifier for my project which logs (Information messages - log.info) all Java classes part of the Logger highlighted in green - com.aem.demoproject to the file named project-demoproject.log

Vijayalakshmi_S_0-1625585955775.png