Experimenting with programmatic log levels.

This commit is contained in:
JesseBrault0709 2023-01-08 10:05:13 -06:00
parent d856f194ff
commit 09c174415b
3 changed files with 54 additions and 8 deletions

View File

@ -13,17 +13,20 @@ dependencies {
// https://mvnrepository.com/artifact/org.apache.groovy/groovy
implementation 'org.apache.groovy:groovy:4.0.7'
// https://mvnrepository.com/artifact/info.picocli/picocli
implementation 'info.picocli:picocli:4.7.0'
/**
* Logging
*/
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
implementation 'org.slf4j:slf4j-api:1.7.36'
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api
implementation 'org.apache.logging.log4j:log4j-api:2.19.0'
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl
runtimeOnly 'org.apache.logging.log4j:log4j-slf4j-impl:2.19.0'
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
runtimeOnly 'org.apache.logging.log4j:log4j-core:2.19.0'
implementation 'org.apache.logging.log4j:log4j-core:2.19.0'
}
application {

View File

@ -14,14 +14,56 @@ import com.jessebrault.ssg.text.MarkdownFrontMatterGetter
import com.jessebrault.ssg.text.MarkdownTextRenderer
import com.jessebrault.ssg.text.TextFileTextsProvider
import com.jessebrault.ssg.text.TextType
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.apache.logging.log4j.core.config.Configuration
import org.apache.logging.log4j.spi.LoggerContext
import picocli.CommandLine
class StaticSiteGeneratorCli {
import java.util.concurrent.Callable
private static final Logger logger = LoggerFactory.getLogger(StaticSiteGeneratorCli)
@CommandLine.Command(
name = 'ssg',
mixinStandardHelpOptions = true,
version = '0.0.1-SNAPSHOT',
description = 'Generates a set of html files from a given configuration.'
)
class StaticSiteGeneratorCli implements Callable<Integer> {
private static final Logger logger = LogManager.getLogger(StaticSiteGeneratorCli)
static class LogLevel {
@CommandLine.Option(names = ['--info']) boolean info
@CommandLine.Option(names = ['--debug']) boolean debug
@CommandLine.Option(names = ['--trace']) boolean trace
}
static void main(String[] args) {
System.exit(new CommandLine(StaticSiteGeneratorCli).execute(args))
}
@CommandLine.ArgGroup(exclusive = true)
private LogLevel logLevel
@Override
Integer call() throws Exception {
def context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false)
def configuration = context.getConfiguration()
def rootLoggerConfig = configuration.getRootLogger()
if (this.logLevel.info) {
rootLoggerConfig.level = Level.INFO
} else if (this.logLevel.debug) {
rootLoggerConfig.level = Level.DEBUG
} else if (this.logLevel.trace) {
rootLoggerConfig.level = Level.TRACE
} else {
rootLoggerConfig.level = Level.WARN
}
context.updateLoggers()
def markdownText = new TextType(['.md'], new MarkdownTextRenderer(), new MarkdownFrontMatterGetter())
def gspTemplate = new TemplateType(['.gsp'], new GspTemplateRenderer())
def gspPart = new PartType(['.gsp'], new GspPartRenderer())
@ -51,6 +93,7 @@ class StaticSiteGeneratorCli {
def ssg = new SimpleStaticSiteGenerator(config)
ssg.generate(new File('build'), globals)
return 0
}
}

View File

@ -10,7 +10,7 @@
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<Root level="warn">
<AppenderRef ref="standard" />
</Root>
</Loggers>