Log4j, nested diagnostic contexts

I have a MySession object (shared session, not web) that works in its thread. I want to use the NDC class to include some data taken from the fields of my MySession: the user who created it, the start time, etc. Etc. I would like to “visualize” the fields in the message. Is this possible or only supported for messages? thanks in advance

public class MySession {
    String userName;
    Date startTime;

    public void doSomething() {
        NDC.push(this);                                    //cannot do something like this?
        NDC.push(this.userName 
                  + " " + startTime.toString());          //or should I do this? 
    }

}
+3
source share
2 answers

NDC simply adds a “context” (free-form string) to all log messages (which may or may not be displayed depending on the logging format). The nested part means that it NDC.pop()returns to the previous (next level up) context.

, , , - this.username + '.' + this.startTime.toString(), . API, push String; , ( String), .

+3

( ), NDC. , toString() (, , ) . / NDC , NDC .

. , (, , ). - NDC ( ) , , . .

+2

All Articles