如何在SBT中登录settingKey?

时间:2016-10-17 05:05:45

标签: scala playframework-2.0 sbt

我想在SBT做一些登录。我在初始化streams时试图获得settingKey。然而,编译器抱怨A setting cannot depend on a task

配置代码段在这里。

val appConfig = settingKey[Config]("The parsed application.conf in SBT")

appConfig := {
  // ...
  streams.value.log.error("Cannot find application.conf. Please check if -Dconfig.file/resource is setting correctly.")
  // ...
}

是否有任何方法可以登录settingKey?感谢。

3 个答案:

答案 0 :(得分:2)

设置应仅包含可设置的数据或可从其他设置直接计算的内容。 streams是一项任务,因此您可以根据它和appConfig设置创建另一项任务。例如:

val appConfig = settingKey[File]("application.conf file")
val parsedAppConfig = taskKey[Config]("The parsed application.conf in SBT")

parsedAppConfig := {
  // ...
  parse(appConfig.value)
  // ...
  streams.value.log.error("Cannot find application.conf. Please check if -Dconfig.file/resource is setting correctly.")
  // ...
}

答案 1 :(得分:2)

可以绕过streams并直接创建新的ConsoleLogger()

val appConfig = settingKey[Config]("The parsed application.conf in SBT")

appConfig := {
  // ...
  val logger: Logger = ConsoleLogger()
  logger.error("Cannot find application.conf. Please check if -Dconfig.file/resource is setting correctly.")
  // ...
}

警告:如果将streams.value.log设置为ConsoleLogger以外的其他内容,则您的日志语句将无视并仍然会登录到控制台。尽管如此,这可能是可以接受的。

答案 2 :(得分:1)

从sbt设置登录的正确方法是使用Object.values设置。此设置由sbt设置,并保存sbt Universe中其他设置使用的记录器。

let a = { 
   "lclWarehouseId":11,
   "name":"asdasd",
   "country":{ 
      "id":1,
      "name":"Indonesia"
   },
   "city":{ 
      "id":1,
      "countryId":1,
      "name":"Jakarta"
   },
   "address":"asdasd",
   "phone":"+62123123",
   "fax":"+62123123",
   "monday":{ 
      "isOpen":true,
      "start":"0000-01-01T14:43:20+07:00",
      "end":"0000-01-01T15:43:23+07:00"
   },
   "tuesday":{ 
      "isOpen":false,
      "start":"0000-01-01T00:00:00Z",
      "end":"0000-01-01T00:00:00Z"
   },
   "wednesday":{ 
      "isOpen":false,
      "start":"0000-01-01T00:00:00Z",
      "end":"0000-01-01T00:00:00Z"
   },
   "thursday":{ 
      "isOpen":false,
      "start":"0000-01-01T00:00:00Z",
      "end":"0000-01-01T00:00:00Z"
   },
   "friday":{ 
      "isOpen":false,
      "start":"0000-01-01T00:00:00Z",
      "end":"0000-01-01T00:00:00Z"
   },
   "saturday":{ 
      "isOpen":false,
      "start":"0000-01-01T00:00:00Z",
      "end":"0000-01-01T00:00:00Z"
   },
   "sunday":{ 
      "isOpen":false,
      "start":"0000-01-01T00:00:00Z",
      "end":"0000-01-01T00:00:00Z"
   }
}

const days = ["monday", "tuesday","wednesday", "thursday",  "friday","saturday","sunday" ]
let arr = []
Object.keys(a).forEach(key => {
  if(days.includes(key)){
      arr.push(a[key])
    }
  })
  
  console.log(arr)