Log 4j文件没有生成/我不知道路径

时间:2015-07-27 06:52:44

标签: java log4j

  1. 我是java和log4j的学习者。
  2. 我只想让.log文件中的所有日志的输出没有生成,或者我不知道路径。
  3. 我只是使用eclipse并在其上运行我的程序。如果您有一个简单的程序可以帮助我学习log4j并且我如何在项目中使用它,请发帖。提前。
  4. //这是我的Java文件///

    package test;
    import org.apache.log4j.Logger;
    import java.io.*;
    
    public class LoggingSample {
        private static Logger logger = Logger.getLogger("LoggingExample");
    
        public static void main(String[] args) {
            try {
                FileInputStream fstream = new FileInputStream("D:\\textfile.txt");
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                while ((strLine = br.readLine()) != null) {
                    System.out.println(strLine);
                }
                in.close();
            } catch (FileNotFoundException fe) {
                logger.error("File Not Found", fe);
                logger.warn("This is a warning message");
                logger.trace("This message will not be logged since log  level is set as DEBUG");
            } catch (IOException e) {
                logger.error("IOEXception occured:", e);
            }
        }
    }
    

    ////这是我的属性文件////

    log4j.rootLogger=INFO,CONSOLE,R
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
    log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.R.File=./logs/testlog.log
    log4j.appender.R.MaxFileSize=200KB
    log4j.appender.R.MaxBackupIndex=2
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n
    

3 个答案:

答案 0 :(得分:2)

您可以添加自定义路径,如下所示

log = /usr/home/log4j
log4j.appender.R.File=${log}/MyLog.log

修改

添加我的整个属性文件

log4j.rootLogger=INFO,Console,FILE

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n


log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d - %c - %p - %m%n
log4j.appender.FILE.File=C:/Log/MyLog.log

答案 1 :(得分:0)

如果你不知道使用路径

{{1}}

它在服务器的日志文件夹中生成文件,即tomcat日志文件夹。

答案 2 :(得分:0)

1.log4j配置文件(log4j.properties)在类路径中不存在。所以我把log4j.property放在文件夹target / classes上。

2.取而代之的是现在一切正常,感谢所有&的 @optuyvu

package test;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import java.io.*;
public class LoggingSample {
 private static Logger logger=Logger.getLogger("LoggingExample");
 static {
      PropertyConfigurator.configure("D:/Vikas/LoggingExample/log4j.properties");
 }
  public static void main(String[] args){
   try{
      FileInputStream fstream = new FileInputStream("D:\\textfile.txt");
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;
       while ((strLine = br.readLine()) != null){
     System.out.println (strLine);
      }
      in.close();
   }catch (FileNotFoundException fe){
     logger.error("File Not Found",fe);
        logger.warn("This is a warning message");
        logger.trace("This message will not be logged since log  level is     set as DEBUG");
   }catch (IOException e){
     logger.error("IOEXception occured:", e);
  }
 }
}