Guest User

Untitled

a guest
Jan 21st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. spring boot에서
  2. ~~~xml
  3. <springProfile name="fileLog">
  4. <appender name="consoleAppender" class="ch.qos.logback.core.helpers.NOPAppender" />
  5. <appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
  6. <file>${LOG_FILE}</file>
  7. <encoder>
  8. <pattern>${FILE_PATTERN}</pattern>
  9. </encoder>
  10. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  11. <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}</fileNamePattern>
  12. <maxHistory>5</maxHistory>
  13. </rollingPolicy>
  14. </appender>
  15. </springProfile>
  16. ~~~
  17. 이런식으로 rolling을 지정해줄 수 있다. 어떻게 rolling이 되는 걸까?
  18.  
  19. 우선 두가지를 봐야하는데, 현재 위의 logback configuration에서는 RollingFileAppender를 사용하며, Rolling Policy로 TimeBasedRollingPolicy를 사용한다.
  20. RollingFileAppender의
  21. ~~~java
  22. public void rollover() {
  23. lock.lock();
  24. try {
  25. // Note: This method needs to be synchronized because it needs exclusive
  26. // access while it closes and then re-opens the target file.
  27. //
  28. // make sure to close the hereto active log file! Renaming under windows
  29. // does not work for open files.
  30. this.closeOutputStream();
  31. attemptRollover();
  32. attemptOpenFile();
  33. } finally {
  34. lock.unlock();
  35. }
  36. }
  37. ~~~
  38. 를 확인해보면 ```attemptRollover()```라는 함수가 있다.
  39. ~~~java
  40. private void attemptRollover() {
  41. try {
  42. rollingPolicy.rollover();
  43. } catch (RolloverFailure rf) {
  44. addWarn("RolloverFailure occurred. Deferring roll-over.");
  45. // we failed to roll-over, let us not truncate and risk data loss
  46. this.append = true;
  47. }
  48. }
  49. ~~~
  50. 를 보면 rollingPolicy의 rollover함수를 실행시키고, TimeBasedRollingPolicy에서 이를 확인해보면
  51. ~~~java
  52. public void rollover() throws RolloverFailure {
  53.  
  54. // when rollover is called the elapsed period's file has
  55. // been already closed. This is a working assumption of this method.
  56.  
  57. String elapsedPeriodsFileName = timeBasedFileNamingAndTriggeringPolicy.getElapsedPeriodsFileName();
  58.  
  59. String elapsedPeriodStem = FileFilterUtil.afterLastSlash(elapsedPeriodsFileName);
  60.  
  61. if (compressionMode == CompressionMode.NONE) {
  62. if (getParentsRawFileProperty() != null) {
  63. renameUtil.rename(getParentsRawFileProperty(), elapsedPeriodsFileName);
  64. } // else { nothing to do if CompressionMode == NONE and parentsRawFileProperty == null }
  65. } else {
  66. if (getParentsRawFileProperty() == null) {
  67. compressionFuture = compressor.asyncCompress(elapsedPeriodsFileName, elapsedPeriodsFileName, elapsedPeriodStem);
  68. } else {
  69. compressionFuture = renameRawAndAsyncCompress(elapsedPeriodsFileName, elapsedPeriodStem);
  70. }
  71. }
  72.  
  73. if (archiveRemover != null) {
  74. Date now = new Date(timeBasedFileNamingAndTriggeringPolicy.getCurrentTime());
  75. this.cleanUpFuture = archiveRemover.cleanAsynchronously(now);
  76. }
  77. }
  78. ~~~
  79. rename을 하는 것을 확인할 수 있다. 그리고 다시 ```rollover```의 ```attemptOpenFile```를 확인해보면
  80. ~~~java
  81. private void attemptOpenFile() {
  82. try {
  83. // update the currentlyActiveFile LOGBACK-64
  84. currentlyActiveFile = new File(rollingPolicy.getActiveFileName());
  85.  
  86. // This will also close the file. This is OK since multiple close operations are safe.
  87. this.openFile(rollingPolicy.getActiveFileName());
  88. } catch (IOException e) {
  89. addError("setFile(" + fileName + ", false) call failed.", e);
  90. }
  91. }
  92. ~~~
  93. 새롭게 activeFileName을 이용하여 file을 생성함을 알 수 있다.
Add Comment
Please, Sign In to add comment