我在属性文件中设置Threshold
时遇到一些问题。
我创建了两个文件:
log4j-server.properties
log4j.rootLogger=ERROR, CA, FA
#
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=[%d] [%t] [%m]%n
log4j.appender.CA.Threshold=DEBUG
#
log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.File=result.log
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=[%d] [%t] [%m]%n
log4j.appender.FA.Threshold=DEBUG
和log4j.properties
log4j.rootLogger=DEBUG, server
log4j.appender.server=org.apache.log4j.net.SocketAppender
log4j.appender.server.Port=4712
log4j.appender.server.RemoteHost=localhost
log4j.appender.server.ReconnectionDelay=10000
我得到了日志:
INFO [2012-08-02 11:14:27,490] [main] [Listening on port 4712]
INFO [2012-08-02 11:14:27,490] [main] [Waiting to accept a new client.]
INFO [2012-08-02 11:14:37,669] [main] [Connected to client at /127.0.0.1]
INFO [2012-08-02 11:14:37,669] [main] [Starting new socket node.]
INFO [2012-08-02 11:14:37,669] [main] [Waiting to accept a new client.]
INFO [2012-08-02 11:14:41,385] [main] [Connected to client at /127.0.0.1]
INFO [2012-08-02 11:14:41,385] [main] [Starting new socket node.]
INFO [2012-08-02 11:14:41,401] [main] [Waiting to accept a new client.]
INFO [2012-08-02 11:14:41,385] [request1343898881385] [public static void hello.RMIClient.main(java.lang.String[])]
INFO [2012-08-02 11:14:41,510] [request1343898881385] [public java.lang.String hello.Hello.greeting(java.lang.String)]
INFO [2012-08-02 11:14:41,510] [request1343898881385] [public java.lang.String hello.Hello.greeting(java.lang.String)]
INFO [2012-08-02 11:14:41,510] [request1343898881385] [public static void hello.RMIClient.main(java.lang.String[])]
INFO [2012-08-02 11:14:41,526] [SimpleSocketServer-4712] [Caught java.net.SocketException closing conneciton.]
INFO [2012-08-02 11:14:42,228] [main] [Connected to client at /127.0.0.1]
INFO [2012-08-02 11:14:42,228] [main] [Starting new socket node.]
INFO [2012-08-02 11:14:42,228] [main] [Waiting to accept a new client.]
INFO [2012-08-02 11:14:42,228] [request1343898882228] [public static void hello.RMIClient.main(java.lang.String[])]
INFO [2012-08-02 11:14:42,353] [request1343898882228] [public java.lang.String hello.Hello.greeting(java.lang.String)]
INFO [2012-08-02 11:14:42,353] [request1343898882228] [public java.lang.String hello.Hello.greeting(java.lang.String)]
INFO [2012-08-02 11:14:42,353] [request1343898882228] [public static void hello.RMIClient.main(java.lang.String[])]
INFO [2012-08-02 11:14:42,353] [SimpleSocketServer-4712] [Caught java.net.SocketException closing conneciton.]
虽然我需要仅由我的程序生成的日志,而不是由套接字服务器本身生成的消息
INFO [2012-08-02 11:14:41,385] [request1343898881385] [public static void hello.RMIClient.main(java.lang.String[])]
INFO [2012-08-02 11:14:41,510] [request1343898881385] [public java.lang.String hello.Hello.greeting(java.lang.String)]
INFO [2012-08-02 11:14:41,510] [request1343898881385] [public java.lang.String hello.Hello.greeting(java.lang.String)]
INFO [2012-08-02 11:14:41,510] [request1343898881385] [public static void hello.RMIClient.main(java.lang.String[])]
INFO [2012-08-02 11:14:42,228] [request1343898882228] [public static void hello.RMIClient.main(java.lang.String[])]
INFO [2012-08-02 11:14:42,353] [request1343898882228] [public java.lang.String hello.Hello.greeting(java.lang.String)]
INFO [2012-08-02 11:14:42,353] [request1343898882228] [public java.lang.String hello.Hello.greeting(java.lang.String)]
INFO [2012-08-02 11:14:42,353] [request1343898882228] [public static void hello.RMIClient.main(java.lang.String[])]
我几乎尝试了所有配置,但我找不到解决方案。
修改
我添加了-Dlog4j.debug=true
行
我得到了
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@35ce36.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@35ce36 class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@35ce36.
log4j: Trying to find [log4j.properties] using sun.misc.Launcher$AppClassLoader@35ce36 class loader.
log4j: Trying to find [log4j.properties] using ClassLoader.getSystemResource().
log4j: Could not find resource: [null].
log4j: Parsing for [root] with value=[DEBUG, CA, FA].
log4j: Level token is [DEBUG].
log4j: Category root set to DEBUG
log4j: Parsing appender named "CA".
log4j: Parsing layout options for "CA".
log4j: Setting property [conversionPattern] to [%p [%d] [%t] [%m]%n].
log4j: End of parsing for "CA".
log4j: Setting property [threshold] to [DEBUG].
log4j: Parsed "CA" options.
log4j: Parsing appender named "FA".
log4j: Parsing layout options for "FA".
log4j: Setting property [conversionPattern] to [[%d] [%t] [%m]%n].
log4j: End of parsing for "FA".
log4j: Setting property [threshold] to [DEBUG].
log4j: Setting property [file] to [result.log].
log4j: setFile called: result.log, true
log4j: setFile ended
log4j: Parsed "FA" options.
log4j: Finished configuring.
编辑:更改转化模式后|我得到了
INFO [2012-08-02 12:13:11,278] [main] org.apache.log4j.net.SimpleSocketServer Listening on port 4712
INFO [2012-08-02 12:13:11,278] [main] org.apache.log4j.net.SimpleSocketServer Waiting to accept a new client.
INFO [2012-08-02 12:13:22,125] [main] org.apache.log4j.net.SimpleSocketServer Connected to client at /127.0.0.1
INFO [2012-08-02 12:13:22,125] [main] org.apache.log4j.net.SimpleSocketServer Starting new socket node.
INFO [2012-08-02 12:13:22,125] [main] org.apache.log4j.net.SimpleSocketServer Waiting to accept a new client.
INFO [2012-08-02 12:13:25,664] [main] org.apache.log4j.net.SimpleSocketServer Connected to client at /127.0.0.1
INFO [2012-08-02 12:13:25,664] [main] org.apache.log4j.net.SimpleSocketServer Starting new socket node.
INFO [2012-08-02 12:13:25,664] [main] org.apache.log4j.net.SimpleSocketServer Waiting to accept a new client.
INFO [2012-08-02 12:13:25,664] [request1343902405664] aspects.ReportingAspect public static void hello.RMIClient.main(java.lang.String[])
INFO [2012-08-02 12:13:25,789] [request1343902405664] aspects.ReportingAspect public java.lang.String hello.Hello.greeting(java.lang.String)
INFO [2012-08-02 12:13:25,789] [request1343902405664] aspects.ReportingAspect public java.lang.String hello.Hello.greeting(java.lang.String)
INFO [2012-08-02 12:13:25,789] [request1343902405664] aspects.ReportingAspect public static void hello.RMIClient.main(java.lang.String[])
INFO [2012-08-02 12:13:25,789] [SimpleSocketServer-4712] org.apache.log4j.net.SocketNode Caught java.net.SocketException closing conneciton.
答案 0 :(得分:4)
您需要了解log4j如何应用阈值:
首先应用记录器的阈值。如果记录器没有记录器,则应用父记录器的阈值。如果消息无法传递任何这些消息,那么任何追加者都不会看到它。
在下一步中,所有appender都会看到该消息,他们将再次使用其阈值对其进行过滤。
因此,如果任何记录器的阈值高于appender的阈值,则appender将永远不会看到该消息。
你怎么解决这个问题?您可以使用“更接近”消息源的记录器。如果您想从com.something.x.y.z
下面的班级登录,您可以为com
定义一个记录器并为其指定一个阈值+一个追加者。
它将在根记录器之前看到消息,因此您可以使用它来重定向部分消息。
另请注意,使用包/类名称只是一种方便。记录器名称可以是任何东西。因此,您可以定义一个名为RMI
或network
的记录器并使用它。由于它不是通常的记录器层次结构的一部分,因此只有特定的消息才能到达。
不要忘记根据需要将additive
设置为false
,以确保只处理一次邮件。
[编辑] 我的猜测是读取了错误的配置。将-Dlog4j.debug=true
添加到命令行以查看log4j读取的配置文件及其配置方式。
[EDIT2] 使用日志模式%-5p [%d] [%t] %c %m%n
查看哪个记录器创建了哪条消息。然后,您可以提高您不想要的阈值。