Exception log practical_coding_guide, 예외와 로그 코딩 실용 가이드

Click here to load reader

download Exception log practical_coding_guide, 예외와 로그 코딩 실용 가이드

of 63

description

예외 처리 가이드(http://www.slideshare.net/dhrim/ss-2804901) 의 후편입니다. 실제적인 예를 가지고 예외와 로그를 어떻게 처리해 하는지 설명합니다.

Transcript of Exception log practical_coding_guide, 예외와 로그 코딩 실용 가이드

  • 1. , ? ? ? ?

2. 3. ? 4. 5. ? 6. ? 7. ? 8. ? 9. ? 10. ? 11. . . 12. , 13. . . 14. . . 15. . 16. 2. . 17. ? 18. ? . JVM, OS Framework . JVM, OS, Framework 19. REST Exception Mapper Http Status . . . Exception Mapper Scheduling Task REST type interface 20. exception mapper HTTP . Exception Class HTTP Response Status UserNotFoundException BAD_REQUEST(400) InvalidParameterException BAD_REQUEST(400) WebApplicatoinExceptoin(by Jersey) NOT_FOUND(404) Throwable INTERNAL_SERVER_ERROR(500) 21. @Provider public class InvalidParameterExceptionMapper implements ExceptionMapper { @Override public Response toResponse(InvalidParameterException exception) { logger.debug(request of invalid parameter., exception); return Response.status(Status.BAD_REQUEST).build(); } } InvalidParameterException BAD_REQUEST(400) . 22. @Provider public class DefaultExceptionMapper implements ExceptionMapper { @Override public Response toResponse(Throwable exception) { logger.error(unhandled exception., exception); return Response.status(Status.INTERNAL_SYSTEM_ERROR).build(); } } exception mapper Throwable INTERNAL_SYSTEM_ERROR(500) . 23. @Service public class SomeScheduledTask { @Scheduled public void process() { try { } catch(SomeException e) { logger.warn(...); } catch(Throwable e) { logger.error(...); } } } process() Spring . Throwable . 24. Info : . debug : . warn : , . error : , . fatal : , . 25. 8080 . . DB url . some.properties /some/path . 26. . . . 27. 10% . . 28. default 29. . null null . 30. . NullPointerException. RuntimeException. . 31. warn . debug . . 32. NullPointerException bug. JVM NPE thread . warn, fatal error. 33. RuntimeException JVM thread . fatal. 34. . . . 35. . . REST . id password . id , password . . 36. . . . 5 . timeout . . id . password . 37. . SystemFailException (extends RuntimeException) Exception Handler FATAL . AuthSystemFailException (extends RuntimeException) Exception Handler ERROR 500 . AuthSystemFailException (extends RuntimeException) Exception Handler ERROR 500 5 . . WARN timeout . . WARN . AuthSystemFailException (extends RuntimeException) Exception Handler ERROR 500 id . AuthFailException . InvalidInputException . DEBUG 400 password . AuthFailException DEBUG 400 38. or or . loading auth server info failed. properties file=/some/path/. . connecting auth server failed. connection info=http://some.url . getting response from auth server failed. connection info=. 5 . got response from auth server, but too long response time. time=10.3 sec. timeout . get response after 1 timeout. connection info= . parsing message failed. message= id . AuthFailException NOT_EXIST_ID . 400 NOT_EXIST_ID . password . AuthFailException INCORRECT_PASSWORD . 400 INCORRECT_PASSWORD . 39. 3 . SystemFailException AuthSystemFailException . . AuthFailException . 40. . SystemFailException FATAL AuthSystemFailException ERROR 500 RuntimeException ERROR 500. . . 41. try { authToken = authManager.getAuthToken(id, password); } catch(AuthFailException e) { throw new InvalidInputException("invalid auth info", e.getErrorCode(), e); } AuthFailException . RuntimeException . getErrorCode() . 42. . RuntimeException . FATAL ERROR SystemFailException AuthSystemFailException . ERROR . getErrorCode() AuthFailException . 43. RuntimeException stack . exception handler . exception handler . 44. DEBUG . . logback . ( log4j .) JMX . 45. debug . , debug . . 46. . Logger logger = LogManager.getLogger(SomeDao.class); . . 47. 2 . String (by SLF4J) (by LogBack) 48. String logger.debug(userId=+userId); logger.debug(userId={}, userId); SLF4J . . 49. Log4J ( ) 50. default logback.xml log4j.xml jar , . , . 51. Java JUL(Java Util Logging) java.util.logging.LogManager.getLogManager().reset(); org.slf4j.bridge.SLF4JBridgeHandler.install(); java.util.logging.Logger.getLogger("").setLevel(java.util.logging.Level.FINEST); refer http://www.slf4j.org/legacy.html#jul-to-slf4j 52. Apache JCL(Java Commons Logging) commons-logging.jar jcl-over-slf4j.jar refer http://www.slf4j.org/legacy.html#jcl-over-slf4j 53. System.out.println() . . . , . . logger.debug(userId); // . logger.debug(userId={}, userId); 54. . logger.debug(userId={}, request.getUserId()); // . . logger.debug(request={}, request); toString() . 55. Apache commons ToStringBuilder @Override public String toString() { return ToStringBuilder.reflectionToString(this, ); } 56. . stacktrace . debug . 57. logger.debug("request={}", request); logger.debug("context={}", context); logger.debug("binder={}", binder); . multi thread . logger.debug("request={}, context={}, binder={}", request, context, binder);