You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by "Dubois, Fabien" <fa...@capgemini.com> on 2008/07/01 15:43:21 UTC
Specify Log Folder path dynamically
Hi everybody,
I have a problem:
I develop a web application, I want to create my log files in a Folder
in the context like this: $CATALINA_HOME/webapps/mywebapp/ $LOG_FOLDER.
I use my own FileAppender to create a log file every day which contains
the date in the name file. It is working!
I have tried to use the same behaviour to specify the folder but it's
not working! I give you the code, maybe someone can find an error
My log4j.properties
******************
log4j.rootLogger=INFO, file
log4j.appender.file=com.capgemini.gestion.GestionLogging
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm} %5p
[%t] (%F\:%L) - %m%n
#Before :
#log4j.appender.file.File=/tomcat/webapps/appWicket-1.0/logs/appWicket_%
date%.log
#Now :
log4j.appender.file.File=%path%/appWicket_%date%.log
log4j.appender.file.append=false
******************
Part of my FileAppender classe
******************
public void setFile(String strFile) {
String strDate;
SimpleDateFormat sdf;
// for the folder, create the link for the path
String real_path = ((WebApplication)
WebApplication.get()).getServletContext().getRealPath("/");
String name_log =
GestionProperties.getProperty("logRealdir");
String path_log = real_path+name_log;
Folder folder_log = new Folder(path_log);
if (!folder_log.exists())
folder_log.mkdir();
// replace %path% to path_log
strFile = strFile.replaceAll("%path%",
path_log);
if (datePattern!=null && strFile!=null) {
sdf = new
SimpleDateFormat(datePattern);
strDate = sdf.format(new Date());
fileName = [B]path_log +[/B]
strFile.replaceAll("%date%", strDate);
} else {
System.err.println("Either File or
DatePattern options are not set for appender [" + name + "].");
}
}
public String getDatePattern() {
return datePattern;
}
public void setDatePattern(String datePattern) {
this.datePattern = datePattern;
}
******************
If I remove the code include for the path folder, as said above, it's
working. But I have to specify manually the path folder, which could
change when machine server changes.
So how can I specify dynamically the log folder path?
Thank you in advance,
Re: Specify Log Folder path dynamically
Posted by Jacob Kjome <ho...@visi.com>.
There are lots of references to doing this on the list. I suggest you
search. In short, you can either add properties directlyto the Property
configurator, if you manually configure Log4j, or set a Java System
property....
java -Dlog.dir=/some/path/to/log/dir ....
And in all cases, you reference the properties using standard Ant-like
property references, such as...
log4j.appender.file.File=${log.dir}/my.log
Jake
On Tue, 1 Jul 2008 15:43:21 +0200
"Dubois, Fabien" <fa...@capgemini.com> wrote:
> Hi everybody,
>
>
>
> I have a problem:
>
>
>
> I develop a web application, I want to create my log files in a Folder
> in the context like this: $CATALINA_HOME/webapps/mywebapp/ $LOG_FOLDER.
>
>
>
> I use my own FileAppender to create a log file every day which contains
> the date in the name file. It is working!
>
>
>
> I have tried to use the same behaviour to specify the folder but it's
> not working! I give you the code, maybe someone can find an error
>
>
>
> My log4j.properties
>
> ******************
>
> log4j.rootLogger=INFO, file
>
> log4j.appender.file=com.capgemini.gestion.GestionLogging
>
> log4j.appender.file.layout=org.apache.log4j.PatternLayout
>
> log4j.appender.file.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm} %5p
> [%t] (%F\:%L) - %m%n
>
> #Before :
>
> #log4j.appender.file.File=/tomcat/webapps/appWicket-1.0/logs/appWicket_%
> date%.log
>
> #Now :
>
> log4j.appender.file.File=%path%/appWicket_%date%.log
>
> log4j.appender.file.append=false
>
> ******************
>
>
>
> Part of my FileAppender classe
>
> ******************
>
> public void setFile(String strFile) {
>
> String strDate;
>
> SimpleDateFormat sdf;
>
>
>
> // for the folder, create the link for the path
>
> String real_path = ((WebApplication)
> WebApplication.get()).getServletContext().getRealPath("/");
>
> String name_log =
> GestionProperties.getProperty("logRealdir");
>
> String path_log = real_path+name_log;
>
> Folder folder_log = new Folder(path_log);
>
> if (!folder_log.exists())
>
> folder_log.mkdir();
>
> // replace %path% to path_log
>
> strFile = strFile.replaceAll("%path%",
> path_log);
>
>
>
> if (datePattern!=null && strFile!=null) {
>
> sdf = new
> SimpleDateFormat(datePattern);
>
> strDate = sdf.format(new Date());
>
> fileName = [B]path_log +[/B]
> strFile.replaceAll("%date%", strDate);
>
> } else {
>
> System.err.println("Either File or
> DatePattern options are not set for appender [" + name + "].");
>
> }
>
> }
>
>
>
> public String getDatePattern() {
>
> return datePattern;
>
> }
>
>
>
> public void setDatePattern(String datePattern) {
>
> this.datePattern = datePattern;
>
> }
>
> ******************
>
>
>
> If I remove the code include for the path folder, as said above, it's
> working. But I have to specify manually the path folder, which could
> change when machine server changes.
>
>
>
> So how can I specify dynamically the log folder path?
>
>
>
> Thank you in advance,
>
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org