Config.kt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package com.dolin.zap.entity
  2. /**
  3. * @author #Suyghur.
  4. * Created on 4/7/21
  5. */
  6. class Config private constructor(builder: Builder) {
  7. var logDir = ""
  8. private set
  9. var tag = ""
  10. private set
  11. var logcatLevel: Level
  12. private set
  13. var recordLevel: Level
  14. private set
  15. var recordEnable: Boolean
  16. private set
  17. var compressEnable: Boolean
  18. private set
  19. var overdueDayMs = 0L
  20. private set
  21. private var fileSizeLimitDayByte = 0
  22. init {
  23. this.logDir = builder.logDir
  24. this.tag = builder.tag
  25. this.logcatLevel = builder.logcatLevel
  26. this.recordLevel = builder.recordLevel
  27. this.recordEnable = builder.recordEnable
  28. this.compressEnable = builder.compressEnable
  29. this.overdueDayMs = builder.overdueDay * 24 * 3600 * 1000L
  30. this.fileSizeLimitDayByte = builder.fileSizeLimitDay * 1024 * 1024
  31. }
  32. override fun toString(): String {
  33. return "Config{" +
  34. "logDir='" + logDir + '\'' +
  35. ", logcatLevel=" + logcatLevel +
  36. ", recordLevel=" + recordLevel +
  37. ", overdueDayMs=" + overdueDayMs +
  38. ", fileSizeLimitDayByte=" + fileSizeLimitDayByte +
  39. '}'
  40. }
  41. class Builder {
  42. internal var logDir = ""
  43. internal var tag = ""
  44. internal var logcatLevel = Level.DEBUG
  45. internal var recordLevel = Level.DEBUG
  46. internal var recordEnable = true
  47. internal var compressEnable = true
  48. internal var overdueDay = 3
  49. internal var fileSizeLimitDay = 15
  50. /**
  51. * 日志存储路径,默认是应用的私有目录下dolin文件夹
  52. */
  53. fun setLogDir(logDir: String): Builder {
  54. this.logDir = logDir
  55. return this
  56. }
  57. fun setDefaultTag(tag: String): Builder {
  58. this.tag = tag
  59. return this
  60. }
  61. /**
  62. * 允许输出到logcat的日志的最低级别,默认为[Level.DEBUG]级别
  63. */
  64. fun setLogcatLevel(level: Level): Builder {
  65. this.logcatLevel = level
  66. return this
  67. }
  68. /**
  69. * 允许记录到文件的日志的最低级别, 默认为[Level.DEBUG]级别
  70. */
  71. fun setRecordLevel(level: Level): Builder {
  72. this.recordLevel = level
  73. return this
  74. }
  75. fun setRecordEnable(enable: Boolean): Builder {
  76. this.recordEnable = enable
  77. return this
  78. }
  79. fun setRecordCompressEnable(enable: Boolean): Builder {
  80. this.compressEnable = enable
  81. return this
  82. }
  83. /**
  84. * 日志过期天数,过期日志将自动清除,默认为3
  85. */
  86. fun setOverdueDay(overdueDay: Int): Builder {
  87. this.overdueDay = overdueDay
  88. return this
  89. }
  90. /**
  91. * 单天日志文件存储上限,单位 MB,默认为15
  92. */
  93. fun setFileSizeLimitDay(mb: Int): Builder {
  94. this.fileSizeLimitDay = mb
  95. return this
  96. }
  97. fun create(): Config {
  98. return Config(this)
  99. }
  100. }
  101. }