方法一:将日志文件输出到tomcat自身的日志文件夹logs中
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
# console is set to be a ConsoleAppender using a PatternLayout.
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
log4j.appender.console=org.apache.log4j.ConsoleAppender
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
log4j.appender.console.Threshold=info
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
log4j.appender.console.layout=org.apache.log4j.PatternLayout
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
log4j.appender.console.layout.ConversionPattern=- %m%n
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
# R is set to be a File appender using a PatternLayout.
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
log4j.appender.R=org.apache.log4j.RollingFileAppender
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
log4j.appender.R.Append=
true
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
log4j.appender.R.Threshold=info
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
log4j.appender.R.MaxFileSize=1024KB
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
log4j.appender.R.MaxBackupIndex=10
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
log4j.appender.R.File=../logs/abc.log
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
log4j.appender.R.layout=org.apache.log4j.PatternLayout
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
log4j.appender.R.layout.ConversionPattern=%-d
{yyyy-MM-dd HH\:mm\:ss} [%c]-[%p] %m%n
方法二:将日志文件输出到自己程序WEB-INF\logs目录中
1
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
1.
<
servlet
>
2
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2.
<
servlet-name
>lo4jInit
</
servlet-name
>
3
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3.
<
servlet-class
>com.linder.util.log4j.init.Lo4jInit
</
servlet-class
>
4
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
4.
<
init-param
>
5
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
5.
<
param-name
>log4j_init_path
</
param-name
>
6
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
6.
<
param-value
>WEB-INF\classes\log4j.properties
</
param-value
>
7
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
7.
</
init-param
>
8
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
8.
<
init-param
>
9
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
9.
<
param-name
>log4j_file_path
</
param-name
>
10
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
10.
<
param-value
>WEB-INF\logs\
</
param-value
>
11
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
11.
</
init-param
>
12
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
12.
<
load-on-startup
>0
</
load-on-startup
>
13
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
13.
</
servlet
>
14
2、编写一个初始化log4j的Servlet
3、log4j.properties的配置信息
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
1.# Set root category priority to info and its only appender to console.
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2.log4j.rootCategory=info,console,R
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3.#log4j.debug=
true
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
4.
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
5.# console is set to be a ConsoleAppender using a PatternLayout.
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
6.log4j.appender.console=org.apache.log4j.ConsoleAppender
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
7.log4j.appender.console.Threshold=info
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
8.log4j.appender.console.layout=org.apache.log4j.PatternLayout
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
9.log4j.appender.console.layout.ConversionPattern=- %m%n
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
10.
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
11.# R is set to be a File appender using a PatternLayout.
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
12.log4j.appender.R=org.apache.log4j.RollingFileAppender
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
13.log4j.appender.R.Append=
true
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
14.log4j.appender.R.Threshold=info
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
15.log4j.appender.R.MaxFileSize=1024KB
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
16.log4j.appender.R.MaxBackupIndex=10
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
17.log4j.appender.R.File=abc.log
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
18.log4j.appender.R.layout=org.apache.log4j.PatternLayout
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
19.log4j.appender.R.layout.ConversionPattern=%-d
{yyyy-MM-dd HH\:mm\:ss} [%c]-[%p] %m%n