Scenario : I have an application where we noticed some unnecessary logging due to which the memory usage on that server goes upto 90% which is not desirable at all and a concern for the prod support . The culprit is a class where I have bunch of logging statements such as log.info and in the prod environment I don't have access to this class but I have access to the spring config xml. Can I apply an aspect to the class/in particular a method to stop executing the log.info statements?
-
4Why not just reconfigure the logging for that class?! In any case, Spring AOP won't allow skipping specific lines. AspectJ itself might, or you could simply manipulate the byte code of the compiled class yourself through a variety of means. IMO configuring your logging right is the best approach, though. – Dave Newton Jun 10 '13 at 13:56
-
@DaveNewton Although... configuring the logging may not help the performance if the log statements are not encapsulated in a `if (log.isInfoEnable()) {...}` statements and there is a cost involved in calculating the output. – Duncan Jones Jun 10 '13 at 14:13
-
@Dave Newton, I agree with you for reconfiguring the logging levels but at this point of time I need a quick solution from my spring config as I can not release immediately to prod. I have access to just replace a spring config file so I am thinking if I can take advantage of AOP. – GKP Jun 10 '13 at 16:27
-
1You can replace a Spring config file, which could fundamentally alter how the application runs, but can't replace a logging configuration? That's nuts. AOP is far more intrusive and dangerous than changing log levels. – Dave Newton Jun 10 '13 at 16:28
1 Answers
I agree to the comments right below your question: Just reconfigure logging.
Technically speaking, with Spring AOP you are limited to intercepting calls to or executions of Spring bean methods. So unless your logging calls are not encapsulated in Spring beans, Spring AOP will not be useful.
With AspectJ though, you could apply LTW (load-time weaving) to your logging library and block execution of logging methods called by a specific control flow. You could also do the same by weaving your application code and then block calls to logging methods from within a specific control flow.
Disclaimer: While this is easily implemented aspect-wise, I have no idea if your production admins will permit you to use AspectJ on the target system. Even if you compile the aspects into your own production code, at least you will need the AspectJ runtime (aspectjrt.jar) on the application's classpath.

- 63,017
- 15
- 111
- 202