您如何减少Java日志记录样板代码? [英] How do you reduce Java logging boilerplate code?

查看:113
本文介绍了您如何减少Java日志记录样板代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个想要使用java.util.logging的类通常都需要声明一个这样的记录器:

Every class that wants to use java.util.logging generally needs to declare a logger like this:

public class MyClass {
    private static Logger _log = Logger.getLogger(MyClass.class.getName());
}

如何避免使用此MyClass.class.getName()样板代码?

How do you avoid this MyClass.class.getName() boilerplate code?

推荐答案

我在Eclipse中设置了一个模板,这样我只需要输入一部分声明,然后Eclipse将自动 - 为我完成剩下的工作。

I have a template set up in Eclipse so that I only have to type a portion of the declaration, and then Eclipse will auto-complete the rest for me.

${:import(org.apache.log4j.Logger)}
private final static Logger log = Logger.getLogger(${enclosing_type}.class);
${cursor}

所以,我只需输入记录器,点击 Ctrl + Space ,然后按输入,Eclipse填写其余部分对我来说并添加了导入声明。

So, I only have to type logger, hit Ctrl+Space, followed by Enter, and Eclipse fills in the rest for me and adds the import declaration as well.

这不会减少样板代码的数量,但至少会减少击键量。

This won't cut down on the amount of boilerplate code, but at least it cuts down on the amount of keystrokes.

这篇关于您如何减少Java日志记录样板代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