Advertisement
Guest User

log4j configuration

a guest
Feb 20th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.37 KB | None | 0 0
  1.         val ctx = LoggerContext.getContext(false)
  2.         val root = ctx.rootLogger
  3.         val cfg = ctx.configuration
  4.  
  5.         val layout = PatternLayout.newBuilder()
  6.             .withPattern("[%d{HH:mm:ss}] [%25.25t] [%level]: %msg%n")
  7.             .withCharset(StandardCharsets.UTF_8)
  8.             .withAlwaysWriteExceptions(true)
  9.             .withConfiguration(cfg)
  10.             .build()
  11.  
  12.         val policy = OnStartupTriggeringPolicy.createPolicy(0)
  13.  
  14.         val strategy = DefaultRolloverStrategy.newBuilder()
  15.             .withMax("10")
  16.             .withMin("1")
  17.             .withFileIndex("min")
  18.             .withConfig(cfg)
  19.             .build()
  20.  
  21.         val fileAppender = (
  22.                 RollingFileAppender.newBuilder<RollingFileAppenderBuilder>() as RollingFileAppender.Builder<RollingFileAppenderBuilder>
  23.         ).apply {
  24.             name = "File"
  25.             configuration = cfg
  26.             setLayout(layout)
  27.  
  28.             withFileName(File("latest.log").absolutePath.toString())
  29.             withFilePattern(File("latestn.log").absolutePath.toString() + ".%i")
  30.             withAppend(true)
  31.             withImmediateFlush(true)
  32.             withPolicy(policy)
  33.             withStrategy(strategy)
  34.         }.build()
  35.  
  36.         fileAppender.start()
  37.  
  38.         cfg.addAppender(fileAppender)
  39.         root.addAppender(fileAppender)
  40.  
  41.         root.info("test")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement