当前位置: 首页 > 知识库问答 >
问题:

akka、spray和log4j2记录不正确

冉俊德
2023-03-14

我正在Akka、Spray和Log4j2中构建一个新应用程序。我的大多数主要系统都在“hello world”类型的模式下工作,但我很难让日志正常工作,我无法判断这是log4j2问题、Akka/Spray问题还是我的迟钝问题。

首先,我的Akka配置文件

akka {
    loggers = ["akka.event.slf4j.Slf4jLogger"]
    loglevel = DEBUG
    log-config-on-start = on

    actor{
        debug {
              receive = on
              autoreceive = on
              lifecycle = on
              fsm = on
              event-stream = on
              unhandled = on
              router-misconfiguration = on
            }
    }
}

现在,如果我把loggers行注释掉,在IntelliJ中运行这个应用程序,它在控制台中是完美的。我得到我想要的一切,我得到非常详细和有用的日志,我得到配置设置,我看到交易进出,我是一个非常快乐的人。当我试图通过Log4j2管道传输它时,问题就出现了。

现在我想这样做的原因是在不远的将来,我将把所有日志泵送到Cassandra集群中进行map减少工作。

因此,我在上面的配置文件中取消了slf4j行的注释,在类路径中添加slf4j、log4j2和log4j2的log4j-slf4j-impl,log4j激活,读取配置文件,创建日志文件,将附件的初始化和调试信息记录到控制台,就这样。没有其他消息。没有一件幸运的事。娜达。

这是我的log4j2.xml档案

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="DEBUG">
    <appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <PatternLayout pattern="%-5p %c{2} - %m%n" />
        </Console>
        <RollingRandomAccessFile name="logFileOutput" fileName="logs/wisdomOutput.log"
                     filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <pattern>%d %p %C [%t] %m%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="50 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="20"/>
        </RollingRandomAccessFile>
    </appenders>
    <loggers>
        <!-- if you want to dump different packages to different files, here's where you do it
            <logger name="com.onuspride" level="DEBUG" additivity="false">
            <appender-ref ref="logFileOutput"/>
        </logger>-->
        <root>
            <appender-ref ref="CONSOLE"/>
            <appender-ref ref="logFileOutput"/>
        </root>
    </loggers>
</configuration>

我不知道我是否在处理log4j2中的错误,或者我的配置文件是否错误,或者是什么。

这是当我的日志被激活时我的控制台输出

