Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4073450
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:01:25+00:00 2026-05-20T17:01:25+00:00

We are using Spring with slf4j and hibernate, I’m trying to figure out a

  • 0

We are using Spring with slf4j and hibernate, I’m trying to figure out a way to log exceptions and errors automatically (i.e without initiating an instance of the debugger in each class), so that it may catch any error or exception thrown and also get class and method name in the log,

I read a very short note about using aspects & interceptors for this, so could you provide me with some detailed way to implement this,

Regards,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:01:26+00:00Added an answer on May 20, 2026 at 5:01 pm

    an exception aspect could look like this:

    @Aspect
    public class ExceptionAspect {
    
      private static final Logger log = LoggerFactory.getLogger(ExceptionAspect.class);
    
      public Object handle(ProceedingJoinPoint pjp) throws Throwable {
         try {
           return pjp.proceed();
         } catch (Throwable t) {
           // so something with t: log, wrap, return default, ...
           log.warn("invocation of " + pjp.getSignature().toLongString() + " failed", t);
           // I hate logging and re-raising, but let's do it for the sake of this example
           throw t;
         }
      }
    }
    

    spring conf:

    <!-- log exceptions for any method call to any object in a package called 'svc' -->
    <bean class="org.example.aspects.ExceptionAspect" name="exceptionAspect" />
    <aop:config>
      <aop:aspect ref="exceptionAspect">
        <aop:around method="handle" pointcut="execution(* org.example..svc..*.*(..))" />
      </aop:aspect>
    </aop:config>
    

    EDIT:

    if you want the logger to log on behalf of the wrapped bean, you could of course do:

    LoggerFactory.getLogger(pjp.getTarget().getClass()).warn("damn!");
    

    or if you prefere the declaring class of this method rather than the actual (potentially proxied/auto-generated type):

    LoggerFactory.getLogger(pjp.getSignature().getDeclaringType()).warn("damn!");
    

    Honestly, I can’t estimate the performance implications of calling LoggerFactory.getLogger(..) every time. I’d assume that it shouldn’t be too bad, as exceptions are exceptional (i.e. rare) anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Spring 3.1 and Hibernate 4 in a console application (i'm trying some
When using spring security, specifically with @notation; what is the proper way to access
I'm using spring(3.0.4.RELEASE) and hibernate (3.4.0.GA) and deploying into a glassfish (web_3.1) container. I
I am using spring/hibernate. i have configured 5 datasources in applicationContext.xml file. All datasources
I'm trying to use Spring to inject a SLF4J logger into a class like
I am using intellij, spring mvc and tiles. At least I am trying :)
Using Spring MVC, is there any way to factorize the org.springframework.ui.Model , in order
I am using Spring MVC 3.05. I was wondering what the best way to
i am using spring annotaions with <task:annotation-driven/> @Scheduled(fixedDelay=100) public void shout(){ System.out.println(hello); } this
Using Spring 3.0.5, I am trying to return a list of strings in JSON

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.