Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 7, 2023 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/dc2e71b116d97bc5a29777e33950214f to your computer and use it in GitHub Desktop.
Save dacr/dc2e71b116d97bc5a29777e33950214f to your computer and use it in GitHub Desktop.
Application random logs generator, with multi-line entries / published by https://github.com/dacr/code-examples-manager #22c428d2-e5da-4d3a-b02a-cbf430bda590/dc0cfeebb0a6b0f32deae20cb27feca43bf55487
// summary : Application random logs generator, with multi-line entries
// keywords : scala, logs, generator, logback
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 22c428d2-e5da-4d3a-b02a-cbf430bda590
// created-on : 2020-05-31T19:54:52Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.2.2"
//> using dep "ch.qos.logback:logback-classic:1.4.7"
// ---------------------
import org.slf4j._
import ch.qos.logback.classic
object LogBackHelpers {
def configureLogBack(xmlConfig:String):Unit = {
val loggerContext = LoggerFactory.getILoggerFactory.asInstanceOf[classic.LoggerContext]
loggerContext.reset()
val configurator = new classic.joran.JoranConfigurator()
val configStream = new java.io.ByteArrayInputStream(xmlConfig.getBytes)
configurator.setContext(loggerContext)
configurator.doConfigure(configStream)
}
def getRootLogger():classic.Logger = {
LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME).asInstanceOf[classic.Logger]
}
def getLogger(name:String):Logger = {
LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)
}
}
val logConfig =
"""<?xml version="1.0" encoding="UTF-8"?>
|<configuration
| scan="false"
| scanPeriod="45 seconds"
| xmlns="http://ch.qos.logback/xml/ns/logback"
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
| <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
| <file>${log.application.output:-application.log}</file>
| <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
| <fileNamePattern>${log.application.output:-application.log}.%i</fileNamePattern>
| <minIndex>1</minIndex>
| <maxIndex>10</maxIndex>
| </rollingPolicy>
| <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
| <maxFileSize>20MB</maxFileSize>
| </triggeringPolicy>
| <encoder >
| <pattern>%date %level [%thread] %logger{10} %file:%line %msg%n</pattern>
| </encoder>
| </appender>
|
| <root level="${log.root.level:-DEBUG}">
| <appender-ref ref="FILE"/>
| </root>
|
|</configuration>
|""".stripMargin
import LogBackHelpers._
configureLogBack(logConfig)
val mainLogger = getLogger("main")
mainLogger.info("Application starts")
mainLogger.info("Application ends")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment