Last active
January 23, 2016 00:57
-
-
Save 8bitbuddhist/6d232fb4b9803207bf3a to your computer and use it in GitHub Desktop.
Simple demonstration of Log4J 2's ThreadContext using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<Configuration status="WARN"> | |
<Appenders> | |
<File name="File" fileName="log.txt" > | |
<JSONLayout properties="true" compact="false" eventEol="true" /> | |
</File> | |
</Appenders> | |
<Loggers> | |
<Root level="info"> | |
<AppenderRef ref="File" /> | |
</Root> | |
</Loggers> | |
</Configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.UUID; | |
import org.apache.logging.log4j.Logger; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.ThreadContext; | |
public class MyAppStack { | |
private static final Logger logger = LogManager.getLogger(MyAppStack.class); | |
public void login(String username, UUID sessionID) { | |
ThreadContext.push("sessionID", sessionID.toString()); | |
ThreadContext.push("username", username); | |
logger.info("User logged in.") | |
} | |
public void logout() { | |
logger.info("Logging out user."); | |
ThreadContext.remove("username"); | |
ThreadContext.remove("sessionID"); | |
logger.info("User logged out.") | |
} | |
public static void main(String[] args) { | |
UUID sessionID = java.util.UUID.randomUUID(); | |
String username = "admin"; | |
login(username, sessionID); | |
logout(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.UUID; | |
import org.apache.logging.log4j.Logger; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.ThreadContext; | |
public class MyAppStack { | |
private static final Logger logger = LogManager.getLogger(MyAppStack.class); | |
public void login(String username, UUID sessionID) { | |
ThreadContext.push(sessionID.toString()); | |
ThreadContext.push(username); | |
logger.info("User logged in.") | |
} | |
public void logout() { | |
logger.info("Logging out user."); | |
ThreadContext.pop(); | |
ThreadContext.pop(); | |
logger.info("User logged out.") | |
} | |
public static void main(String[] args) { | |
UUID sessionID = java.util.UUID.randomUUID(); | |
String username = "admin"; | |
login(username, sessionID); | |
logout(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment