logback,”printStackTrace()”方法在遇到”log.debug()”方法后执行。

huangapple go评论78阅读模式
英文:

logback, Method printStackTrace() is executed after meeting with method log.debug()

问题

Method e.printStackTrace() is executed after the method log.debug().

maven

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.24</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.4.5</version>
</dependency>`

my code

@Slf4j
public class SleepTest {
    public static void main(String[] args) {
        //Create a thread named "t0" and start it.
        Thread t0 = new Thread(()->{
            log.debug("running...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("continue");
        },"t0");
        t0.start();
        //interrupt "t0"
        t0.interrupt();
    }
}

output

00:09:48 [t0] [DEBUG] com.codingcube.introduction.SleepTest - running...
00:09:48 [t0] [DEBUG] com.codingcube.introduction.SleepTest - bug
00:09:48 [t0] [DEBUG] com.codingcube.introduction.SleepTest - continue
java.lang.InterruptedException: sleep interrupted
	at java.base/java.lang.Thread.sleep(Native Method)
	at com.codingcube.introduction.SleepTest.lambda$main$0(SleepTest.java:14)
	at java.base/java.lang.Thread.run(Thread.java:829)

In the code, log.debug("continue"); comes after e.printStackTrace();, but the output comes before it.

This problem does not occur when I run part of the code in the main thread.

@Slf4j
public class SleepTest {
    public static tomain(String[] args) {
        log.debug("running...");
        try {
            Thread.sleep(1000);
            throw new InterruptedException();
        }catch (Exception e){
            e.printStackTrace();
        }
        log.debug("continue");
    }
}

output

00:16:05 [main] [DEBUG] com.codingcube.introduction.SleepTest - running...
java.lang.InterruptedException
	at com.codingcube.introduction.SleepTest.main(SleepTest.java:13)
00:16:06 [main] [DEBUG] com.codingcube.introduction.SleepTest - continue

and the problem does not occur when I remove the first log

@Slf4j
public class SleepTest {
    public static void main(String[] args) {
        //Create a thread named "t0" and start it.
        Thread t0 = new Thread(()->{
            //log.debug("running...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("continue");
        },"t0");
        t0.start();

        //interrupt "t0"
        t0.interrupt();
    }
}

output

java.lang.InterruptedException: sleep interrupted
	at java.base/java.lang.Thread.sleep(Native Method)
	at com.codingcube.introduction.SleepTest.lambda$main$0(SleepTest.java:15)
	at java.base/java.lang.Thread.run(Thread.java:829)
00:18:17 [t0] [DEBUG] com.codingcube.introduction.SleepTest - continue
英文:

Method e.printStackTrace() is executed after the method log.debug().

maven

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.24</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.4.5</version>
</dependency>`

my code

@Slf4j
public class SleepTest {
    public static void main(String[] args) {
        //Create a thread named "t0" and start it.
        Thread t0 = new Thread(()->{
            log.debug("running...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("continue");
        },"t0");
        t0.start();
        //interrupt "t0"
        t0.interrupt();
    }
}

output

00:09:48 [t0] [DEBUG] com.codingcube.introduction.SleepTest - running...
00:09:48 [t0] [DEBUG] com.codingcube.introduction.SleepTest - bug
00:09:48 [t0] [DEBUG] com.codingcube.introduction.SleepTest - continue
java.lang.InterruptedException: sleep interrupted
	at java.base/java.lang.Thread.sleep(Native Method)
	at com.codingcube.introduction.SleepTest.lambda$main$0(SleepTest.java:14)
	at java.base/java.lang.Thread.run(Thread.java:829)

In the code, log.debug("continue"); comes after e.printStackTrace();, but the output comes before it.

This problem does not occur when I run part of the code in the main thread.

@Slf4j
public class SleepTest {
    public static void main(String[] args) {
        log.debug("running...");
        try {
            Thread.sleep(1000);
            throw new InterruptedException();
        }catch (Exception e){
            e.printStackTrace();
        }
        log.debug("continue");
    }
}

output

00:16:05 [main] [DEBUG] com.codingcube.introduction.SleepTest - running...
java.lang.InterruptedException
	at com.codingcube.introduction.SleepTest.main(SleepTest.java:13)
00:16:06 [main] [DEBUG] com.codingcube.introduction.SleepTest - continue

and the problem does not occur when I remove the first log

@Slf4j
public class SleepTest {
    public static void main(String[] args) {
        //Create a thread named "t0" and start it.
        Thread t0 = new Thread(()->{
            //log.debug("running...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("continue");
        },"t0");
        t0.start();

        //interrupt "t0"
        t0.interrupt();
    }
}

output

java.lang.InterruptedException: sleep interrupted
	at java.base/java.lang.Thread.sleep(Native Method)
	at com.codingcube.introduction.SleepTest.lambda$main$0(SleepTest.java:15)
	at java.base/java.lang.Thread.run(Thread.java:829)
00:18:17 [t0] [DEBUG] com.codingcube.introduction.SleepTest - continue

答案1

得分: 1

e.printStackTrace() 在执行之前执行 log.debug("continue")

第一个写入 stderr,第二个可能写入 stdout。
输出顺序取决于两个流刷新和关闭的顺序。

尝试将 e.printStackTrace() 替换为 log.debug("exception", e)

英文:

e.printStackTrace() is executed before log.debug("continue")

The first one writes to stderr and the second one writes probably to stdout.
The output order depends on the order the two streams are flushed and closed.

Try to replace e.printStackTrace() with log.debug("exception",e)

huangapple
  • 本文由 发表于 2023年3月10日 00:25:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定