From 93b3391167aaa3f3ad2c5409d945c5696167e6ed Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Fri, 1 Dec 2017 21:26:34 -0500 Subject: [PATCH] Fixes server log hardcoded to debug level Fixes #3786 To configure the log level of the server, this introduces a new task key named `serverLog`. The idea is to set this using `Global / serverLog / logLevel`. It will also check the global log level, and if all else fails, fallback to Warn. ``` lazy val level: Level.Value = (s get serverLogLevel) orElse (s get logLevel) match { case Some(x) => x case None => Level.Warn } ``` --- main-command/src/main/scala/sbt/BasicKeys.scala | 9 +++++++++ main/src/main/scala/sbt/Keys.scala | 3 ++- main/src/main/scala/sbt/Project.scala | 7 ++++++- .../scala/sbt/internal/CommandExchange.scala | 16 +++++++++++++--- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/main-command/src/main/scala/sbt/BasicKeys.scala b/main-command/src/main/scala/sbt/BasicKeys.scala index 42b68daa4..86dd9343f 100644 --- a/main-command/src/main/scala/sbt/BasicKeys.scala +++ b/main-command/src/main/scala/sbt/BasicKeys.scala @@ -11,6 +11,7 @@ import java.io.File import sbt.internal.util.AttributeKey import sbt.internal.inc.classpath.ClassLoaderCache import sbt.librarymanagement.ModuleID +import sbt.util.Level object BasicKeys { val historyPath = AttributeKey[Option[File]]( @@ -38,6 +39,14 @@ object BasicKeys { "The wire protocol for the server command.", 10000) + // Unlike other BasicKeys, this is not used directly as a setting key, + // and severLog / logLevel is used instead. + private[sbt] val serverLogLevel = + AttributeKey[Level.Value]("serverLogLevel", "The log level for the server.", 10000) + + private[sbt] val logLevel = + AttributeKey[Level.Value]("logLevel", "The amount of logging sent to the screen.", 10) + private[sbt] val interactive = AttributeKey[Boolean]( "interactive", "True if commands are currently being entered from an interactive environment.", diff --git a/main/src/main/scala/sbt/Keys.scala b/main/src/main/scala/sbt/Keys.scala index 3df25679d..bfcb3139e 100644 --- a/main/src/main/scala/sbt/Keys.scala +++ b/main/src/main/scala/sbt/Keys.scala @@ -94,7 +94,7 @@ object Keys { val TraceValues = "-1 to disable, 0 for up to the first sbt frame, or a positive number to set the maximum number of frames shown." // logging - val logLevel = settingKey[Level.Value]("The amount of logging sent to the screen.").withRank(ASetting) + val logLevel = SettingKey(BasicKeys.logLevel).withRank(ASetting) val persistLogLevel = settingKey[Level.Value]("The amount of logging sent to a file for persistence.").withRank(CSetting) val traceLevel = settingKey[Int]("The amount of a stack trace displayed. " + TraceValues).withRank(ASetting) val persistTraceLevel = settingKey[Int]("The amount of stack trace persisted.").withRank(CSetting) @@ -105,6 +105,7 @@ object Keys { val logManager = settingKey[LogManager]("The log manager, which creates Loggers for different contexts.").withRank(DSetting) val logBuffered = settingKey[Boolean]("True if logging should be buffered until work completes.").withRank(CSetting) val sLog = settingKey[Logger]("Logger usable by settings during project loading.").withRank(CSetting) + val serverLog = taskKey[Unit]("A dummy task to set server log level using Global / serverLog / logLevel.").withRank(CTask) // Project keys val autoGeneratedProject = settingKey[Boolean]("If it exists, represents that the project (and name) were automatically created, rather than user specified.").withRank(DSetting) diff --git a/main/src/main/scala/sbt/Project.scala b/main/src/main/scala/sbt/Project.scala index 680bba811..fd175f6c6 100755 --- a/main/src/main/scala/sbt/Project.scala +++ b/main/src/main/scala/sbt/Project.scala @@ -11,6 +11,7 @@ import java.io.File import java.net.URI import java.util.Locale import Project._ +import BasicKeys.serverLogLevel import Keys.{ stateBuildStructure, commands, @@ -21,9 +22,11 @@ import Keys.{ shellPrompt, templateResolverInfos, serverHost, + serverLog, serverPort, serverAuthentication, serverConnectionType, + logLevel, watch } import Scope.{ Global, ThisScope } @@ -41,7 +44,7 @@ import sbt.internal.util.{ AttributeKey, AttributeMap, Dag, Relation, Settings, import sbt.internal.util.Types.{ const, idFun } import sbt.internal.util.complete.DefaultParsers import sbt.librarymanagement.Configuration -import sbt.util.Show +import sbt.util.{ Show, Level } import sjsonnew.JsonFormat import language.experimental.macros @@ -463,6 +466,7 @@ object Project extends ProjectExtra { val port: Option[Int] = get(serverPort) val authentication: Option[Set[ServerAuthentication]] = get(serverAuthentication) val connectionType: Option[ConnectionType] = get(serverConnectionType) + val srvLogLevel: Option[Level.Value] = (logLevel in (ref, serverLog)).get(structure.data) val commandDefs = allCommands.distinct.flatten[Command].map(_ tag (projectCommand, true)) val newDefinedCommands = commandDefs ++ BasicCommands.removeTagged(s.definedCommands, projectCommand) @@ -477,6 +481,7 @@ object Project extends ProjectExtra { .put(historyPath.key, history) .put(templateResolverInfos.key, trs) .setCond(shellPrompt.key, prompt) + .setCond(serverLogLevel, srvLogLevel) s.copy( attributes = newAttrs, definedCommands = newDefinedCommands diff --git a/main/src/main/scala/sbt/internal/CommandExchange.scala b/main/src/main/scala/sbt/internal/CommandExchange.scala index 7bbb2c38b..476ea5f76 100644 --- a/main/src/main/scala/sbt/internal/CommandExchange.scala +++ b/main/src/main/scala/sbt/internal/CommandExchange.scala @@ -13,7 +13,14 @@ import java.util.concurrent.ConcurrentLinkedQueue import java.util.concurrent.atomic.AtomicInteger import scala.collection.mutable.ListBuffer import scala.annotation.tailrec -import BasicKeys.{ serverHost, serverPort, serverAuthentication, serverConnectionType } +import BasicKeys.{ + serverHost, + serverPort, + serverAuthentication, + serverConnectionType, + serverLogLevel, + logLevel +} import java.net.Socket import sjsonnew.JsonFormat import sjsonnew.shaded.scalajson.ast.unsafe._ @@ -106,7 +113,10 @@ private[sbt] final class CommandExchange { case Some(x) => x case None => ConnectionType.Tcp } - val serverLogLevel: Level.Value = Level.Debug + lazy val level: Level.Value = (s get serverLogLevel) + .orElse(s get logLevel) + .getOrElse(Level.Warn) + def onIncomingSocket(socket: Socket, instance: ServerInstance): Unit = { val name = newNetworkName s.log.info(s"new client connected: $name") @@ -114,7 +124,7 @@ private[sbt] final class CommandExchange { val log = LogExchange.logger(name, None, None) LogExchange.unbindLoggerAppenders(name) val appender = MainAppender.defaultScreen(s.globalLogging.console) - LogExchange.bindLoggerAppenders(name, List(appender -> serverLogLevel)) + LogExchange.bindLoggerAppenders(name, List(appender -> level)) log } val channel =