Can we achieve logging for AEM servlet methods?

Avatar

Avatar
Validate 1
Level 1
AnmolBhardwaj1808
Level 1

Likes

0 likes

Total Posts

13 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Establish
View profile

Avatar
Validate 1
Level 1
AnmolBhardwaj1808
Level 1

Likes

0 likes

Total Posts

13 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Establish
View profile
AnmolBhardwaj1808
Level 1

01-07-2021

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

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Boost 500
MVP
Vijayalakshmi_S
MVP

Likes

573 likes

Total Posts

728 posts

Correct reply

240 solutions
Top badges earned
Boost 500
Give Back 50
Give Back 5
Ignite 10
Ignite 5
View profile

Avatar
Boost 500
MVP
Vijayalakshmi_S
MVP

Likes

573 likes

Total Posts

728 posts

Correct reply

240 solutions
Top badges earned
Boost 500
Give Back 50
Give Back 5
Ignite 10
Ignite 5
View profile
Vijayalakshmi_S
MVP

06-07-2021

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

 

Answers (3)

Answers (3)

Avatar

Avatar
Boost 100
Level 6
Dipti_Chauhan
Level 6

Likes

114 likes

Total Posts

136 posts

Correct reply

40 solutions
Top badges earned
Boost 100
Give Back 10
Affirm 25
Boost 50
Boost 25
View profile

Avatar
Boost 100
Level 6
Dipti_Chauhan
Level 6

Likes

114 likes

Total Posts

136 posts

Correct reply

40 solutions
Top badges earned
Boost 100
Give Back 10
Affirm 25
Boost 50
Boost 25
View profile
Dipti_Chauhan
Level 6

01-07-2021

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

Avatar

Avatar
Affirm 25
Level 5
Bimmi_Soi
Level 5

Likes

73 likes

Total Posts

81 posts

Correct reply

26 solutions
Top badges earned
Affirm 25
Contributor
Applaud 5
Boost 50
Ignite 1
View profile

Avatar
Affirm 25
Level 5
Bimmi_Soi
Level 5

Likes

73 likes

Total Posts

81 posts

Correct reply

26 solutions
Top badges earned
Affirm 25
Contributor
Applaud 5
Boost 50
Ignite 1
View profile
Bimmi_Soi
Level 5

01-07-2021

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 

Avatar

Avatar
Boost 5
Level 7
Ritesh_M
Level 7

Likes

179 likes

Total Posts

142 posts

Correct reply

44 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 1
View profile

Avatar
Boost 5
Level 7
Ritesh_M
Level 7

Likes

179 likes

Total Posts

142 posts

Correct reply

44 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 1
View profile
Ritesh_M
Level 7

01-07-2021

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?