我找不到一个允许以比通常看到的更好的方式格式化日志输出语句的库.我记得的一个功能是它可以"偏移"日志消息,具体取决于日志语句发生位置的"嵌套".
也就是说,而不是这个:
DEBUG | DefaultBeanDefinitionDocumentReader.java| 86 | Loading bean definitions
DEBUG | AbstractAutowireCapableBeanFactory.java| 411 | Finished creating instance of bean 'MS-SQL'
DEBUG | DefaultSingletonBeanRegistry.java| 213 | Creating shared instance of singleton bean 'MySQL'
DEBUG | AutowireCapableBeanFactory.java| 383 | Creating instance of bean 'MySQL'
DEBUG | AutowireCapableBeanFactory.java| 459 | Eagerly caching bean 'MySQL' to allow for resolving potential circular references
DEBUG | AutowireCapableBeanFactory.java| 789 | Another debug message
Run Code Online (Sandbox Code Playgroud)
它会显示如下:
DEBUG | DefaultBeanDefinitionDocumentReader.java| 86 | Loading bean definitions
DEBUG | AbstractAutowireCapableBeanFactory.java | …Run Code Online (Sandbox Code Playgroud) 正如标题中所指出的,我正在使用Maven和Jetty.使用SLF4J和Logback进行日志记录.我在'src/main/resources'有'logback.xml'.
<configuration>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%-4relative [%thread] %-5level %class - %msg%n</pattern>
</layout>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.FileAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%-4relative [%thread] %-5level %class - %msg%n</pattern>
</layout>
<File>myLog.log</File>
</appender>
<logger name="org.mortbay">
<level value="debug" />
</logger>
<root>
<level value="error" />
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Run Code Online (Sandbox Code Playgroud)
但我的问题是,如果我运行/调试项目,它不会创建文件'myLog.log'.获取日志文件的解决方案是什么?
有没有办法只用SLF4J获取日志文件?
我想使用slf4j从.properties文件中读取数据,我可以在控制台上输出数据,但我想要的是在某个文件上输出数据,所以我需要文件Appender,这是在.properties文件和i中声明的我无法使用slf4j读取.properties文件.任何人都可以帮忙.
PS:我需要一个例子来解释如何在slf4j中使用.properties文件以及如何初始化logger工厂.
我们正在使用SLF4J
基于团队最近的讨论
if(LOG.isDebugEnabled()){
LOG.debug("hello " + a + " world" + b);
}
Run Code Online (Sandbox Code Playgroud)
比...更好
LOG.debug("hello {} world {}", a, b);
Run Code Online (Sandbox Code Playgroud)
因为在后一种情况下,hello {} world {}即使未启用"debug" ,也会创建String .换句话说,即使不需要,我们也总是创建字符串.
我喜欢后一版本,因为它显着提高了可读性.
有人可以提供意见吗?
问候,
Shardul.
编辑
让我换一点吧.
哪种方法更好?或者哪种方法最受欢迎?
Shardul.
我查阅了所有消息,但没有找到该问题的明确答案.
如何配置日志记录以记录CXF入站和出站的restful消息?
我有以下设置.
文件org.apache.cxf.Logger with
org.apache.cxf.common.logging.Log4jLogger
Run Code Online (Sandbox Code Playgroud)applicationContext.xml有以下内容(听起来很傻,但它是我可以获取消息输出的拦截器的唯一地方)
<bean id="abstractLoggingInterceptor" abstract="true">
<property name="prettyLogging" value="true"/>
</bean>
<bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"
parent="abstractLoggingInterceptor"/>
<bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"
parent="abstractLoggingInterceptor"/>
<cxf:bus>
<cxf:inInterceptors>
<ref bean="loggingInInterceptor"/>
</cxf:inInterceptors>
<cxf:outInterceptors>
<ref bean="loggingOutInterceptor"/>
</cxf:outInterceptors>
<cxf:outFaultInterceptors>
<ref bean="loggingOutInterceptor"/>
</cxf:outFaultInterceptors>
<cxf:inFaultInterceptors>
<ref bean="loggingInInterceptor"/>
</cxf:inFaultInterceptors>
</cxf:bus>
Run Code Online (Sandbox Code Playgroud)我试着用slf4j和log4j来遵循这些指令,但是我得到的唯一输出是应用程序日志消息.我可以在我的控制台上看到入站和出站邮件.
我是否可以获得类似于logback.xml的功能,因此我将应用程序日志和消息日志分开.示例:http://www.wolfe.id.au/2011/05/20/apache-cxf-logging/
谢谢.
编辑1: 我从我的类路径中删除了org.apache.cxf.common.logging.Log4jLogger,并将以下内容放到我的log4j.xml中.当日志记录级别等于INFO时,它将记录到文件和控制台.
<appender name="RSLOGFILE" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${project.basedir}/logs/cxf_inout_messages.log"/>
<param name="MaxFileSize" value="100KB"/>
<!-- Keep one backup file -->
<param name="MaxBackupIndex" value="1"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- Print the date in ISO 8601 format -->
<param name="ConversionPattern" value="%d …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Scala 2.11.6和Play 从Akka actor设置调试日志到控制台2.4.6.所以我看到这个配置的信息消息,但没有调试:
application.conf:
akka {
loggers = ["akka.event.slf4j.Slf4jLogger"]
level = "DEBUG"
logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"
}
Run Code Online (Sandbox Code Playgroud)
logback.xml:
<logger name="akka" level="DEBUG" />
<logger name="actors" level="DEBUG" />
Run Code Online (Sandbox Code Playgroud)
用法:
package actors
import akka.actor._
import akka.event.Logging
object DispatchActor {
def props(out: ActorRef) = Props(new DispatchActor(out))
}
class DispatchActor(out: ActorRef) extends Actor {
val log = Logging(context.system, this)
log.debug("akka started: info")
def receive = {
case msg: String =>
log.debug("actor received a message")
out ! ("I received your message: " + …Run Code Online (Sandbox Code Playgroud) 我在slf4j之后使用logback,想知道如何OutputStream从org.slf4j.Logger实例中获取引用。用例是Shrinkwrap的Archive.writeTo(OutputStream, ...)方法。
我知道,可以通过创建ByteArrayOutputStream,传递引用并将其内容写入记录器来解决该引用的需求。我知道,通常可以将stdout和stderr重定向到记录器。话虽如此,我正在寻找这个问题的直接答案。如果没有,我建议向slf4j添加一种机制。
我正在使用slf4j API 1.7.5和logback 1.2.2。
我已经创建了用于记录日志的扩展功能:
import org.slf4j.LoggerFactory
fun Any.log(msg: String) {
LoggerFactory.getLogger(javaClass.name).debug(msg)
}
Run Code Online (Sandbox Code Playgroud)
但是我不确定它是否会在任何时候被调用,因为方法LoggerFactory.getLogger调用getILoggerFactory。
MB已经有人做过类似的事情,可以向我保证不会有任何内存泄漏:)吗?
现在我使用老式的方法(在类中声明logger字段):
companion object {
private val logger = LoggerFactory.getLogger(LoggerTest::class.java.name)
}
Run Code Online (Sandbox Code Playgroud)
但是unit像这样的简单测试:
@Test
fun testLogger() {
val start = System.currentTimeMillis()
for (i in 0..999) {
log("i=" + i)
}
val end = System.currentTimeMillis()
val time = end - start
println("*** TIME=" + time.toDouble() / 1000 + " sec")
}
Run Code Online (Sandbox Code Playgroud)
显示的结果与旧时尚选项相同:
@Test
fun testLogger() {
val start = System.currentTimeMillis()
for (i in 0..999) {
logger.debug("i=" …Run Code Online (Sandbox Code Playgroud) 我可能在这里做了些蠢事.我的dropwizard设置有一些(小的)错误.运行着色jar工作正常,但在执行集成测试时,我收到此警告:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/graphhopper/web/target/graphhopper-web-0.11-SNAPSHOT.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/user/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
INFO [2018-04-08 18:44:27,653] org.eclipse.jetty.util.log: Logging initialized @1090ms to org.eclipse.jetty.util.log.Slf4jLog
Run Code Online (Sandbox Code Playgroud)
graphhopper-web-0.11-SNAPSHOT.jar是带有dropwizard(带有logback)的阴影jar.
这通常意味着类路径上不止一个slf4j绑定,但我可以拒绝这个理论,只有slf4j-api存在,加上dropwizard的logback依赖.我还用Netbeans和.分析了依赖图
mvn dependency:tree -Dverbose -Dincludes=org.slf4j
(见这里的输出)但找不到有问题的东西.
是否可以将阴影jar(带有logback)以某种方式与其他jar(包括logback)一起放入classpath中mvn clean install?我怎么能避免这个?
通过以下方式重现:
git clone https://github.com/graphhopper/graphhopper
cd web
mvn clean install
Run Code Online (Sandbox Code Playgroud)
看到这个问题.
我试图找出一种在Java中基于反应/事件的异步编程中使用MDC的方法,但找不到。
有人可以解释如何在回调事件/方法中传播MDC变量吗?
在这种情况下,如何像在传统的同步编程中那样跟踪请求,直到响应被提供为止?