Last active
May 25, 2024 10:18
-
-
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/5d6afccfae4409a7d2fef098a281d9d6974143f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.4.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