Discussion:
set the placeholder in the log4j.properties file
maven apache
2010-12-27 06:26:22 UTC
Permalink
Hi:

I want the logs created by log4j put under the app dir,so I set the
placeholder in the log4j.properties:

# chartstdout is set to be a ConsoleAppender.
log4j.appender.chartstdout=org.apache.log4j.ConsoleAppender
log4j.appender.chartstdout.layout=org.apache.log4j.PatternLayout
log4j.appender.chartstdout.layout.ConversionPattern=%-4r [%t] %-5p %l %x -
%m%n


log4j.appender.chartfileout=org.apache.log4j.FileAppender
log4j.appender.chartfileout.File=${apppath}/logs/log.html
log4j.appender.chartfileout.Append=true
log4j.appender.chartfileout.layout=com.test.util.CustomHTMLLayout

log4j.rootLogger=debug, chartstdout,chartfileout

Then in my servlet whose "load-on-startup" attribute is "0" I set the
property "apppath",but it does not work.
It seems that the log4j read the log4j.properties and config the log before
I modify the property.
public void init() {
DbManager.init();
// log
String context =
getServletConfig().getServletContext().getRealPath("/");
Properties p = new Properties();
p.setProperty("apppath", context);
try {

p.load(InitServlet.class.getResourceAsStream("/log4j.properties"));
} catch (IOException e) {
e.printStackTrace();
}
PropertyConfigurator.configure(p);
}



Of course,I can rename the log4j.properties,then the log4j will not find the
file but I can. However some other libs which I used in my appliction
require log4j also,they need the log4j.properties to generate there logs.

Any ideas?
Jacob Kjome
2010-12-29 18:14:00 UTC
Permalink
Log4j will attempt to auto-configure itself by looking for "log4j.xml" and then
"log4j.properties" in the root of the claspath upon the first call to getLogger().
And yes, this may very well be before your init servlet runs. Note that the new
configuration (done manually in the init servlet) will have things configured as
you want them.

There is a solution. You can set system properties....

-Dapppath=/my/logging/directory

And, optionally....
-Dlog4j.configuration=file:///path/to/log4j.properties


That said, you could just place a minimal config file in the classpath pointing
all logging to the Console and then let your init servlet configure the preferred
config file as you are doing. I guess it all depends on how important it is for
you to get all logging to your preferred file, even logging that occurs prior to
your init servlet running.


