在运行时配置 log4j

发布于 2024-10-09 14:05:47 字数 408 浏览 0 评论 0原文

我正在使用 org.apache.tools.ant.listener.Log4jListener 来管理我的 ant 脚本的日志记录。 ant 脚本是高度可配置的,并且设计为使用不同的参数以不同的方式运行,因此我需要能够记录到运行时指定的文件。我有一个 log4j.properties ,它指定一个日志文件为 build.log ,尽管我尝试启动 ant 重新定义 log4j.properties 中定义的属性,但没有成功。

构建会忽略它们并继续写入 build.log。我还没有找到关于写入自定义文件的太多支持,除非它是在 Java 中的 Logger 类。

也许我的想法是错误的。 log4j.properties 的处理方式与 ant 脚本中的属性文件不同(因此可以从命令行覆盖)?有没有一种方法可以让我在不编写自定义任务或其他内容的情况下智能地完成此操作?

I'm using org.apache.tools.ant.listener.Log4jListener to manage logging with my ant script. The ant script is highly configurable and designed to be run different ways with different parameters and therefore I need to be able to log to files specified at runtime. I have a log4j.properties which specifies a log file to be build.log, and despite my attempts to launch ant redefining properties defined in log4j.properties have been unsuccessful.

The build ignores them and continues to write to build.log. I haven't found much support regarding writing to custom files unless it's in Java with their Logger class.

Perhaps I'm thinking this through wrong. log4j.properties isn't treated in the same way as a property file in an ant script (hence overrideable from the command line)? Is there a way I can do this intelligently without writing a custom task or something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼前雾蒙蒙 2024-10-16 14:05:47

您可以使用可以在命令行上动态定义的系统属性来设置 log4j.properties 文件。下面的属性是“${logfile.name}”。 log4j 配置示例如下:

# logfile is set to be a RollingFileAppender
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${logfile.name}
log4j.appender.logfile.MaxFileSize=10MB
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=[%-5p]%d{yyyyMMdd@HH\:mm\:ss,SSS}\:%c - %m%n

调用“ant”时传递属性的命令行选项是“-Dlogfile.name={运行时路径/日志文件的文件名}”。将 {运行时路径/日志文件的文件名} 替换为您的文件名。当 ant 运行时,该值被设置为系统属性。然后,该系统属性会在运行时替换到 log4j.properties 中。

http://ant.apache.org/manual/running.html

You setup your log4j.properties file using a system property that you can define dynamically on the command line. The property below is "${logfile.name}". An example log4j configuration would be like this:

# logfile is set to be a RollingFileAppender
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${logfile.name}
log4j.appender.logfile.MaxFileSize=10MB
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=[%-5p]%d{yyyyMMdd@HH\:mm\:ss,SSS}\:%c - %m%n

The command line option to pass a property, when calling "ant", is "-Dlogfile.name={runtime path/filename of log file}". Replace {runtime path/filename of log file} with your file name. When ant is run this value is set as a system property. That system property is then substituted into the log4j.properties at runtime.

http://ant.apache.org/manual/running.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文