How to create predictable logging locations with sbt-native-packager

无人久伴 提交于 2019-12-12 17:16:50

问题


I am using sbt-native-packager with the experimental Java Server archetype. I am trying to identify a conventional way to access my log files, and I'm wondering if anyone knows of a common approach here. Since I am using the Java Server archetype, I am getting a symlink /var/log/$app -> install_dir/$app/log, but it feels a little dirty and less portable to just have log4j open /var/log/$app/error.log directly.

[Update]

I ended up creating an object with run time path information:

object MakaraPaths {
  def getLogPath = new File(getJarPath, "../logs").getPath
  def getConfigPath = new File(getJarPath, "../conf").getPath

  def getJarPath = { 
    val regex = new Regex("(/.+/)*")
    val jarPath = Makara.getClass.getProtectionDomain.getCodeSource.getLocation.getPath

    (regex findAllIn jarPath).mkString("")
  }
}

In my main method, I established a system property based on the new MakaraPaths object:

System.setProperty("logPath", MakaraPaths.getLogPath)

I also used this for my config file:

val config = ConfigFactory.parseFile(new File(MakaraPaths.getConfigPath, "application.conf"))

Ultimately, to load the log file, I used a System Property lookup:

<RollingFile name="fileAppender" fileName="${sys:logPath}/server.log" filePattern="${sys:logPath}/server_%d{yyMMdd}.log">

This gets me most of the way where I needed to be. It's not completely portable, but it does technically support my use case. (Deploying to Ubuntu)


回答1:


You could use relative path in log4j configuration. Just write logs in logs/filename.log. During installation symlink install_dir/$app/logs -> /var/log/$app will be created, and all logs will be written in /var/log/$app/filename.log



来源:https://stackoverflow.com/questions/20982161/how-to-create-predictable-logging-locations-with-sbt-native-packager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!