log4j日志文件未被创建。

huangapple go评论76阅读模式
英文:

file with log4j logs is not created

问题

在资源文件夹中搜索,未找到合适内容,以下为文件夹中的配置文件:

    log4j.rootLogger=INFO, file
            
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=C:\\Users\096\\Desktop\\Directories\\logs
    log4j.appender.file.MaxFileSize=1MB
    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

Maven 依赖:

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.13.3</version>
    </dependency>

主要代码:

    static Logger log = LoggerFactory.getLogger(Application.class);
    
    public static void main(String[] args) throws InterruptedException {
    
        log.debug("Hello this is a debug message");
        log.info("Hello this is an info message");

IDEA 输出:

    2020-10-02 22:36:47 DEBUG Application:25 - Hello this is a debug message
    2020-10-02 22:36:47 INFO  Application:26 - Hello this is an info message
    
    进程以退出代码 0 结束

但未创建日志文件

尝试更改依赖项,添加不同代码,如:

    static {
            PropertyConfigurator.configure("D:\\intelliJIDEAProjects\\CRUDCOMPUTER\\src\\main\\resources\\log4j.properties");
        }

配置文件:

    log4j.rootLogger=file, stdout
    
    log4j.appender.file.File=C:\\Users\096\\Desktop\\Directories\\logs
    log4j.appender.file.MaxFileSize=10MB
    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
    
    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

没有任何帮助,我怀疑没有写入权限,但我不知道如何修复。
英文:

searched, found nothing suitable, here is the config file in the folder resources:

log4j.rootLogger=INFO, file
        
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\Users\096\\Desktop\\Directories\\logs
log4j.appender.file.MaxFileSize=1MB
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

maven dependence :

&lt;dependency&gt;
  &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
  &lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
  &lt;version&gt;2.13.3&lt;/version&gt;
&lt;/dependency&gt;

main code :

static Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) throws InterruptedException {

            log.debug(&quot;Hello this is a debug message&quot;);
            log.info(&quot;Hello this is an info message&quot;);

IDEA output

2020-10-02 22:36:47 DEBUG Application:25 - Hello this is a debug message
2020-10-02 22:36:47 INFO  Application:26 - Hello this is an info message

Process finished with exit code 0

but no log file was created

tried to change dependencies, added different codes such as

static {
        PropertyConfigurator.configure(&quot;D:\\intelliJIDEAProjects\\CRUDCOMPUTER\\src\\main\\resources\\log4j.properties&quot;);
    }

config file

log4j.rootLogger=file, stdout


log4j.appender.file.File=C:\\Users\096\\Desktop\\Directories\\logs
log4j.appender.file.MaxFileSize=10MB
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


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

nothing helped, I suspect there is no write access, but I do not know how to fix it

答案1

得分: 0

看起来你正在使用log4j2。这有一个不同的配置文件。你正在使用的配置文件是针对log4j 1.X的。尝试将依赖项更改为使用log4j 1.X,或者将配置文件更改为与log4j2规范相匹配。

(注:这是来自我的原始评论)

英文:

It seems you're using log4j2. This has a different configuration file. The config file you are using is for log4j 1.X. Try changing the dependencies to use log4j 1.X or change the configuration file to match the log4j2 specifications.

(Note: This is from my original comment)

huangapple
  • 本文由 发表于 2020年10月3日 04:14:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64177728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定