应该“静态最终记录器”以大写形式声明?

发布于 2024-08-04 12:39:03 字数 566 浏览 6 评论 0原文

在Java中,静态最终变量是常量,并且约定它们应该是大写的。然而,我发现大多数人都以小写形式声明记录器,这在 PMD 中被视为违规。

例如:

private static final Logger logger = Logger.getLogger(MyClass.class);

只需搜索 googleSO 表示“静态最终记录器”,您将亲眼看到这一点。

我们应该改用记录器吗?

In Java, static final variables are constants and the convention is that they should be in upper-case. However, I have seen that most people declare loggers in lower-case which comes up as a violation in PMD.

e.g:

private static final Logger logger = Logger.getLogger(MyClass.class);

Just search googleor SO for "static final logger" and you will see this for yourself.

Should we be using LOGGER instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

万水千山粽是情ミ 2024-08-11 12:39:03

记录器引用不是常量,而是最终引用,并且不应为大写。常量 VALUE 应为大写。

private static final Logger logger = Logger.getLogger(MyClass.class);

private static final double MY_CONSTANT = 0.0;

The logger reference is not a constant, but a final reference, and should NOT be in uppercase. A constant VALUE should be in uppercase.

private static final Logger logger = Logger.getLogger(MyClass.class);

private static final double MY_CONSTANT = 0.0;
听风吹 2024-08-11 12:39:03

为了给 crunchdog 的答案增加更多价值, Java 编码风格指南 在第 3.3 段字段命名中说明了这一点

