14.3.4. 在 CLI 里配置文件日志处理程序

您可以在 CLI 里添加、删除和编辑文件日志处理程序。
配置文件日志处理程序的主要任务是:
  • 添加新的文件处理程序。
  • 显示文件日志处理程序的配置
  • 设置处理程序的日志级别。
  • 设置处理程序的附加行为。
  • 设置处理程序是否使用自动冲刷。
  • 设置用于处理程序输出的编码。
  • 指定日志处理程序将写入的文件。
  • 设置用于处理程序输出的格式器。
  • 删除文件日志处理程序。

重要

当在日志配置集里配置日志处理程序时,配置路径的根目录是 /subsystem=logging/logging-profile=NAME/ 而不是/subsystem=logging/
添加文件处理程序
请使用 add 命令和下列语法。用写入的日志文件的名称替换 PATH。用文件所在的目录的名称替换 DIRDIR 的值可以是一个路径变量。
 /subsystem=logging/file-handler=HANDLER:add(file={"path"=>"PATH", "relative-to"=>"DIR"}) 

例 14.20. 添加文件处理程序

[standalone@localhost:9999 /] /subsystem=logging/file-handler=accounts_log:add(file={"path"=>"accounts.log", "relative-to"=>"jboss.server.log.dir"})
{"outcome" => "success"}
[standalone@localhost:9999 /]
显示文件日志处理程序的配置
请使用 read-resource 命令和下列语法。用文件日志处理程序的名称替换 HANDLER
 /subsystem=logging/file-handler=HANDLER:read-resource 

例 14.21. 使用 read-resource 操作

[standalone@localhost:9999 /] /subsystem=logging/file-handler=accounts_log:read-resource
{
    "outcome" => "success",
    "result" => {
        "append" => true,
        "autoflush" => true,
        "encoding" => undefined,
        "file" => {
            "path" => "accounts.log",
            "relative-to" => "jboss.server.log.dir"
        },
        "filter" => undefined,
        "formatter" => "%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n",
        "level" => undefined
    }
}
设置日志级别
请使用 write-attribute 命令和下列语法。用文件日志处理程序的名称替换 HANDLER,并用日志级别替换 LOG_LEVEL_VALUE
 /subsystem=logging/file-handler=HANDLER:write-attribute(name="level", value="LOG_LEVEL_VALUE") 

例 14.22. 改变日志级别

/subsystem=logging/file-handler=accounts_log:write-attribute(name="level", value="DEBUG")
{"outcome" => "success"}
[standalone@localhost:9999 /]
设置附加行为
请使用 write-attribute 命令和下列语法。用文件日志处理程序的名称替换 HANDLER。如果要求每次启动服务器时都创建新的日志文件,则用 false 替换 BOOLEAN。如果应用服务器应该继续使用相同的文件,则请用 true 替换 BOOLEAN
 /subsystem=logging/file-handler=HANDLER:write-attribute(name="append", value="BOOLEAN") 

例 14.23. 修改附加的属性

[standalone@localhost:9999 /] /subsystem=logging/file-handler=accounts_log:write-attribute(name="append", value="true")
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
[standalone@localhost:9999 /]
重启 JBoss EAP 6 服务器以使修改生效。
设置自动冲刷
请使用 write-attribute 命令和下列语法。用文件日志处理程序的名称替换 HANDLER。如果处理程序立即写入到输出,则用 true 替换 BOOLEAN
 /subsystem=logging/file-handler=HANDLER:write-attribute(name="autoflush", value="BOOLEAN") 

例 14.24. 设置自动冲刷属性

[standalone@localhost:9999 /] /subsystem=logging/file-handler=accounts_log:write-attribute(name="autoflush", value="false")
{
    "outcome" => "success",
    "response-headers" => {"process-state" => "reload-required"}
}
[standalone@localhost:9999 /]
重启 JBoss EAP 6 服务器以使修改生效。
设置编码
请使用 write-attribute 命令和下列语法。用文件日志处理程序的名称替换 HANDLER,并用所需的字符编码系统替换 ENCODING
 /subsystem=logging/file-handler=HANDLER:write-attribute(name="encoding", value="ENCODING") 

例 14.25. 设置编码

[standalone@localhost:9999 /] /subsystem=logging/file-handler=accounts_log:write-attribute(name="encoding", value="utf-8")     
{"outcome" => "success"}
指定日志处理程序将写入的文件
请使用 write-attribute 命令和下列语法。用写入的日志文件的名称替换 PATH。用文件所在的目录的名称替换 DIRDIR 的值可以是一个路径变量。
 /subsystem=logging/file-handler=HANDLER:write-attribute(name="file", value={"path"=>"PATH", "relative-to"=>"DIR"}) 

例 14.26. 指定日志处理程序将写入的文件

[standalone@localhost:9999 /] /subsystem=logging/file-handler=accounts_log:write-attribute(name="file", value={"path"=>"accounts-debug.log", "relative-to"=>"jboss.server.log.dir"})
{"outcome" => "success"}
[standalone@localhost:9999 /]
设置格式器
请使用 write-attribute 命令和下列语法。用文件日志处理程序的名称替换 HANDLER,并用格式器字符串替换 FORMAT
 /subsystem=logging/file-handler=HANDLER:write-attribute(name="formatter", value="FORMAT") 

例 14.27. 设置格式器

[standalone@localhost:9999 /] /subsystem=logging/file-handler=accounts-log:write-attribute(name="formatter", value="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n")
{"outcome" => "success"}
[standalone@localhost:9999 /]
删除文件日志处理程序
请使用 remove 命令和下列语法。用要删除的日志处理程序的名称替换 HANDLER
 /subsystem=logging/file-handler=HANDLER:remove 

例 14.28. 删除文件日志处理程序

[standalone@localhost:9999 /] /subsystem=logging/file-handler=accounts_log:remove
{"outcome" => "success"}
[standalone@localhost:9999 /]
日志处理程序只有没有被本地类别或异步日志处理程序引用时才可以被删除。