在Logback中记录并行线程 [英] Logging parallel threads in logback

查看:1059
本文介绍了在Logback中记录并行线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将简要介绍我的Selenium框架,以便我可以解释我的问题.

I'll try to make a brief description of my Selenium framework so that I can explain my problem.

我使用 Selenium 2 (当前版本2.3.1)+ testNG 5.14

I use Selenium 2 (current version 2.3.1) + testNG 5.14

我设置了testng.xml文件以在2个实例中并行运行测试套件中的测试

I set testng.xml file to run the tests in the test suite in parallel, just 2 instances

出于记录目的,我使用 logback (我读过的是日志世界中的第二件事)

For logging purposes, I use logback (I've read is the next best thing in the log world)

我的问题是,当检查应用程序日志记录时,我得到如下信息:

My problem is that when checking what the application logs I get something like this:

18:48:58.551 [TestNG] INFO d.a.a.s.t.setup.TestConfiguration- 从用户池中检索随机用户

18:48:58.551 [TestNG] INFO d.a.a.s.t.setup.TestConfiguration - Retrieving random User from User Pool

18:48:58.551 [TestNG] INFO d.a.a.s.t.setup.TestConfiguration- 从用户池中检索随机用户

18:48:58.551 [TestNG] INFO d.a.a.s.t.setup.TestConfiguration - Retrieving random User from User Pool

18:48:58.551 [TestNG]调试d.a.a.s.t.setup.TestConfiguration- 创建用于访问数据库的数据源

18:48:58.551 [TestNG] DEBUG d.a.a.s.t.setup.TestConfiguration - Creating DataSource for acccessing DataBase

18:48:58.551 [TestNG]调试d.a.a.s.t.setup.TestConfiguration- 创建用于访问数据库的数据源

18:48:58.551 [TestNG] DEBUG d.a.a.s.t.setup.TestConfiguration - Creating DataSource for acccessing DataBase

18:48:58.552 [TestNG]调试d.a.a.s.t.setup.TestConfiguration- 启动SQL查询

18:48:58.552 [TestNG] DEBUG d.a.a.s.t.setup.TestConfiguration - Launching SQL Query

18:48:58.552 [TestNG]调试d.a.a.s.t.setup.TestConfiguration- 启动SQL查询

18:48:58.552 [TestNG] DEBUG d.a.a.s.t.setup.TestConfiguration - Launching SQL Query

18:48:59.613 [TestNG] TRACE d.a.a.s.t.setup.TestConfiguration-查询 成功

18:48:59.613 [TestNG] TRACE d.a.a.s.t.setup.TestConfiguration - Query succesful

18:48:59.613 [TestNG] TRACE d.a.a.s.t.setup.TestConfiguration-查询 成功

18:48:59.613 [TestNG] TRACE d.a.a.s.t.setup.TestConfiguration - Query succesful

如您所见,无法看到同时运行的两个线程之间的差异.我的问题是,是否可以配置登录设置,以便它们也添加数字或ID来标识正在运行的每个线程?

As you may see, it is impossible to see the difference between the two threads that are running at the same time. My question is, is there a way I can configure the logback settings so that they also add a number or Id to identify each thread that is running?

PD为防万一,我的 logback.xml 文件如下所示:

PD Just in case it may help, my logback.xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>logs/selenium.log</file>

    <encoder>
      <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="trace">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

推荐答案

登录映射的诊断上下文(MDC)是您的朋友.它允许您添加线程局部变量,这些变量可以进行管理,在线程之间复制以及使用日志记录格式进行记录.

Logback Mapped Diagnostic Context (MDC) is your friend. It allows you to add thread local variables that can be managed, copied between threads, and logged using a logging format.

从文档中

logback的设计目标之一是审核和调试复杂的分布式应用程序.大多数现实世界中的分布式系统需要同时处理多个客户端.在这种系统的典型多线程实现中,不同的线程将处理不同的客户端.将一个客户端的日志记录输出与另一个客户端的日志记录输出区分开的一种可能但略微沮丧的方法包括为每个客户端实例化一个新的单独的记录器.此技术促进记录器的扩散,并可能增加记录器的管理开销.

One of the design goals of logback is to audit and debug complex distributed applications. Most real-world distributed systems need to deal with multiple clients simultaneously. In a typical multithreaded implementation of such a system, different threads will handle different clients. A possible but slightly discouraged approach to differentiate the logging output of one client from another consists of instantiating a new and separate logger for each client. This technique promotes the proliferation of loggers and may increase their management overhead.

更轻松的技术包括唯一地标记服务给定客户端的每个日志请求.尼尔·哈里森(Neil Harrison)在R. Martin,D.Riehle和F.Buschmann(Addison-Wesley,1997)编辑的《程序设计3的模式语言中的诊断消息的记录模式》一书中描述了这种方法. Logback利用了SLF4J API中包含的该技术的一种变体:映射诊断上下文(MDC).

A lighter technique consists of uniquely stamping each log request servicing a given client. Neil Harrison described this method in the book Patterns for Logging Diagnostic Messages in Pattern Languages of Program Design 3, edited by R. Martin, D. Riehle, and F. Buschmann (Addison-Wesley, 1997). Logback leverages a variant of this technique included in the SLF4J API: Mapped Diagnostic Contexts (MDC).

要唯一标记每个请求,用户将上下文信息放入MDC中,即"Mapped Diagnostic Context"(映射诊断上下文)的缩写. MDC类的主要部分如下所示.有关方法的完整列表,请参考 MDC javadocs . /p>

To uniquely stamp each request, the user puts contextual information into the MDC, the abbreviation of Mapped Diagnostic Context. The salient parts of the MDC class are shown below. Please refer to the MDC javadocs for a complete list of methods.

这篇关于在Logback中记录并行线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