问题我没有得到Gamebot.log
文件的任何输出,但我确实在控制台上得到了它,我不知道它为什么输出不输出到日志。请帮忙。
代码
Log4j配置文件
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG" name="GameBot">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%-5level] %logger{36} - %msg%n" />
</Console>
<File name="MyFile" fileName="Gamebot.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="DEBUG">
<AppenderRef ref="MyFile" />
</Root>
<Root level="INFO">
<AppenderRef ref="Console" />
</Root>
<Logger name="wh1spr.gamebot.MessageHandler" level="Debug" additivity="false">
<AppenderRef ref="Console" />
</Logger>
</Loggers>
</Configuration>
代码本身
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MessageHandler {
final static Logger logger = LoggerFactory.getLogger(MessageHandler.class);
...
@EventSubscriber
public onMessageReceived(MessageReceivedEvent event) {
...
logger.info("This should show up in both console and Gamebot.log")
...
}
...
}
输出
对于文件,什么都没有。
对于控制台:
记录机器人...
2017-05-30 17:54:54,666 main DEBUG Initializing configuration XmlConfiguration[location=/Users/NAME/Desktop/Workspace/DiscordBot/bin/log4j2.xml]
2017-05-30 17:54:54,675 main DEBUG Installed script engines
2017-05-30 17:54:55,164 main DEBUG Oracle Nashorn Version: 1.8.0_45, Language: ECMAScript, Threading: Not Thread Safe, Compile: true, Names: {nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript}
2017-05-30 17:54:55,164 main DEBUG PluginManager 'Core' found 112 plugins
2017-05-30 17:54:55,165 main DEBUG PluginManager 'Level' found 0 plugins
2017-05-30 17:54:55,170 main DEBUG PluginManager 'Lookup' found 13 plugins
2017-05-30 17:54:55,173 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2017-05-30 17:54:55,188 main DEBUG PluginManager 'TypeConverter' found 26 plugins
2017-05-30 17:54:55,201 main DEBUG PatternLayout$Builder(pattern="%d{HH:mm:ss.SSS} [%-5level] %logger{36} - %msg%n", PatternSelector=null, Configuration(GameBot), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2017-05-30 17:54:55,201 main DEBUG PluginManager 'Converter' found 41 plugins
2017-05-30 17:54:55,202 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
2017-05-30 17:54:55,210 main DEBUG ConsoleAppender$Builder(target="SYSTEM_OUT", follow="null", direct="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%d{HH:mm:ss.SSS} [%-5level] %logger{36} - %msg%n), name="Console", Configuration(GameBot), Filter=null)
2017-05-30 17:54:55,210 main DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false
2017-05-30 17:54:55,211 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2017-05-30 17:54:55,212 main DEBUG PatternLayout$Builder(pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n", PatternSelector=null, Configuration(GameBot), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2017-05-30 17:54:55,225 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.FileAppender].
2017-05-30 17:54:55,230 main DEBUG FileAppender$Builder(fileName="Gamebot.log", append="false", locking="null", advertise="null", advertiseUri="null", createOnDemand="null", bufferedIo="null", bufferSize="null", immediateFlush="false", ignoreExceptions="null", PatternLayout(%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n), name="MyFile", Configuration(GameBot), Filter=null)
2017-05-30 17:54:55,233 main DEBUG Starting FileManager Gamebot.log
2017-05-30 17:54:55,234 main DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
2017-05-30 17:54:55,235 main DEBUG createAppenders(={Console, MyFile})
2017-05-30 17:54:55,235 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2017-05-30 17:54:55,243 main DEBUG createAppenderRef(ref="MyFile", level="null", Filter=null)
2017-05-30 17:54:55,244 main DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
2017-05-30 17:54:55,245 main DEBUG createLogger(additivity="null", level="DEBUG", includeLocation="null", ={MyFile}, ={}, Configuration(GameBot), Filter=null)
2017-05-30 17:54:55,248 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2017-05-30 17:54:55,249 main DEBUG createAppenderRef(ref="Console", level="null", Filter=null)
2017-05-30 17:54:55,249 main DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
2017-05-30 17:54:55,250 main DEBUG createLogger(additivity="null", level="INFO", includeLocation="null", ={Console}, ={}, Configuration(GameBot), Filter=null)
2017-05-30 17:54:55,251 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2017-05-30 17:54:55,251 main DEBUG createAppenderRef(ref="Console", level="null", Filter=null)
2017-05-30 17:54:55,252 main DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
2017-05-30 17:54:55,256 main DEBUG createLogger(additivity="false", level="DEBUG", name="wh1spr.gamebot.MessageHandler", includeLocation="null", ={Console}, ={}, Configuration(GameBot), Filter=null)
2017-05-30 17:54:55,257 main DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
2017-05-30 17:54:55,258 main DEBUG createLoggers(={root, root, wh1spr.gamebot.MessageHandler})
2017-05-30 17:54:55,259 main DEBUG Configuration XmlConfiguration[location=/Users/NAME/Desktop/Workspace/DiscordBot/bin/log4j2.xml] initialized
2017-05-30 17:54:55,260 main DEBUG Starting configuration XmlConfiguration[location=/Users/NAME/Desktop/Workspace/DiscordBot/bin/log4j2.xml]
2017-05-30 17:54:55,260 main DEBUG Started configuration XmlConfiguration[location=/Users/NAME/Desktop/Workspace/DiscordBot/bin/log4j2.xml] OK.
2017-05-30 17:54:55,263 main DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false-1
2017-05-30 17:54:55,263 main DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false-1, all resources released: true
2017-05-30 17:54:55,263 main DEBUG Appender DefaultConsole-1 stopped with status true
2017-05-30 17:54:55,264 main DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@58d25a40 OK
2017-05-30 17:54:55,325 main DEBUG Registering MBean org.apache.logging.log4j2:type=73d16e93
2017-05-30 17:54:55,327 main DEBUG Registering MBean org.apache.logging.log4j2:type=73d16e93,component=StatusLogger
2017-05-30 17:54:55,329 main DEBUG Registering MBean org.apache.logging.log4j2:type=73d16e93,component=ContextSelector
2017-05-30 17:54:55,331 main DEBUG Registering MBean org.apache.logging.log4j2:type=73d16e93,component=Loggers,name=
2017-05-30 17:54:55,332 main DEBUG Registering MBean org.apache.logging.log4j2:type=73d16e93,component=Loggers,name=wh1spr.gamebot.MessageHandler
2017-05-30 17:54:55,336 main DEBUG Registering MBean org.apache.logging.log4j2:type=73d16e93,component=Appenders,name=Console
2017-05-30 17:54:55,337 main DEBUG Registering MBean org.apache.logging.log4j2:type=73d16e93,component=Appenders,name=MyFile
2017-05-30 17:54:55,338 main DEBUG Reconfiguration complete for context[name=73d16e93] at URI /Users/NAME/Desktop/Workspace/DiscordBot/bin/log4j2.xml (org.apache.logging.log4j.core.LoggerContext@2cdd0d4b) with optional ClassLoader: null
2017-05-30 17:54:55,339 main DEBUG Shutdown hook enabled. Registering a new one.
2017-05-30 17:54:55,340 main DEBUG LoggerContext[name=73d16e93, org.apache.logging.log4j.core.LoggerContext@2cdd0d4b] started OK.
17:54:55.383 [INFO ] org.eclipse.jetty.util.log - Logging initialized @1179ms to org.eclipse.jetty.util.log.Slf4jLog
17:54:55.389 [INFO ] sx.blah.discord.Discord4J - Discord4J v2.8.1 (60523841852316689438ed3099ad7203f5bd262b-6052384) (https://github.com/austinv11/Discord4J)
17:54:55.389 [INFO ] sx.blah.discord.Discord4J - A Java binding for the official Discord API, forked from the inactive https://github.com/nerd/Discord4J. Copyright (c) 2017, Licensed under GNU LGPLv3
17:54:56.887 [INFO ] sx.blah.discord.Discord4J - Websocket Connected.
17:54:57.107 [INFO ] sx.blah.discord.Discord4J - Connected to Discord Gateway v5. Receiving 1 guilds.
GameBot is ready for action!
17:55:19.565 [DEBUG] wh1spr.gamebot.MessageHandler - Message received! Wh1spr > &&shutdown ==> This is the message that initiates shutdown (saving a few files then calling System.exit(0))
2017-05-30 17:55:19,740 pool-1-thread-1 DEBUG Stopping LoggerContext[name=73d16e93, org.apache.logging.log4j.core.LoggerContext@2cdd0d4b]
2017-05-30 17:55:19,740 pool-1-thread-1 DEBUG Stopping LoggerContext[name=73d16e93, org.apache.logging.log4j.core.LoggerContext@2cdd0d4b]...
2017-05-30 17:55:19,743 pool-1-thread-1 DEBUG Shutting down FileManager Gamebot.log
2017-05-30 17:55:19,744 pool-1-thread-1 DEBUG Shut down FileManager Gamebot.log, all resources released: true ==> It should've written something to the file here, yet it doesn't.
2017-05-30 17:55:19,744 pool-1-thread-1 DEBUG Appender MyFile stopped with status true
2017-05-30 17:55:19,744 pool-1-thread-1 DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false
2017-05-30 17:55:19,744 pool-1-thread-1 DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false, all resources released: true
2017-05-30 17:55:19,745 pool-1-thread-1 DEBUG Appender Console stopped with status true
2017-05-30 17:55:19,745 pool-1-thread-1 DEBUG Stopped XmlConfiguration[location=/Users/NAME/Desktop/Workspace/DiscordBot/bin/log4j2.xml] OK
2017-05-30 17:55:19,745 pool-1-thread-1 DEBUG Stopped LoggerContext[name=73d16e93, org.apache.logging.log4j.core.LoggerContext@2cdd0d4b] with status true
编辑:
答案 0 :(得分:4)
这最终比评论更长,所以我将其作为答案发布。
看起来您只能设置Root level
一次。所以你实际上告诉log4j是这样的:
我。 &#34;将根记录器的级别设置为debug,并让它使用名为&#39; MyFile&#39;的文件追加器。
II。但是你说,&#34;实际上,将根记录器的级别设置为info,并将appender设置为名为&#39; Console&#39;的控制台appender。
执行以下操作时,您使用定义的记录器执行了以下操作:
<Logger name="wh1spr.gamebot.MessageHandler" level="Debug" additivity="false">
<AppenderRef ref="Console" />
</Logger>
告诉log4j你要创建一个名为&#34; wh1spr.gamebot.MessageHandler&#34;的记录器。如果将level设置为debug,并且您希望它使用名为Console'. Now, since you've set the root logger's level to
INFO`的Appender,则当前记录器的级别为Debug(优先级更高),因此您只需使用控制台appender。
你应该能够通过摆脱第二个<Root>...</Root>