4

I am using log4j1.2, and I am new to 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.

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.

Here is what my log4j.properties looks like:

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
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149

4 Answers4

6

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

  • First, locate your Tomcat server in eclipse (servers tab) and double-click:

Servers

  • Next, click in Open lauch configuration:

Tomcat configuration

  • Add the log4j configuration argument in Tomcat and Apply.

JVM arguments

Enjoy!


See more in Apache log4j 1.2 - Short introduction to log4j

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • Thank you!!!But I have a very confusing question that, if I right click my project--run configuation-->I will see two server under the tab of Apache Tomcat, one named"New_configuation", another is "Tomcat v7.0 Server at localhost"... If I add the args in the "Tomcat v7.0 Server at localhost" there, it is not working. But if I follow you suggestion, it works!!Why ? – JaskeyLam Aug 26 '14 at 05:26
  • Humm... Interesting! Is it a bug of Tomcat's adapter for eclipse? ;) – Paul Vargas Aug 26 '14 at 05:42
  • And, then after I follow you solution, I check that the arguments are written to the one called "New_configuation" under tab of "Apache Tomcat". But the one under the server tabs which you point to is really named "Tomcat v7.0 Server at localhost" – JaskeyLam Aug 26 '14 at 05:43
  • The names of the Run Configurations are independent of the definitions of the servers that you have in the Servers tabs (also visibles in the Servers *project*). – Paul Vargas Aug 26 '14 at 05:49
1

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

-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:

-DMAS_HOME="c:\EE"
Qiu
  • 5,651
  • 10
  • 49
  • 56
0

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

This might help if you are using Maven.
And also take a look at this question for another solution.

Community
  • 1
  • 1
Rad
  • 4,292
  • 8
  • 33
  • 71
0

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

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:

Community
  • 1
  • 1
Jonatan Cloutier
  • 899
  • 9
  • 26
  • So, what about my properties file, do I need to remove something? Now, if i persists my ole settings, the prod env will still print to the console which is useless. – JaskeyLam Aug 26 '14 at 04:34
  • You could add a second property file that would be specified only in eclipse jvm param. that file doesn't need to be name log4j.xml so it wont be pickedup by the automatic config. or it can be outside your class path – Jonatan Cloutier Aug 26 '14 at 04:44
  • I add "-Dlog4j.configuration="E:\myworkspace\wodinow\src\debug_log4j.properties" to my tomcat arguments, but the logs is not printed. It seems it still use my exisiting log4j.properties. – JaskeyLam Aug 26 '14 at 04:55
  • You have to prefix it with "file:" if it's not in your class path. If it is you should have a path relative the classpath – Jonatan Cloutier Aug 26 '14 at 04:59
  • -Dlog4j.configuration=file:E:\myworkspace\wodinow\src\debug_log4j.properties .. No use, it seems it still look for my old existing log4j.properties. – JaskeyLam Aug 26 '14 at 05:01
  • And I rename my old existing properties into a strange name that ecplise will not recognize, the output says "log4j:WARN Please initialize the log4j system properly", so it seems the vm arguments is not working – JaskeyLam Aug 26 '14 at 05:06