eclipse 如何在Tomcat中配置另一个log4j配置文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25497678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:54:51  来源:igfitidea点击:

How to configure another log4j configuration file in Tomcat?

javaeclipselogginglog4j

提问by Jaskey

I am using log4j1.2, and I am new to log4j.

我正在使用 log4j1.2,我是 log4j 的新手。

I hope that when I am debugging in my local machine ,the logs will be printed to console. But when it is running in PROD , it just logs to file, since I hope to have the least overhead.

我希望当我在本地机器上调试时,日志会打印到控制台。但是当它在 PROD 中运行时,它只是记录到文件中,因为我希望有最少的开销。

I hope that I can achieve this(only print to console when in local machine rather than the prod env) without modifying the properties file every time when I deploy.

我希望我可以实现这一点(仅在本地机器而不是 prod 环境中打印到控制台),而无需每次部署时修改属性文件。

Here is what my log4j.properties looks like:

这是我的 log4j.properties 的样子:

log4j.rootLogger=DEBUG,BAE,console,error


##################production logs###############
log4j.appender.BAE=org.apache.log4j.FileAppender
log4j.appender.BAE.encoding=utf-8
log4j.appender.BAE.Append=true
log4j.appender.BAE.Threshold=INFO
log4j.appender.BAE.layout=org.apache.log4j.SimpleLayout


####################Conole logs####################
##(hope only enable when debuging in local machine)
log4j.appender.console= org.apache.log4j.ConsoleAppender
log4j.appender.console.encoding=utf-8
## DEBUG logs
log4j.appender.console.Threshold = Trace 
log4j.appender.console.layout=org.apache.log4j.SimpleLayout


##################error logs##########################
log4j.appender.error=org.apache.log4j.FileAppender
log4j.appender.error.File=/home/bae/log/error.log 
log4j.appender.error.Threshold = ERROR  
log4j.appender.error.Append=true
log4j.appender.error.layout=org.apache.log4j.SimpleLayout

回答by Paul Vargas

You can configure another log4j configuration file in your environment in eclipse:

您可以在 eclipse 中的环境中配置另一个 log4j 配置文件:

  • First, locate your Tomcat server in eclipse (servers tab) and double-click:
  • 首先,在 eclipse(服务器选项卡)中找到您的 Tomcat 服务器并双击:

Servers

服务器

  • Next, click in Open lauch configuration:
  • 接下来,单击Open lauch configuration

Tomcat configuration

Tomcat配置

  • Add the log4j configuration argument in Tomcat and Apply.
  • 在 Tomcat 和 Apply 中添加 log4j 配置参数。

JVM arguments

JVM 参数

Enjoy!

享受!



See more in Apache log4j 1.2 - Short introduction to log4j

Apache log4j 1.2 中查看更多信息- log4j 的简短介绍

回答by user5285742

If you do not have the log4j file in your path, you'll need to add something like this:

如果您的路径中没有 log4j 文件,则需要添加如下内容:

-Dlog4j.configuration="file:///C:/<path>/<nameifLog4jFile>"

Also if you use an environment variable in the log4j file, you need to declare it in the arguments. I have an env var by the name MAS_HOME that I use to specify the path to the log file. I then had to add this parameter to VM args:

此外,如果您在 log4j 文件中使用环境变量,则需要在参数中声明它。我有一个名为 MAS_HOME 的环境变量,用于指定日志文件的路径。然后我不得不将此参数添加到 VM args:

-DMAS_HOME="c:\EE"

回答by Rad

I wanted to put this as comment but there is not enough reputation :P

我想把它作为评论,但没有足够的声誉:P

Thismight help if you are using Maven.
And also take a look at this questionfor another solution.

如果您使用 Maven,可能会有所帮助。
也看看这个问题的另一个解决方案。

回答by Jonatan Cloutier

You could use jvm param (answer from Brian Agnew : log4j configuration via JVM argument(s)?) :

您可以使用 jvm 参数(来自 Brian Agnew 的回答:通过 JVM 参数进行 log4j 配置?):

Do you have a log4j configuration file ? Just reference it using

-Dlog4j.configuration={path to file}

where {path to file} should be prefixed with file:

你有 log4j 配置文件吗?只需使用引用它

-Dlog4j.configuration={path to file}

其中 {path to file} 应该以 file: