2

我将 sbt-native-packager 与实验性 Java Server 原型一起使用。我正在尝试确定一种访问我的日志文件的传统方式,我想知道是否有人知道这里的常用方法。因为我使用的是 Java Server 原型,所以我得到了一个符号链接 /var/log/$app -> install_dir/$app/log,但是仅仅让 log4j 打开 /var/log/$ 感觉有点脏且不那么便携app/error.log 直接。

[更新]

我最终创建了一个带有运行时路径信息的对象:

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("")
  }
}

在我的主要方法中,我基于新的 MakaraPaths 对象建立了一个系统属性:

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

我也将它用于我的配置文件:

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

最终,为了加载日志文件,我使用了系统属性查找:

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

这让我大部分时间都在我需要的地方。它不是完全可移植的,但它在技术上支持我的用例。(部署到 Ubuntu)

4

1 回答 1

2

您可以在 log4j 配置中使用相对路径。只需在logs/filename.log 中写入日志。安装时会创建符号链接 install_dir/$app/logs -> /var/log/$app,所有日志都会写入 /var/log/$app/filename.log

于 2014-01-08T06:31:54.200 回答