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(...);
}
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hi @Anmol_Bhardwaj,
From the code perspective in Servlet class,
From Config standpoint,
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
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?
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.
Views
Replies
Total Likes
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);
Views
Replies
Total Likes
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());
Views
Replies
Total Likes
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
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
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(...)
}
Views
Replies
Total Likes
Views
Replies
Total Likes
Adding screenshot for Logging logger configuration
Views
Replies
Total Likes
Hi @Anmol_Bhardwaj,
From the code perspective in Servlet class,
From Config standpoint,
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