From 09c174415bd21e6268c0940cde050ce57b58d40b Mon Sep 17 00:00:00 2001 From: JesseBrault0709 <62299747+JesseBrault0709@users.noreply.github.com> Date: Sun, 8 Jan 2023 10:05:13 -0600 Subject: [PATCH] Experimenting with programmatic log levels. --- cli/build.gradle | 9 ++-- .../ssg/StaticSiteGeneratorCli.groovy | 51 +++++++++++++++++-- cli/src/main/resources/log4j2.xml | 2 +- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/cli/build.gradle b/cli/build.gradle index 9c84f9c..7ead005 100644 --- a/cli/build.gradle +++ b/cli/build.gradle @@ -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 { diff --git a/cli/src/main/groovy/com/jessebrault/ssg/StaticSiteGeneratorCli.groovy b/cli/src/main/groovy/com/jessebrault/ssg/StaticSiteGeneratorCli.groovy index 2af63b3..b8ed7eb 100644 --- a/cli/src/main/groovy/com/jessebrault/ssg/StaticSiteGeneratorCli.groovy +++ b/cli/src/main/groovy/com/jessebrault/ssg/StaticSiteGeneratorCli.groovy @@ -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 { + + 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 } } diff --git a/cli/src/main/resources/log4j2.xml b/cli/src/main/resources/log4j2.xml index 47819a7..79c3e7d 100644 --- a/cli/src/main/resources/log4j2.xml +++ b/cli/src/main/resources/log4j2.xml @@ -10,7 +10,7 @@ - +