Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging config #1682

Merged
merged 2 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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