/usr/lib/jvm/java-7-oracle/bin/java -Didea.launcher.port=7533 -Didea.launcher.bin.path=/home/willie/apps/intellij13/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-7-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-7-oracle/jre/lib/javaws.jar:/usr/lib/jvm/java-7-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-7-oracle/jre/lib/jfxrt.jar:/usr/lib/jvm/java-7-oracle/jre/lib/deploy.jar:/usr/lib/jvm/java-7-oracle/jre/lib/rt.jar:/usr/lib/jvm/java-7-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-7-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-7-oracle/jre/lib/plugin.jar:/usr/lib/jvm/java-7-oracle/jre/lib/management-agent.jar:/usr/lib/jvm/java-7-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-7-oracle/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-7-oracle/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-7-oracle/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-7-oracle/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-7-oracle/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-7-oracle/jre/lib/ext/sunjce_provider.jar:/home/willie/code/onus/collective/build/production/wisdom:/home/willie/.gradle/caches/modules-2/files-2.1/org.scala-lang/scala-reflect/2.10.0/10ec1256a6e875e886fec050edb0669166912d0d/scala-reflect-2.10.0.jar:/home/willie/.gradle/caches/modules-2/files-2.1/io.spray/spray-routing/1.2.0/955690e1d6b0d92f06da4b53a185a0d59615e1c6/spray-routing-1.2.0.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.json4s/json4s-ast_2.10/3.2.6/e52c46c51f75a7536c3ffedefdbdc331ffaecbdd/json4s-ast_2.10-3.2.6.jar:/home/willie/.gradle/caches/modules-2/files-2.1/io.spray/spray-http/1.2.0/ba24112e069e61224255f6c3cf85727767351fcc/spray-http-1.2.0.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.json4s/json4s-core_2.10/3.2.6/c89c64b04fec958fc57a271bc741dfde759d8ffc/json4s-core_2.10-3.2.6.jar:/home/willie/.gradle/caches/modules-2/files-2.1/com.thoughtworks.paranamer/paranamer/2.6/52c3c8d8876440d714e23036eb87bcc4244d9aa5/paranamer-2.6.jar:/home/willie/.gradle/caches/modules-2/files-2.1/io.spray/spray-util/1.2.0/efea073f15b79a3f62f1f16b22cfcad9a2119439/spray-util-1.2.0.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.json4s/json4s-native_2.10/3.2.6/3728a03373e3eac2354ea0cc855fef71fe97d82e/json4s-native_2.10-3.2.6.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.scala-lang/scala-compiler/2.10.0/fec8066cd2b4f8dc7ff7ba7a8e0a792939d9f9a/scala-compiler-2.10.0.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.jvnet.mimepull/mimepull/1.9.3/c55096ff89a27e22c2e081371d0570ac19cc6788/mimepull-1.9.3.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.parboiled/parboiled-scala_2.10/1.1.6/bc93b215151f7dc42653f3eb0243b3130a7e1e11/parboiled-scala_2.10-1.1.6.jar:/home/willie/.gradle/caches/modules-2/files-2.1/com.chuusai/shapeless_2.10/1.2.4/2f41ccc19eb4d38ae5ef907ce7469003e0253dc5/shapeless_2.10-1.2.4.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.parboiled/parboiled-core/1.1.6/11bd0c34fc6ac3c3cbf440ab8180cc6422c044e9/parboiled-core-1.1.6.jar:/home/willie/.gradle/caches/modules-2/files-2.1/io.spray/spray-httpx/1.2.0/328e8d28cd3fcd7a7aea6467048a17577d427272/spray-httpx-1.2.0.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.scala-lang/scalap/2.10.0/ab42ae21d1fd7311b367fe3d7f33343f2e4bff6b/scalap-2.10.0.jar:/home/willie/code/onus/collective/build/production/inclusive:/home/willie/.gradle/caches/modules-2/files-2.1/org.apache.commons/commons-lang3/3.1/905075e6c80f206bbe6cf1e809d2caa69f420c76/commons-lang3-3.1.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.springframework.security/spring-security-web/3.1.4.RELEASE/56d52794df838d0bc3ccbc149a62ea019946f82a/spring-security-web-3.1.4.RELEASE.jar:/home/willie/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-annotations/2.3.0/f5e853a20b60758922453d56f9ae1e64af5cb3da/jackson-annotations-2.3.0.jar:/home/willie/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.1.1/5043bfebc3db072ed80fbd362e7caf00e885d8ae/commons-logging-1.1.1.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jdbc/3.0.7.RELEASE/c53d9bf4e6a6a6cf49c7d7630e612042364295a1/spring-jdbc-3.0.7.RELEASE.jar:/home/willie/.m2/repository/org/springframework/social/spring-social-security/1.1.0.M4/spring-social-security-1.1.0.M4.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-webmvc/3.2.3.RELEASE/6d6d1b53c53dbefd0d8c15f2d5c194ced1f8d53c/spring-webmvc-3.2.3.RELEASE.jar:/home/willie/.m2/repository/org/springframework/social/spring-social-web/1.1.0.M4/spring-social-web-1.1.0.M4.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/3.2.3.RELEASE/accdd65db57e79e49f2af037bb76f5a55a580f00/spring-core-3.2.3.RELEASE.jar:/home/willie/.gradle/caches/modules-2/files-2.1/aopalliance/aopalliance/1.0/235ba8b489512805ac13a8f9ea77a1ca5ebe3e8/aopalliance-1.0.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.springframework.security/spring-security-core/3.1.4.RELEASE/41af4a10d7e31722bd8ef2c101f60afb6c24e04b/spring-security-core-3.1.4.RELEASE.jar:/home/willie/.gradle/caches/modules-2/files-2.1/javax.inject/javax.inject/1/6975da39a7040257bd51d21a231b76c915872d38/javax.inject-1.jar:/home/willie/.m2/repository/org/springframework/social/spring-social-core/1.1.0.M4/spring-social-core-1.1.0.M4.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/3.2.3.RELEASE/a48eb92ef51e0aed7d23dcaa54225b8f24808db4/spring-beans-3.2.3.RELEASE.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-expression/3.2.3.RELEASE/51e4c94a79892c0a2d15c5efda8c5b336ebf5c37/spring-expression-3.2.3.RELEASE.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-web/3.2.3.RELEASE/833c5e9a60f82209a3f669a5026dfa8de57c1df3/spring-web-3.2.3.RELEASE.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-context/3.2.3.RELEASE/12ad5920647556704f79cc579d4e133430741c5/spring-context-3.2.3.RELEASE.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aop/3.2.3.RELEASE/a70bec95e45a29cad126d250699d0ed0fedfe28a/spring-aop-3.2.3.RELEASE.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.springframework/spring-tx/3.0.7.RELEASE/c340bf8606f6bf235bc1277d25315df1abe51c31/spring-tx-3.0.7.RELEASE.jar:/home/willie/.gradle/caches/modules-2/files-2.1/io.spray/spray-can/1.2.0/4a3ec8def45e7f567739fe15a6537f57316612bf/spray-can-1.2.0.jar:/home/willie/.gradle/caches/modules-2/files-2.1/io.spray/spray-io/1.2.0/35c2866c4c7ac7f9b4ee2c06df18075a5bb8c55b/spray-io-1.2.0.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-api/2.0-beta9/1dd66e68cccd907880229f9e2de1314bd13ff785/log4j-api-2.0-beta9.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-core/2.0-beta9/678861ba1b2e1fccb594bb0ca03114bb05da9695/log4j-core-2.0-beta9.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.scala-lang/scala-library/2.10.3/21d99cee3d3e478255ef9fcc90b571fb2ab074fc/scala-library-2.10.3.jar:/home/willie/.gradle/caches/modules-2/files-2.1/com.typesafe/config/1.0.2/a0bca82c39f23f75e3afccd6e12840eeabaea123/config-1.0.2.jar:/home/willie/.gradle/caches/modules-2/files-2.1/com.typesafe.akka/akka-actor_2.10/2.2.3/3a74ea5a7ec2a23fa22077c4debebf96f015dc73/akka-actor_2.10-2.2.3.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-api/1.7.5/6b262da268f8ad9eff941b25503a9198f0a0ac93/slf4j-api-1.7.5.jar:/home/willie/.gradle/caches/modules-2/files-2.1/com.typesafe.akka/akka-slf4j_2.10/2.2.3/616c79a2d66ebd136bf0a4e5902bcd1e74dd4cb1/akka-slf4j_2.10-2.2.3.jar:/home/willie/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-slf4j-impl/2.0-beta9/e970fc3c10144a521d691db4ac38027cc1935b74/log4j-slf4j-impl-2.0-beta9.jar:/home/willie/.gradle/caches/modules-2/files-2.1/com.lmax/disruptor/3.2.0/ac62995678dd4b906e85b26354aa2ebfda130c32/disruptor-3.2.0.jar:/home/willie/apps/intellij13/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain com.onuspride.wisdom.Boot
2014-01-10 17:42:36,673 DEBUG Generated plugins in 0.000064883 seconds
2014-01-10 17:42:36,719 DEBUG Calling createLayout on class org.apache.logging.log4j.core.layout.PatternLayout for element PatternLayout with params(pattern="%-5p %c{2} - %m%n", Configuration(/home/willie/code/onus/collective/build/production/wisdom/log4j2.xml), null, charset="null", alwaysWriteExceptions="null")
2014-01-10 17:42:36,721 DEBUG Generated plugins in 0.000061321 seconds
2014-01-10 17:42:36,727 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.ConsoleAppender for element Console with params(PatternLayout(%-5p %c{2} - %m%n), null, target="SYSTEM_OUT", name="CONSOLE", follow="null", ignoreExceptions="null")
2014-01-10 17:42:36,729 DEBUG Calling createLayout on class org.apache.logging.log4j.core.layout.PatternLayout for element PatternLayout with params(pattern="%d %p %C [%t] %m%n", Configuration(/home/willie/code/onus/collective/build/production/wisdom/log4j2.xml), null, charset="null", alwaysWriteExceptions="null")
2014-01-10 17:42:36,732 DEBUG Calling createPolicy on class org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy for element TimeBasedTriggeringPolicy with params(interval="null", modulate="null")
2014-01-10 17:42:36,735 DEBUG Calling createPolicy on class org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy for element SizeBasedTriggeringPolicy with params(size="50 MB")
2014-01-10 17:42:36,743 DEBUG Calling createPolicy on class org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy for element Policies with params(Policies={TimeBasedTriggeringPolicy, SizeBasedTriggeringPolicy(size=52428800)})
2014-01-10 17:42:36,748 DEBUG Calling createStrategy on class org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy for element DefaultRolloverStrategy with params(max="20", min="null", fileIndex="null", compressionLevel="null", Configuration(/home/willie/code/onus/collective/build/production/wisdom/log4j2.xml))
2014-01-10 17:42:36,754 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender for element RollingRandomAccessFile with params(fileName="logs/wisdomOutput.log", filePattern="logs/${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz", append="null", name="logFileOutput", immediateFlush="null", Policies(CompositeTriggeringPolicy{TimeBasedTriggeringPolicy, SizeBasedTriggeringPolicy(size=52428800)}), DefaultRolloverStrategy(DefaultRolloverStrategy(min=1, max=20)), PatternLayout(%d %p %C [%t] %m%n), null, ignoreExceptions="null", advertise="null", advertiseURI="null", Configuration(/home/willie/code/onus/collective/build/production/wisdom/log4j2.xml))
2014-01-10 17:42:36,763 DEBUG Starting RollingRandomAccessFileManager logs/wisdomOutput.log
2014-01-10 17:42:36,767 DEBUG Generated plugins in 0.000057060 seconds
2014-01-10 17:42:36,772 DEBUG Calling createAppenders on class org.apache.logging.log4j.core.config.plugins.AppendersPlugin for element appenders with params(Appenders={CONSOLE, logFileOutput})
2014-01-10 17:42:36,773 DEBUG Generated plugins in 0.000052311 seconds
2014-01-10 17:42:36,777 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element appender-ref with params(ref="CONSOLE", level="null", null)
2014-01-10 17:42:36,778 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element appender-ref with params(ref="logFileOutput", level="null", null)
2014-01-10 17:42:36,783 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger for element root with params(additivity="null", level="null", includeLocation="null", AppenderRef={CONSOLE, logFileOutput}, Properties={}, Configuration(/home/willie/code/onus/collective/build/production/wisdom/log4j2.xml), null)
2014-01-10 17:42:36,787 DEBUG Calling createLoggers on class org.apache.logging.log4j.core.config.plugins.LoggersPlugin for element loggers with params(Loggers={root})
2014-01-10 17:42:36,799 DEBUG Reconfiguration completed
2014-01-10 17:42:36,808 DEBUG Using default SystemClock for timestamps
2014-01-10 17:42:36,815 DEBUG property AsyncLogger.WaitStrategy=null
2014-01-10 17:42:36,815 DEBUG disruptor event handler uses SleepingWaitStrategy
2014-01-10 17:42:37,045 DEBUG No AsyncLogger.ExceptionHandler specified
2014-01-10 17:42:37,058 DEBUG Starting AsyncLogger disruptor with ringbuffer size 262144...

