Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Can we achieve logging for AEM servlet methods?

AnmolBhardwaj1808
Level 2
Level 2

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
Vijayalakshmi_S
Correct answer by
Community Advisor
Community Advisor

Hi @AnmolBhardwaj1808,

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

0 Replies
Ritesh_Mittal
Community Advisor
Community Advisor

Hi @AnmolBhardwaj1808 ,

 

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?

 

 

 

AnmolBhardwaj1808
Level 2
Level 2

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.

Ritesh_Mittal
Community Advisor
Community Advisor

Hi @AnmolBhardwaj1808 ,

 

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);

 

AnmolBhardwaj1808
Level 2
Level 2

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());

Bimmi_Soi
Community Advisor
Community Advisor

Hi @AnmolBhardwaj1808 ,

 

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 

Dipti_Chauhan
Community Advisor
Community Advisor

HI @AnmolBhardwaj1808 

  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

AnmolBhardwaj1808
Level 2
Level 2

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(...)

}

 

 

Dipti_Chauhan
Community Advisor
Community Advisor
Hi @AnmolBhardwaj1808 Can you share screen shot for Apache Sling Logging Logger configiuration?
Vijayalakshmi_S
Correct answer by
Community Advisor
Community Advisor

Hi @AnmolBhardwaj1808,

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