vendredi 8 janvier 2021

Logging in each method vs Logging with Aspect?

Whenever I write an api or program with spring boot, my logging strategy is usually like below;

@Service
public class ShortLinkServiceImpl implements ShortLinkService {
   
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public String getByHashCode(String hashCode)
            throws HashCodeExpiredException, LinkNotFoundException {
          log.info("getByHashCode -> hashCode: {}", hashCode);
         ...
         ...
         //if necessary
         log.info("getByHashCode -> result: {}", result);
    }
}

However adding log.info into each method in my api does not look like a good approach (at least for me).

Is there any way to write more clean logging strategy, I am thinking of writing aspect for each class that I want to log and by using before and after joinPoint to log input and output of the methods.

To sum up, I would like to hear more about this topic whether there is a more convenient and clean way or not.

Aucun commentaire:

Enregistrer un commentaire