㈠ idea JAVA非web项目如何配置slf4j-Log4j
PropertyConfigurator.configure("E:/study/log4j/log4j.properties");
//DOMConfigurator.configure("E:/study/log4j/log4j.xml");//加载.xml文件
//从项目目录开始读
PropertyConfigurator.configure("log4j/log4j.properties");
//DOMConfigurator.configure("log4j/log4j.xml");//加载.xml文件
㈡ java项目无web-inf 能使用log4j吗
在web.xml中添加配置:
<!-- 配置log4j配置文件的路径,可以是xml或 properties(此参数必须配)-->
下面使用了classpath 参数指定log4j.properties文件的位置,这样log4j的配置文件就不用非要放到src的下面:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:config/log4j/log4j.properties</param-value>
</context-param>
使用spring的监听器,当应用启动时来读取log4j的配置文件
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
㈢ 如何让自己的java web工程使用log4j
导入log4j.jar包
㈣ log4j在java的web项目中怎么用的,如何配置等等。。
在web.xml中添加配置:
<!-- 配置log4j配置文件的路径,可以是xml或 properties(此参数必须配)-->
下面使用了classpath 参数指定log4j.properties文件的位置,这样log4j的配置文件就不用非要放到src的下面:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:config/log4j/log4j.properties</param-value>
</context-param>
使用spring的监听器,当应用启动时来读取log4j的配置文件
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
㈤ java web项目中关于log4j的运用
1、2只要配置一个,不过:
1配置需要配合使用Spring
2配置需要自己写Log4jInitServlet
其实还有更简单的,只要把log4j.properties放在任意一个src目录下就可以了,什么额外的配置都不需要。
㈥ javaweb程序中log4j丢失
打印的级别高了 比如你直接打印error 或warn info级别的日志就会丢失! 先查看打印级别 在处理你的程序!
㈦ java 如何配置log4j日志文件保存路径
以DailyRollingFileAppender 为例:假设每天一个日志文件
有以下设置:
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=app.log
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %5p - %c -%-4r [%t] - %m%n
此时生成日志文件将位于tomcat的bin目录下,如要将日志文件保存在 :根目录/web-info/logs/下,个人有以下4种解决方案:
1 绝对路径
log4j.appender.A1.File=D:\apache-tomcat-6.0.18/webapps/项目/WEB-INF/logs/app.log
但这种写法灵活性很差
以下3中使用相同的设置原理: jvm的环境变量
2:spring的Log4jConfigListener
通过以下配置:
< context-param>
<param-name>webAppRootKey</param-name>
<param-value>webApp.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
< listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
...
log4j.appender.logfile.File=${webApp.root}/WEB-INF/logs/app.log
...
来解决
2:使用已有jvm变量:
例如:
log4j.appender.logfile.File=${user.home}/logs/app.log
日志将位于:例如windows:C:\Documents and Settings\joe\logs\app.log
3 自己设置目录,也就是在项目启动时通过System.setProperty设置
通过实现ServletContextListener来解决:例如
public class log4jlistener implements ServletContextListener {
public static final String log4jdirkey = "log4jdir";
public void contextDestroyed(ServletContextEvent servletcontextevent) {
System.getProperties().remove(log4jdirkey);
}
public void contextInitialized(ServletContextEvent servletcontextevent) {
String log4jdir = servletcontextevent.getServletContext().getRealPath("/");
//System.out.println("log4jdir:"+log4jdir);
System.setProperty(log4jdirkey, log4jdir);
}
}
web.xml配置:
<listener>
<listener-class>com.log4j.log4jlistener</listener-class>
</listener>
log4j.prtperties 配置:
log4j.appender.A1.File=${log4jdir}/WEB-INF/logs/app1.log
来解决。
㈧ 怎样添加java的log4j添加到java项目中
首先,弄到log4j的jar包,maven工程配置以下依赖就行,或者,从阿里的maven仓库下载jar包,添加到工程的“build path”log4j log4j 1.2.17
然后,整一个log4j.properties,内容如下,然后把它放在src/main/java目录(也就是包所在的根目录)
1、普通java工程或spring工程
这是最常见的java工程类型,写demo用的多,把log4j.properties放在src/main/java目录(也就是包所在的根目录)就行了
2、spring mvc工程
web工程里用spring mvc构建的比较多了,把log4j.properties放在src/main/resources的conf目录(web工
程配置文件通常在resources或WEB-INF目录),编辑web.xml,添加
log4jConfigLocation classpath:/conf/log4j.properties org.springframework.web.util.Log4jConfigListener
3、普通web工程
没有了spring提供的listener加载log4j.properties,我们要怎么加载这个文件呢?同样,把log4j.properties
放在src/main/resources的conf目录,我们整一个servlet来加载
{ = 1L; publicvoidinit(ServletConfig config)throwsServletException { String prefix =this.getClass().getClassLoader().getResource("/").getPath(); String path = config.getInitParameter("log4j-path"); PropertyConfigurator.configure(prefix + path); } publicvoiddoGet(HttpServletRequest req, HttpServletResponse res)throwsIOException, ServletException {} publicvoiddoPost(HttpServletRequest req, HttpServletResponse res)throwsIOException, ServletException {} publicvoiddestroy() {} }
然后配置servlet随着web工程启动而初始化,编辑web.xml,添加
log4j com.xmyself.log4j.Log4jServlet log4j-path conf/log4j.properties 1
看着是不是和spring mvc的很像,甚至你也想到了,普通java工程没有指定log4j.properties的路径,那说明
log4j的jar包一定有一个默认的路径。另外,建议,log4j的配置放在第一个,因为后续加载其他组件就要开始使用日
㈨ java log4j放在其他目录怎么配置
你意思是不是想把log4j的配置文件放在其他目录,而不是默认目录?这个需要在web.xml里配置一下
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/classes/log4j.properties</param-value>
</context-param>
如果你放在默认目录里,就不需要这一段,加上这一段,就是认为改变了log4jConfigLocation的值,log4j会在加载的时候去读取这个变量,如果没设置过,就会取运行时根目录作为默认目录