Skip to content

Commit

Permalink
Logging config (#1682)
Browse files Browse the repository at this point in the history
* Update logback config to remove debug message of HikariCP

* Add --logback-config option to customize logging config.

Co-authored-by: You Yamagata <youy@bg8.so-net.ne.jp>
  • Loading branch information
yoyama and You Yamagata committed Jan 13, 2022
1 parent 011d848 commit 736d92a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
3 changes: 3 additions & 0 deletions digdag-cli/src/main/java/io/digdag/cli/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public abstract class Command
@Parameter(names = {"-l", "--log-level"})
protected String logLevel = "info";

@Parameter(names = {"--logback-config"})
protected String logbackConfigPath = null;

@DynamicParameter(names = "-X")
protected Map<String, String> systemProperties = new HashMap<>();

Expand Down
39 changes: 31 additions & 8 deletions digdag-cli/src/main/java/io/digdag/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@
import io.digdag.core.Environment;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
Expand Down Expand Up @@ -258,7 +261,7 @@ private boolean processCommonOptions(MainOptions mainOpts, Command command)
command.configPath = mainOpts.configPath;
}

configureLogging(command.logLevel, command.logPath);
configureLogging(command.logLevel, command.logPath, command.logbackConfigPath);

for (Map.Entry<String, String> pair : command.systemProperties.entrySet()) {
System.setProperty(pair.getKey(), pair.getValue());
Expand All @@ -267,7 +270,7 @@ private boolean processCommonOptions(MainOptions mainOpts, Command command)
return verbose;
}

private static void configureLogging(String level, String logPath)
private static void configureLogging(String level, String logPath, String logbackConfig)
{
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
Expand All @@ -277,7 +280,25 @@ private static void configureLogging(String level, String logPath)
// logback uses system property to embed variables in XML file
Level lv = Level.toLevel(level.toUpperCase(), Level.DEBUG);
System.setProperty("digdag.log.level", lv.toString());
if (!logPath.equals("-")) {
System.setProperty("digdag.log.path", logPath);
}

try {
if (logbackConfig == null) {
configurator.doConfigure(getLogbackConfigResource(logPath));
}
else {
configurator.doConfigure(getLogbackConfigureFile(logbackConfig));
}
} catch (JoranException | MalformedURLException ex) {
System.err.println(ex);
throw new RuntimeException(ex);
}
}

private static URL getLogbackConfigResource(String logPath)
{
String name;
if (logPath.equals("-")) {
if (System.console() != null) {
Expand All @@ -286,14 +307,15 @@ private static void configureLogging(String level, String logPath)
name = "/digdag/cli/logback-console.xml";
}
} else {
System.setProperty("digdag.log.path", logPath);
name = "/digdag/cli/logback-file.xml";
}
try {
configurator.doConfigure(Main.class.getResource(name));
} catch (JoranException ex) {
throw new RuntimeException(ex);
}
return Main.class.getResource(name);
}

private static File getLogbackConfigureFile(String logbackConfigPath)
throws MalformedURLException
{
return new File(logbackConfigPath);
}

// called also by Run
Expand Down Expand Up @@ -361,6 +383,7 @@ public static void showCommonOptions(Map<String, String> env, PrintStream err)
err.println(" -L, --log PATH output log messages to a file (default: -)");
err.println(" -l, --log-level LEVEL log level (error, warn, info, debug or trace)");
err.println(" -X KEY=VALUE add a performance system config");
err.println(" --logback-config PATH path to logback configuration file (for developers only)");
err.println(" -c, --config PATH.properties Configuration file (default: " + defaultConfigPath(env) + ")");
err.println(" --version show client version");
err.println("");
Expand Down
1 change: 1 addition & 0 deletions digdag-cli/src/main/resources/digdag/cli/logback-file.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<logger name="io.netty.util" level="INFO"/>
<logger name="io.netty.buffer" level="INFO"/>
<logger name="com.zaxxer.hikari" level="INFO"/>

<appender name="digdag-context" class="io.digdag.cli.LogbackTaskContextLoggerBridgeAppender">
</appender>
Expand Down

0 comments on commit 736d92a

Please sign in to comment.