我想我的问题是,我不知道如何从Akka通过log4j2对标准输出进行格式化,然后写入文件。目前我真的不想再复杂了。

我的设置有什么问题?为什么不起作用?

共有2个答案

傅胡媚
2023-03-14

一切看起来都很好,我想你只需要设置根记录器的日志级别(看起来你已经找到了)。

一件小事:在RollingFile appender PatternLayout中,您使用的是%C。我建议您改用%C(小写)。%C遍历堆栈跟踪以获取位置信息,这将导致性能更差。(log4j2手册有更多细节。)

邹京
2023-03-14

想清楚。这是我的配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="debug">
    <appenders>
        <!--<Console name="CONSOLE" target="SYSTEM_OUT">
            <PatternLayout pattern="%-5p %c{2} - %m%n" />
        </Console>-->
        <RollingRandomAccessFile name="logFileOutput" fileName="logs/wisdomOutput.log"
                     filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <pattern>%d %p %C [%t] %m%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="50 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="20"/>
        </RollingRandomAccessFile>
    </appenders>
    <loggers>
        <!-- if you want to dump different packages to different files, here's where you do it
            <logger name="com.onuspride" level="DEBUG" additivity="false">
            <appender-ref ref="logFileOutput"/>
        </logger>-->
        <root level="debug" includeLocation="true">
            <!--<appender-ref ref="CONSOLE"/>-->
            <appender-ref ref="logFileOutput"/>
        </root>
    </loggers>
