Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SimpleFormatter限制字符串长度_Java_Logging_Java.util.logging - Fatal编程技术网

Java 使用SimpleFormatter限制字符串长度

Java 使用SimpleFormatter限制字符串长度,java,logging,java.util.logging,Java,Logging,Java.util.logging,我试图以SimpleFormatter格式限制源参数的长度,以便在Tomcat8中使用 我已经阅读了和,虽然我不会假装我已经理解了第二个的所有内容,但是在论点后面加上一个数字应该限制它的长度 但在我的测试中,它并没有出现:由 java.util.logging.SimpleFormatter.format=%4$s%n和 java.util.logging.SimpleFormatter.format=%4$1s%n 无法区分 我错过什么了吗 我试图以SimpleFormatter格式限制源参数

我试图以SimpleFormatter格式限制源参数的长度,以便在Tomcat8中使用

我已经阅读了和,虽然我不会假装我已经理解了第二个的所有内容,但是在论点后面加上一个数字应该限制它的长度

但在我的测试中,它并没有出现:由

java.util.logging.SimpleFormatter.format=%4$s%n

java.util.logging.SimpleFormatter.format=%4$1s%n

无法区分

我错过什么了吗

我试图以SimpleFormatter格式限制源参数的长度,以便在Tomcat8中使用

由于您只需要级别的第一个字符,因此语法为
%4$.1s

根据:

常规、字符和数字类型的格式说明符具有以下语法:

可选宽度是一个正十进制整数,表示写入输出的最小字符数

可选精度是一个非负十进制整数,通常用于限制字符数

这意味着使用点字符指定精度。既然你不关心最大值,如果最小值为零,那么你必须从模式中省略它

以下是构建模式的示例测试用例:

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还声明:

对于字符、整数和日期/时间参数类型以及百分比和行分隔符转换,精度不适用;如果提供了精度,将引发异常


文档应该真正地说“参数类别”,而不是“参数类型”。在参数类别表中,“s”和“s”被视为“一般”而不是“字符”。这就是为什么可以使用点精度。

太好了!谢谢正如我所说的,我不理解那个文件中的所有内容。。。羞耻
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));
}