Last active
July 18, 2019 03:42
-
-
Save MonikaMahanthappa/15660dc3e07d12b87109e4fb4637f7e2 to your computer and use it in GitHub Desktop.
Logging examples
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
//Developer friendly | |
{ "sender" : "michael", | |
"recipient": { | |
"name" : "michael", | |
"name" : "andrea", | |
"name" : "itay" | |
} | |
subject: "I heart logs" | |
} | |
//Create logger | |
Logger logger = Logger.getLogger(MyClass.class.getName()); | |
logger.trace("Number is {}", getRandomNumber()); | |
//Traditional, Guarded Logging | |
if (logger.isTraceEnabled()) { | |
logger.trace("Number is {}", getRandomNumer()); | |
} | |
//Parameterized Logging | |
logger.debug("PARAMETERIZED: {}", slow); | |
logger.debug("{}", expensiveOperation()); | |
//The lambda expression is only evaluated if the corresponding log level is enabled. or lazy logging | |
logger.trace("Number is {}", () -> getRandomNumber()); | |
{ | |
"@timestamp": "2017-07025 17:02:12", | |
"level": "error", | |
"message": "connection refused", | |
"service": "listener", | |
"thread": "125", | |
"customerid": "776622", | |
"ip": "34.124.233.12", | |
"queryid": "45" | |
} | |
//Formatting | |
{ | |
"@timestamp": "2017-07025 17:02:12", | |
"level": "error", | |
"message": "connection refused", | |
"service": "listener", | |
"thread": "125", | |
"customerid": "776622", | |
"ip": "34.124.233.12", | |
"queryid": "45" | |
} | |
//Provide context | |
12-19-17 13:40:42:000 login failed. | |
12-19-17 13:40:42:000 userId=23 action=login status=failure |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment