import org.apache.log4j.NDC; public class Logging { public static void main(String[] args) throws Exception { org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(this.getClass()); NDC.push("USER01"); log.info("RECORD-No1" ); NDC.push("192.168.0.1"); log.info("RECORD-No2" ); NDC.pop(); log.info("RECORD-No3" ); NDC.pop(); log.info("RECORD-No4" ); NDC.remove(); } }
01:28:02,265 INFO sample.pg.Logging - USER01:RECORD-No1 01:28:02,265 INFO sample.pg.Logging - USER01 192.168.0.1:RECORD-No2 01:28:02,281 INFO sample.pg.Logging - USER01:RECORD-No3 01:28:02,281 INFO sample.pg.Logging - :RECORD-No4