简体   繁体   中英

log4j.properties cannot be found

I'm having a hard time to get my logger start working.

My log4j.properties file looks like :

# Log levels
log4j.rootLogger=INFO,CONSOLE,R
# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file
log4j.appender.R.File=./logs/testlog.log
log4j.appender.R.MaxFileSize=200KB
# Number of backup files
log4j.appender.R.MaxBackupIndex=2
# Layout for Rolling File Appender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n

Taken from this tutorial

When i'm printing my classPath, it appears that "C:\\Projects\\SDK\\resources" is one of the directories in the classPath, and that's where my log4j.properties file is.

My logger is initiated as:

protected static Logger logger = Logger.getLogger("LoggerExmaple");

And in my main method i'm trying to :

MyClass.logger.warn("bli bla blo");

But getting the following error in the console :

log4j:WARN No appenders could be found for logger (LoggerExmaple).
log4j:WARN Please initialize the log4j system properly.

And when i'm trying to do first

BasicConfigurator.configure();

Then my logger output is

0 [main] WARN LoggerExmaple  - bli bla blo

My maven dependency for log4j is :

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.0-beta9</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.0-beta9</version>
</dependency>

Help anyone? EDIT: Answers didn't help. Anyone?

我没有与L4J长期合作,但我认为像Hibernate一样,log4j.prpperties文件应放置在项目内的目录等中

You need to define WARN appender in log4j.properties. You have only INFO, CONSOLE and R appenders and you are trying to access WARN. Try adding these lines to your log4j.properties file

log4j.appender.WARN=org.apache.log4j.FileAppender
log4j.appender.WARN.File=./logs/testlog.log
log4j.appender.WARN.layout=org.apache.log4j.PatternLayout
log4j.appender.WARN.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

and change

log4j.rootLogger=INFO,CONSOLE,R,WARN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM