- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图以 SimpleFormatter 格式限制源参数的长度,以便在 Tomcat 8 中使用。
我读过 SimpleFormatter doc和 Formatter syntax doc虽然我不会假装我已经理解了第二个,但在参数后面跟一个数字应该会限制它的长度。
但它不在我的测试中:
输出的行java.util.logging.SimpleFormatter.format = %4$s %n
和
java.util.logging.SimpleFormatter.format =%4$1s %n
无法区分。
我错过了什么吗?
最佳答案
I'm trying to limit the length of the source parameter in a SimpleFormatter format, to use in Tomcat 8.
由于您只需要关卡的第一个字符,因此语法为 %4$.1s
。
根据 java.util.Formatter JavaDocs :
The format specifiers for general, character, and numeric types have the following syntax:
%[argument_index$][flags][width][.precision]conversion
The optional width is a positive decimal integer indicating the minimum number of characters to be written to the output.
The optional precision is a non-negative decimal integer usually used to restrict the number of characters.
这意味着使用点字符指定精度。由于您不关心最大值如果最小值为零,那么您必须从模式中省略它。
这是一个构建模式的示例测试用例:
public static void main(String[] args) {
//This really should be set as a command argument but, it works.
//No min and max of seven chars of level.
//System.setProperty("java.util.logging.SimpleFormatter.format", "%4$.7s %n");
//Min and max of seven chars of level (right justified).
//System.setProperty("java.util.logging.SimpleFormatter.format", "%4$7.7s %n");
//Min and max of seven chars of level (left justified).
//System.setProperty("java.util.logging.SimpleFormatter.format", "%4$-7.7s %n");
//No min with max of one chars of level.
System.setProperty("java.util.logging.SimpleFormatter.format", "%4$.1s %n");
LogRecord r = new LogRecord(Level.SEVERE, "Message");
r.setLoggerName("logger");
r.setSourceClassName("class");
r.setSourceMethodName("method");
System.out.println(new SimpleFormatter().format(r));
}
JavaDocs 还声明:
For character, integral, and date/time argument types and the percent and line separator conversions, the precision is not applicable; if a precision is provided, an exception will be thrown.
文档实际上应该说“参数类别”,而不是“参数类型”。 “s”和“S”在参数类别表中被视为“一般”而不是“字符”。这就是您可以使用点精度的原因。
关于java - 使用 SimpleFormatter 限制字符串长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35745714/
我在 GlassFish 4 上使用 java.util.logging。 我正在定义我自己的类,通过定义系统属性来初始化 LogManager:-Djava.util.logging.config.
我正在向我的程序添加一个记录器,在网上冲浪时,我向我的代码添加一个非常简单的记录器,具有基本的两行定义: private static final Logger LOG = Logger.getLog
我试图以 SimpleFormatter 格式限制源参数的长度,以便在 Tomcat 8 中使用。 我读过 SimpleFormatter doc和 Formatter syntax doc虽然我不会
我正在使用 SimpleFormatter 编写日志文件。这些是用 XML 而不是纯文本编写的。我找到的将其写为纯文本的答案都只说使用简单的格式化程序(例如 https://stackoverflow
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我正在使用 JUL 进行日志记录,我有一个属性文件。我想在我的日志文件中以毫秒为单位记录日期时间。 这是我的属性(property): java.util.logging.SimpleFormatte
我想改变我的日志的外观(java.util.logging.Logger),我发现了这个: System.setProperty("java.util.logging.SimpleFormatter.
我想答案可能会有所不同,具体取决于所使用的处理程序,但假设使用了 ConsoleHandler 并使用了默认的 SimpleFormatter。传递给 SimpleFormatter.format()
我在我的 Tomcat 安装上部署了几个 Web 应用程序,我在我的 context.xml 中使用 swallowOutput="true" 以实现不同的'per -webapp'-log-outp
我是一名优秀的程序员,十分优秀!