</configuration>
 类似资料:
  • 我使用的是Camel3.5和Spring Boot2.3.4应用程序。我试图在log4j2.xml文件中使用log4j2和配置日志级别和附加符。它从处理器工作,但不直接在RouteDefinition中工作。 processor类: 下面是控制台输出。只打印错误日志级别。我必须调用方法.log(logginglevel.debug,LOGGER)才能得到我想要的。 编辑 感谢Luca Burgaz

  • 使用spray时,我一直遇到相同的设计问题,即在Akka中执行一些异步(tell)操作之后,如何为请求找到spray http请求的原始上下文。 我使用的是Net-a-Porter actor per request模型。它创建了一个子执行元,我指定该子执行元来处理每个请求,该子执行元由另一个包含正确请求上下文的执行元进行封装。

  • 我有一个maven项目,它使用log4j2进行日志记录。我想覆盖RootLogger的模式。 这是我的log4j2。属性文件: 这是我的pom。log4j的xml文件: 我这样叫我的记录仪: 我的配置有两个问题。第一个是根记录器不使用我的ConsolePender(警告等以默认格式显示),在记录错误时,我只得到第一行,而不是整个堆栈跟踪)。

  • 互联网上有许多帖子建议如何从logback迁移到log4j2进行Spring引导日志记录。 参考这个sof post-Spring Boot日志与log4j2-我已经配置我的项目使用log4j2。然后我添加了一些基本的log4j2.xml和log4j2.properties文件来测试。 但是在这些更改之后,我的项目无法进行任何日志记录。有人能帮我找出毛病吗? 项目代码可在github-https:

  • 问题内容: 我正在尝试使用 Log4j2 将spring日志打印到文件和控制台中。我猜这是我的Log4j2配置中的问题。我无法使其正常运行。我的log4j2.xml文件中有以下配置: 这些是我在pom文件中具有的依赖项:(可能其中一些不是必需的) 我没有在Java代码中做任何与spring相关的事情。我正在使用Apache Camel的Main类,该类读取我的spring配置并加载bean。 我究

  • 问题内容: 因此,我正在尝试学习log4j2,并把头放在记录器及其级别和父母传播上。 当前,我的源层次结构运行是: 而我的CalculatorMain是: 我的log4j2.xml是 问题是将输出到控制台的root记录程序设置为。根据我对级别的理解,这意味着我的root记录器应仅输出错误日志或更低的错误日志。然后是我的 记录器,前者应仅记录错误并降低记录,而后者应记录跟踪并降低记录。因此,我的理解