用作常量的字段名称应全部大写,并用下划线分隔单词。以下被视为常量:

  1. 所有staticfinal基元类型(请记住,所有接口字段本质上都是staticfinal)。
  2. 所有 static final 对象引用类型后面都不会跟有“.”(点)。
  3. 所有后面不跟“[”(左方括号)的static final数组。

示例:

MIN_VALUE、MAX_BUFFER_SIZE、OPTIONS_FILE_NAME

按照此约定,logger 是第 2 点中所述的 static final 对象引用,但因为它后跟“." 每次使用它时,它都不能被视为常量,因此应该是小写。

To add more value to crunchdog's answer, The Java Coding Style Guide states this in paragraph 3.3 Field Naming

Names of fields being used as constants should be all upper-case, with underscores separating words. The following are considered to be constants:

  1. All static final primitive types (Remember that all interface fields are inherently static final).
  2. All static final object reference types that are never followed by "." (dot).
  3. All static final arrays that are never followed by "[" (opening square bracket).

Examples:

MIN_VALUE, MAX_BUFFER_SIZE, OPTIONS_FILE_NAME

Following this convention, logger is a static final object reference as stated in point 2, but because it is followed by "." everytime you use it, it can not be considered as a constant and thus should be lower case.

夏日落 2024-08-11 12:39:03

来自有效的java,第二版,

上一条规则的唯一例外涉及“常量字段”,
其名称应由一个或多个大写单词组成,并用分隔符分隔
下划线字符,例如 VALUES 或 NEGATIVE_INFINITY。 A
常量字段是一个静态最终字段,其值是不可变的。如果一个
static Final 字段具有原始类型或不可变引用类型
(第15项),那么它是一个常数场。例如,枚举常量
是常数场。 如果静态最终字段具有可变引用
类型,如果引用的对象是,它仍然可以是常量字段
不可变。


总之,constant == static final,加上如果它是引用(相对于简单类型),则具有不变性。

查看 slf4j 记录器,
http://www.slf4j.org/api/org/slf4j/Logger.html

它是不可变的。另一方面,JUL 记录器是可变的。 log4j 记录器也是可变的。所以正确地说,如果你使用的是 log4j 或 JUL,它应该是“logger”,如果你使用的是 slf4j,它应该是 LOGGER。

请注意,上面链接的 slf4j javadocs 页面有一个示例,其中使用“logger”,而不是“LOGGER”。

当然这些只是约定而不是规则。如果您碰巧正在使用 slf4j 并且您想使用“logger”,因为您已经习惯了其他框架的“logger”,或者如果它更容易键入,或者为了可读性,请继续。

From effective java, 2nd ed.,

The sole exception to the previous rule concerns “constant fields,”
whose names should consist of one or more uppercase words separated by
the underscore character, for example, VALUES or NEGATIVE_INFINITY. A
constant field is a static final field whose value is immutable
. If a
static final field has a primitive type or an immutable reference type
(Item 15), then it is a constant field. For example, enum constants
are constant fields. If a static final field has a mutable reference
type, it can still be a constant field if the referenced object is
immutable.

In summary, constant == static final, plus if it's a reference (vs. a simple type), immutability.

Looking at the slf4j logger,
http://www.slf4j.org/api/org/slf4j/Logger.html

It is immutable. On the other hand, the JUL logger is mutable. The log4j logger is also mutable. So to be correct, if you are using log4j or JUL, it should be "logger", and if you are using slf4j, it should be LOGGER.

Note that the slf4j javadocs page linked above has an example where they use "logger", not "LOGGER".

These are of course only conventions and not rules. If you happen to be using slf4j and you want to use "logger" because you are used to that from other frameworks, or if it is easier to type, or for readability, go ahead.

女中豪杰 2024-08-11 12:39:03

我喜欢 Google 的做法(Google Java 风格

每个常量都是静态最终字段,但并非所有静态最终字段都是常量。在选择常量情况之前,请考虑该字段是否真的感觉像一个常量。例如,如果该实例的任何可观察状态可以改变,那么它几乎肯定不是一个常量。仅仅打算永远不改变对象通常是不够的。

示例:

// Constants
static final int NUMBER = 5;
static final ImmutableList<String> NAMES = ImmutableList.of("Ed", "Ann");
static final Joiner COMMA_JOINER = Joiner.on(',');  // because Joiner is immutable
static final SomeMutableType[] EMPTY_ARRAY = {};
enum SomeEnum { ENUM_CONSTANT }

// Not constants
static String nonFinal = "non-final";
final String nonStatic = "non-static";
static final Set<String> mutableCollection = new HashSet<String>();
static final ImmutableSet<SomeMutableType> mutableElements = ImmutableSet.of(mutable);
static final Logger logger = Logger.getLogger(MyClass.getName());
static final String[] nonEmptyArray = {"these", "can", "change"};

I like Google's take on it (Google Java Style)

Every constant is a static final field, but not all static final fields are constants. Before choosing constant case, consider whether the field really feels like a constant. For example, if any of that instance's observable state can change, it is almost certainly not a constant. Merely intending to never mutate the object is generally not enough.

Examples:

// Constants
static final int NUMBER = 5;
static final ImmutableList<String> NAMES = ImmutableList.of("Ed", "Ann");
static final Joiner COMMA_JOINER = Joiner.on(',');  // because Joiner is immutable
static final SomeMutableType[] EMPTY_ARRAY = {};
enum SomeEnum { ENUM_CONSTANT }

// Not constants
static String nonFinal = "non-final";
final String nonStatic = "non-static";
static final Set<String> mutableCollection = new HashSet<String>();
static final ImmutableSet<SomeMutableType> mutableElements = ImmutableSet.of(mutable);
static final Logger logger = Logger.getLogger(MyClass.getName());
static final String[] nonEmptyArray = {"these", "can", "change"};
一场信仰旅途 2024-08-11 12:39:03

如果您使用自动化工具来检查您的编码标准并且它违反了所述标准,那么应该修复它或标准。如果您使用外部标准,请修复代码。

Sun Java 中的约定是公共静态常量大写。显然,记录器不是恒定的,而是代表一个可变的东西(否则就没有必要调用它的方法来希望发生某些事情);对于非常量的最终字段没有具体的标准。

If you are using an automated tool to check your coding standards and it violates said standards then it or the standards should be fixed. If you're using an external standard, fix the code.

The convention in Sun Java is uppercase for public static constants. Obviously a logger is not constant, but represents a mutable thing ( otherwise there would be no point calling methods on it in the hope that something will happen ); there's no specific standard for non-constant final fields.

情话已封尘 2024-08-11 12:39:03

我个人认为大写看起来很大。此外,由于它是一个与类行为没有直接关系的类,因此我认为使用 logger 而不是 LOGGER 没有什么大问题。但如果您打算严格迂腐,那么请使用LOGGER

I personally think it looks really big in upper-case. Moreover, since it's a class that it's not directly related to the class behaviour, I don't see a major problem in using logger instead of LOGGER. But if you are going to be strictly pedantic, then use LOGGER.

活泼老夫 2024-08-11 12:39:03

如果你用谷歌搜索这个,你可能会发现在某些情况下,记录器没有定义为静态最终的。添加一些快速复制粘贴,这可能会解释它。

我们在所有代码中使用 LOGGER,这符合我们的命名约定(我们的 CheckStyle 对此很满意)。


我们甚至更进一步,利用 Eclipse 中严格的命名约定。
我们创建一个新类,其代码模板为:

    // private static final Logger LOGGER = Logger.getLogger(${enclosing_type}.class);

记录器被注释掉,因为最初我们不需要它。但如果我们稍后需要它,我们只需取消注释即可。

然后在代码中,我们使用期望此记录器存在的代码模板。
try-catch 模板的示例:

    try {
      ${cursor} or some other template
    } catch (Exception t) {
      LOGGER.error("${methodName} ${method parameters}", t);
    }

我们还有一些使用它的模板。

严格的约定使我们能够提高代码模板的工作效率和一致性

If you google this, you might find that in some cases, the loggers are not defined as static final. Add some quick copy-n-paste to this, and this might explain it.

We use LOGGER in all our code, and this corresponds to our naming convention (and our CheckStyle is happy with it).


We even go further, taking advantage of the strict naming convention in Eclipse.
We create a new class with a code template of :

    // private static final Logger LOGGER = Logger.getLogger(${enclosing_type}.class);

The logger is commented out, as initially we don't need it. But should we need it later, we just uncomment it.

Then in the code, we use code templates that expect this logger to be present.
Example with the try-catch template:

    try {
      ${cursor} or some other template
    } catch (Exception t) {
      LOGGER.error("${methodName} ${method parameters}", t);
    }

We have a few more templates that use it.

The strict convention allow us to be more productive and coherent with code templates.

绝影如岚 2024-08-11 12:39:03

通常常量都是大写的。

然而,记录器不应该是静态的,而是在使用 slf4j 外观时查找包含类的每个“新”。这避免了一些令人讨厌的类加载器问题,尤其是 Web 容器中的问题,而且它允许记录器框架根据调用上下文执行特殊的操作。

Usually constants are in uppercase.

Loggers, however, should not be static but looked up for every "new" of the containing class if using the slf4j facade. This avoids some nasty classloader issues in notably web containers, plus it allows the logger framework to do special stuff depending on the invocation context.

温柔少女心 2024-08-11 12:39:03

不要忘记 PMD 会尊重

// NOPMD

其中的评论。这将导致 PMD 跳过检查中的行,这将允许您选择您想要的任何样式。

Don't forget that PMD will respect a comment with

// NOPMD

in it. This will cause PMD to skip the line from its checks, this will allow you to choose whichever style you want.

说谎友 2024-08-11 12:39:03

我更喜欢“logger”,即小写。原因不在于它是常量还是不是常量(可变或不可变)。如果我们使用这种推理,如果我们更改日志框架(或者如果框架更改了记录器的可变性),则必须重命名变量。

对我来说,其他原因更重要。

  1. 记录器是类中的影子对象,不应该非常突出,因为它不实现主要逻辑。如果我们使用“LOGGER”,那么它会在代码中引起太多关注。

  2. 有时记录器是在实例级别声明的(即不是静态的),甚至作为依赖项注入。如果我决定更改获取记录器的方式,我不想更改我的代码。代码稳定性。这种(在许多情况下是假设的)变化是我更喜欢小写的另一个原因。

I prefer 'logger', i.e. the lower case. The reason is not that it's a constant or not a constant (mutable or immutable). If we'd use that reasoning, we'd have to rename the variable if we change the logging framework (or if the framework changes the mutability of loggers).

For me, other reasons are more important.

  1. A logger is a shadow object in the class and should not be very prominent as it does not implement the main logic. If we use 'LOGGER', it's an eye catcher in the code that attracts too much attention.

  2. Sometimes loggers are declared at instance level (i.e. not as static), and even are injected as a dependency. I wouldn't like to change my code if I decide to change the way I obtain the logger. The code stability wrt. this (hypothetical in many cases) change is the other reason why I prefer the lower case.

音盲 2024-08-11 12:39:03

如果您的编码标准(如果有的话)说它应该是大写的,那么是的。

我看不出任何一种方式有任何严格的理由。我认为这完全取决于您的个人喜好。您公司的编码标准。

顺便说一句:我更喜欢“记录者”;-)

If your coding standards - if you have any - say that it should be uppercase then yes.

I don't see any stringent reason for one way or the other. I think it totally depends on your personal likes resp. your company coding standards.

BTW: I prefer "LOGGER" ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文