當前位置:首頁 » 網頁前端 » javaweb配置log4j
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

javaweb配置log4j

發布時間: 2022-04-01 20:43:14

㈠ 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會在載入的時候去讀取這個變數,如果沒設置過,就會取運行時根目錄作為默認目錄