如何配置Log4j以读取我的属性文件?

时间:2013-05-15 22:22:41

标签: java log4j smslib

我正在使用Java中的SMSLib发送短信。我已经加载了log4j jar文件并将log4j.properties文件放在正确的位置,但它仍然无法读取它并获得以下异常:

例外文字:

  

log4j:错误无法读取配置文件[log4j.properties]。       java.io.FileNotFoundException:log4j.properties(Lefichierspécifiéestintrouvable)           at java.io.FileInputStream.open(Native Method)           在java.io.FileInputStream。(FileInputStream.java:138)           在java.io.FileInputStream。(FileInputStream.java:97)           在org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:372)           在org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:403)           在org.smslib.helper.Logger。(Logger.java:50)           在org.smslib.helper.Logger。(Logger.java:29)           at org.smslib.Service.listSystemInformation(Service.java:113)           在org.smslib.Service.initializeService(Service.java:103)           在org.smslib.Service。(Service.java:95)           在org.smslib.Service。(Service.java:90)           在laposte.EnvoiSMS。(EnvoiSMS.java:181)           在laposte.login.passwordKeyPressed(login.java:230)           在laposte.login.access $ 100(login.java:26)           在laposte.login $ 2.keyPressed(login.java:80)           at java.awt.Component.processKeyEvent(Component.java:6463)           在javax.swing.JComponent.processKeyEvent(JComponent.java:2829)           at java.awt.Component.processEvent(Component.java:6282)           at java.awt.Container.processEvent(Container.java:2229)           at java.awt.Component.dispatchEventImpl(Component.java:4861)           at java.awt.Container.dispatchEventImpl(Container.java:2287)           at java.awt.Component.dispatchEvent(Component.java:4687)           at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)           at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)           at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027)           at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)           at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)           at java.awt.Component.dispatchEventImpl(Component.java:4731)           at java.awt.Container.dispatchEventImpl(Container.java:2287)           at java.awt.Window.dispatchEventImpl(Window.java:2719)           at java.awt.Component.dispatchEvent(Component.java:4687)           at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)           at java.awt.EventQueue.access $ 200(EventQueue.java:103)           at java.awt.EventQueue $ 3.run(EventQueue.java:688)           at java.awt.EventQueue $ 3.run(EventQueue.java:686)           at java.security.AccessController.doPrivileged(Native Method)           at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)           at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87)           at java.awt.EventQueue $ 4.run(EventQueue.java:702)           at java.awt.EventQueue $ 4.run(EventQueue.java:700)           at java.security.AccessController.doPrivileged(Native Method)           at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)           at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)           at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)           at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)           at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)           at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)           at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)           at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)       log4j:ERROR忽略配置文件[log4j.properties]。       log4j:WARN找不到logger(smslib)的appender。       log4j:WARN请正确初始化log4j系统。       log4j:WARN有关详细信息,请参阅http://logging.apache.org/log4j/1.2/faq.html#noconfig

2 个答案:

答案 0 :(得分:8)

您需要将文件放在classpath上。尝试将其放在类路径中的文件夹中,如源文件夹。

link可能有用。

答案 1 :(得分:0)

  

log4j:ERROR无法读取配置文件[log4j.properties]。 java.io.FileNotFoundException:

首先,您应该将名称另存为log4j.properties,然后选择文件类型作为“所有文件”,然后单击“保存”,保存位置应为您的项目(注意:不要将其保存在工作空间中)。

Log4j属性:“ log4j.properties”

这是用于生成日志的代码。将其另存为log4j.properties。

// Here we have defined root logger
log4j.rootLogger=INFO,CONSOLE,R,HTML,TTCC

// Here we define the appender
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.TTCC=org.apache.log4j.RollingFileAppender
log4j.appender.HTML=org.apache.log4j.FileAppender

// Here we define log file location
log4j.appender.R.File=./log/testlog.log
log4j.appender.TTCC.File=./log/testlog1.log
log4j.appender.HTML.File=./log/application.html

// Here we define the layout and pattern
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= %5p [%t] (%F:%L)- %m%n
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c -%p - %m%n
log4j.appender.TTCC.layout=org.apache.log4j.TTCCLayout
log4j.appender.TTCC.layout.DateFormat=ISO8601
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
log4j.appender.HTML.layout.Title=Application log
log4j.appender.HTML.layout.LocationInfo=true