Jake
Post by maven apache
I want the logs created by log4j put under the app dir,so I set the
# chartstdout is set to be a ConsoleAppender.
log4j.appender.chartstdout=org.apache.log4j.ConsoleAppender
log4j.appender.chartstdout.layout=org.apache.log4j.PatternLayout
log4j.appender.chartstdout.layout.ConversionPattern=%-4r [%t] %-5p %l %x -
%m%n
log4j.appender.chartfileout=org.apache.log4j.FileAppender
log4j.appender.chartfileout.File=${apppath}/logs/log.html
log4j.appender.chartfileout.Append=true
log4j.appender.chartfileout.layout=com.test.util.CustomHTMLLayout
log4j.rootLogger=debug, chartstdout,chartfileout
Then in my servlet whose "load-on-startup" attribute is "0" I set the
property "apppath",but it does not work.
It seems that the log4j read the log4j.properties and config the log before
I modify the property.
public void init() {
DbManager.init();
// log
String context =
getServletConfig().getServletContext().getRealPath("/");
Properties p = new Properties();
p.setProperty("apppath", context);
try {
p.load(InitServlet.class.getResourceAsStream("/log4j.properties"));
} catch (IOException e) {
e.printStackTrace();
}
PropertyConfigurator.configure(p);
}
Of course,I can rename the log4j.properties,then the log4j will not find the
file but I can. However some other libs which I used in my appliction
require log4j also,they need the log4j.properties to generate there logs.
Any ideas?
Tushar Kapila
2010-12-30 09:55:44 UTC
Permalink
I think for most apps you would want all logs to go to file so you can
see start up exceptions. Only place you might not mind missing this is
when no app specific common code runs before this servlet inits.
So my advice is customize the log4j xml in file system per install.
That way all logs will be in file.
Post by Jacob Kjome
Log4j will attempt to auto-configure itself by looking for "log4j.xml" and then
"log4j.properties" in the root of the claspath upon the first call to getLogger().
And yes, this may very well be before your init servlet runs. Note that the new
configuration (done manually in the init servlet) will have things configured as
you want them.
There is a solution. You can set system properties....
-Dapppath=/my/logging/directory
And, optionally....
-Dlog4j.configuration=file:///path/to/log4j.properties
That said, you could just place a minimal config file in the classpath pointing
all logging to the Console and then let your init servlet configure the preferred
config file as you are doing. I guess it all depends on how important it is for
you to get all logging to your preferred file, even logging that occurs prior to
your init servlet running.
Jake
Post by maven apache
I want the logs created by log4j put under the app dir,so I set the
# chartstdout is set to be a ConsoleAppender.
log4j.appender.chartstdout=org.apache.log4j.ConsoleAppender
log4j.appender.chartstdout.layout=org.apache.log4j.PatternLayout
log4j.appender.chartstdout.layout.ConversionPattern=%-4r [%t] %-5p %l %x -
%m%n
log4j.appender.chartfileout=org.apache.log4j.FileAppender
log4j.appender.chartfileout.File=${apppath}/logs/log.html
log4j.appender.chartfileout.Append=true
log4j.appender.chartfileout.layout=com.test.util.CustomHTMLLayout
log4j.rootLogger=debug, chartstdout,chartfileout
Then in my servlet whose "load-on-startup" attribute is "0" I set the
property "apppath",but it does not work.
It seems that the log4j read the log4j.properties and config the log before
I modify the property.
public void init() {
DbManager.init();
// log
String context =
getServletConfig().getServletContext().getRealPath("/");
Properties p = new Properties();
p.setProperty("apppath", context);
try {
p.load(InitServlet.class.getResourceAsStream("/log4j.properties"));
} catch (IOException e) {
e.printStackTrace();
}
PropertyConfigurator.configure(p);
}
Of course,I can rename the log4j.properties,then the log4j will not find the
file but I can. However some other libs which I used in my appliction
require log4j also,they need the log4j.properties to generate there logs.
Any ideas?
---------------------------------------------------------------------
--
Sent from my mobile device
maven apache
2010-12-30 10:44:17 UTC
Permalink
Thanks for all you guys. I will have a try.
Post by Tushar Kapila
I think for most apps you would want all logs to go to file so you can
see start up exceptions. Only place you might not mind missing this is
when no app specific common code runs before this servlet inits.
So my advice is customize the log4j xml in file system per install.
That way all logs will be in file.
Post by Jacob Kjome
Log4j will attempt to auto-configure itself by looking for "log4j.xml"
and
Post by Jacob Kjome
then
"log4j.properties" in the root of the claspath upon the first call to getLogger().
And yes, this may very well be before your init servlet runs. Note that the new
configuration (done manually in the init servlet) will have things configured as
you want them.
There is a solution. You can set system properties....
-Dapppath=/my/logging/directory
And, optionally....
-Dlog4j.configuration=file:///path/to/log4j.properties
That said, you could just place a minimal config file in the classpath pointing
all logging to the Console and then let your init servlet configure the preferred
config file as you are doing. I guess it all depends on how important it
is
Post by Jacob Kjome
for
you to get all logging to your preferred file, even logging that occurs prior to
your init servlet running.
Jake
Post by maven apache
I want the logs created by log4j put under the app dir,so I set the
# chartstdout is set to be a ConsoleAppender.
log4j.appender.chartstdout=org.apache.log4j.ConsoleAppender
log4j.appender.chartstdout.layout=org.apache.log4j.PatternLayout
log4j.appender.chartstdout.layout.ConversionPattern=%-4r [%t] %-5p %l %x
-
Post by Jacob Kjome
Post by maven apache
%m%n
log4j.appender.chartfileout=org.apache.log4j.FileAppender
log4j.appender.chartfileout.File=${apppath}/logs/log.html
log4j.appender.chartfileout.Append=true
log4j.appender.chartfileout.layout=com.test.util.CustomHTMLLayout
log4j.rootLogger=debug, chartstdout,chartfileout
Then in my servlet whose "load-on-startup" attribute is "0" I set the
property "apppath",but it does not work.
It seems that the log4j read the log4j.properties and config the log before
I modify the property.
public void init() {
DbManager.init();
// log
String context =
getServletConfig().getServletContext().getRealPath("/");
Properties p = new Properties();
p.setProperty("apppath", context);
try {
p.load(InitServlet.class.getResourceAsStream("/log4j.properties"));
} catch (IOException e) {
e.printStackTrace();
}
PropertyConfigurator.configure(p);
}
Of course,I can rename the log4j.properties,then the log4j will not find the
file but I can. However some other libs which I used in my appliction
require log4j also,they need the log4j.properties to generate there
logs.
Post by Jacob Kjome
Post by maven apache
Any ideas?
---------------------------------------------------------------------
--
Sent from my mobile device
---------------------------------------------------------------------
Loading...