如何避免 java 项目上不需要的日志消息?
在我的 java 项目中,我在 messages.properties
文件上外部化了一堆字符串。 在其各自的 Messages.java 文件中,我具有相同数量的公共静态字符串类型属性,因此我可以访问这些外部化文本。
然后,我实现了一个名为 getString
的方法,该方法接收常量的名称作为其参数并返回所需的文本。 这样,就不需要在 Messages.java
文件中声明所有公共静态字符串类型属性。
但完成此操作后,我的日志中充满了“NLS未使用的消息”消息。
您知道是否有办法阻止记录这些警告消息?
提前致谢。
On my java project, I have a bunch of strings externalized on a messages.properties
file. On its respective Messages.java
file I had the same number of public static String-typed attributes, so I could access those externalized texts.
Then, I implemented a method called getString
, which receives the name of the constant as its argument and returns the wanted text. This way, there is no need to declare all the public static Strings-typed attributes inside the Messages.java
file.
But after doing this my log became filled with "NLS unused message" messages.
Do you know if there's a way to prevent those warning messages to be logged?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
Messages
类 - 听起来它扩展了org.eclipse.osgi.util.NLS
。如果是这种情况,它的设计目的是满足以下要求:
即,NLS 使用 messages.properties 中找到的
staticVariable
的值填充Message.staticVariable
的值。警告日志记录提供有关
Messages.java
和messages.properties
文件之间不匹配的信息。您的 getString() 方法听起来好像没有使用 NLS 的任何优点,因此正如其他人所建议的那样,您最好使用 ResourceBundle。
Your
Messages
class - it sounds like it extendsorg.eclipse.osgi.util.NLS
.If this is the case, it is designed to fill the requirements:
i.e. NLS populates the value of the
Message.staticVariable
with the value of thestaticVariable
found in messages.properties.The warning logging provides information about a mismatch between the
Messages.java
and themessages.properties
file.Your
getString()
method sounds like it does not use any of the advantages of NLS, so as others have suggested, you may be better off using a ResourceBundle.Messages 听起来像是您编写的类,因为我在 JDK 6 javadocs 中没有看到它。
听起来您已经尝试重新发明 java.util.ResourceBundle。 我建议改用它并放弃你的课程。 它将具有正确处理 I18N 的额外优势。
我认为对类中的公共静态消息密钥进行硬编码没有任何价值。 这只是您必须维护的另一件事。 如果我理解你在做什么,我会扔掉你的消息并使用 ResourceBundle 代替。
Messages sounds like a class you wrote, because I don't see it in my JDK 6 javadocs.
It sounds like you've tried to reinvent java.util.ResourceBundle. I'd recommend using that instead and ditching your class. It'll have the added advantage of handling I18N properly.
I don't see any value in hard-coding the public static message keys in the class. It's just another thing you'll have to maintain. If I understand what you're doing properly, I'd throw away your Messages and use ResourceBundle instead.
duffymo,作为 jamesh说,
Messages
是我写的一个类,它扩展了org.eclipse.osgi.util.NLS
。 它有一个私有静态属性,其类型是...ResourceBundle!jamesh,感谢您详细介绍 NLS 的工作方式。
根据您的回答,我从项目中删除了
Messages
类,并在需要使用外部化字符串的类上添加了 ResourceBundle 类型的属性。 另外,我这样做的方式是不需要更改访问外部化字符串的行。我们项目上的文件数量已经减少,代码保持像以前一样干净,并且不再有日志警告。
感谢你们。 你摇滚。
duffymo, as jamesh said,
Messages
is a class I wrote, and it extendsorg.eclipse.osgi.util.NLS
. It has a private static attribute, and its type is... ResourceBundle!jamesh, thanks for detailing the way NLS works.
Based on your answers I removed my
Messages
class from my project and added a ResourceBundle-typed attribute on the classes that need to use the externalized strings. Plus, I did it in a way that the lines accessing the externalized strings did not need to be changed.The number of files on our project has been reduced, the code was kept as clean as before and there are no more log warnings.
Thank you, guys. You rock.