log4j中未生成日志文件

xvw2m8pv  于 2022-11-06  发布在  其他
关注(0)|答案(3)|浏览(244)

我是log4j的新手。我已经创建了一个示例java程序,在其中实现了log4j。
下面是java程序:

package logging;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class Logging {
    /**
     * @param args the command line arguments
     */
    private static Logger logger = Logger.getLogger(Logging.class);
    public static void main(String[] args) {
        BasicConfigurator.configure();

         logger.trace("This is a Trace");
         logger.debug("This is a Debug");
         logger.info("This is an Info");
         logger.warn("This is a Warn");
         logger.error("This is an Error");
         logger.fatal("This is a Fatal");

    }

}

我在控制台屏幕上得到了输出。但是*日志文件没有*生成。我还使用以下链接在Eclipse氖中配置了我的项目:Configuration
我已经做好了一切。但是日志文件没有生成。当我以编程方式实现log4j时,文件正在生成。下面是我的
属性文件:


# root

log4j.logger.com.apress.logging.log4j=debug,dest

log4j.additivity.com.apress.logging.log4j=false

# define the appender

log4j.appender.dest = org.apache.log4j.DailyRollingFileAppender

# set the name of the file

log4j.appender.dest.File=${user.home}/log.out

# setting the immediate flush to true (default)

log4j.appender.dest.ImmediateFlush=true

# setting the threshold

log4j.appender.dest.Threshold=ERROR

# setting the append to false, overwrite

log4j.appender.dest.Append=true

# set the DatePattern

log4j.appender.dest.DatePattern='.' yyyy-MM-dd

要让Log4J写入日志文件,需要执行什么操作?

30byixjq

30byixjq1#

确保log4j.properties位于默认包中


# Root logger option

    log4j.rootLogger=DEBUG, stdout, file
    log4j.logger.infoLogger=DEBUG, infoLogger

    log4j.additivity.infoLogger = false
    # Redirect log messages to console
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

    # Redirect log messages to a log file, support file rolling.
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=E:\\LOG\\ConvertorLogger.log
    log4j.appender.file.MaxFileSize=5MB
    log4j.appender.file.MaxBackupIndex=10
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

    # Redirect log messages to a log file, support file rolling.
    log4j.appender.infoLogger=org.apache.log4j.RollingFileAppender
    log4j.appender.infoLogger.File=E:\\LOG\\ConvertorInfoLogger.log
    log4j.appender.infoLogger.MaxFileSize=5MB
    log4j.appender.infoLogger.MaxBackupIndex=10
    log4j.appender.infoLogger.layout=org.apache.log4j.PatternLayout
    log4j.appender.infoLogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
cunj1qz1

cunj1qz12#

您可以参考此文件:log4j.properties

log4j.rootLogger=WARN,stdout,R
  log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c -%m%n
  log4j.appender.R=org.apache.log4j.RollingFileAppender
  log4j.appender.R.File=${user.home}/log.out
  log4j.appender.MaxFileSize=100KB
  log4j.appender.R.MaxBackupIndex=1
  log4j.appender.R.layout=org.apache.log4j.PatternLayout
  log4j.appender.R.layout.ConversionPattern = %p %t %c -%m%n
  log4j.logger.com.foo=WARN
bjp0bcyl

bjp0bcyl3#

我必须在我的log4j2.xml文件中给予绝对路径(对于Windows,使用Intellij IDEA),如下所示:

fileName="C:\Users\Fotios.Kolytoumpas\IdeaProjects\jetbrains-hibernate\logs\your-app-name.log"

相关问题