英文:
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 :
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
main code :
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 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("D:\\intelliJIDEAProjects\\CRUDCOMPUTER\\src\\main\\resources\\log4j.properties");
}
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论