54

First up - yes, I have read the multiple questions & answers on this topic, and can't get any of the solutions within those to help me. I'm not running Tomcat or JBoss and I don't have a web.xml file to change. I'm using Java 6.0 and log4j-1.2.8.jar.

I am creating a runnable jar file with IDEA IntelliJ with jar libraries packaged separately and linked via the manifest. I am running my code on a Linux server thus:

me@server:/mydir> java -jar code/myjar.jar
log4j:WARN No appenders could be found for logger (FactoredEventsForTrna).
log4j:WARN Please initialize the log4j system properly.

My log4j configuration file (which I've placed both in mydir and mydir/code, just in case) is:

## Logger configure file for myproject
log.dir=log/
datestamp=yyyy-MM-dd/HH:mm:ss.SSS
log4j.rootLogger=TRACE, file, proappender, console

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=1GB
log4j.appender.file.maxBackupIndex=5
log4j.appender.file.File=log/mydebug.log
log4j.appender.file.threshold=TRACE
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{${datestamp}} %5p: %c{2} - %m%n

log4j.appender.proappender=org.apache.log4j.RollingFileAppender
log4j.appender.proappender.maxFileSize=5GB
log4j.appender.proappender.Threshold=INFO
log4j.appender.proappender.File=log/myinfo.log
log4j.appender.proappender.layout=org.apache.log4j.PatternLayout
log4j.appender.proappender.layout.ConversionPattern=%d{${datestamp}} %5p: %c{2} - %m%n

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{${datestamp}} %5p: %c{2} - %m%n

And I have created the log/ directory in mydir and mydir/code, again, just in case.

Any ideas?

StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
Ina
  • 4,400
  • 6
  • 30
  • 44

7 Answers7

63

There are many possible options for specifying your log4j configuration. One is for the file to be named exactly "log4j.properties" and be in your classpath. Another is to name it however you want and add a System property to the command line when you start Java, like this:

-Dlog4j.configuration=file:///path/to/your/log4j.properties

All of them are outlined here http://logging.apache.org/log4j/1.2/manual.html#defaultInit

John Watts
  • 8,717
  • 1
  • 31
  • 35
43

Solution

  1. Download log4j.jar file
  2. Add the log4j.jar file to build path
  3. Call logger by:

    private static org.apache.log4j.Logger log 
        = Logger.getLogger(<class-where-this-is-used>.class);
    
  4. if log4j properties does not exist, create new file log4j.properties file new file in bin directory:

    /workspace/projectdirectory/bin/
    

Sample log4j.properties file

log4j.rootLogger=debug, stdout
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=%t %-5p %c{2} - %m%n 
Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
mel3kings
  • 8,857
  • 3
  • 60
  • 68
  • What is MyLogger.class. What I need to put here exactly?? I am getting this error log4j:WARN No appenders could be found for logger (com.company.Main). final static Logger logger = Logger.getLogger(Main.class); – Usman Tahir Oct 28 '14 at 06:41
  • There is a misplaced white space in the end of ConversionPattern in case someone wonders why his console output looks odd. – dreua Dec 02 '15 at 18:42
  • feel free to visit my site for some pointers on web development as well: http://sysdotoutdotprint.com/ – mel3kings Mar 28 '17 at 03:50
10

I had moved my log4j.properties into the resources folder and it worked fine for me !

Gloria Rampur
  • 353
  • 3
  • 12
  • 1
    By default java project doesn't have resources folder. So we need to create one. Put log4j.properties file into this folder. For intelli Idea, right click on this resources folder, select "Mark Directory As", and then select "Resources Root". – Pankaj Shinde Sep 24 '15 at 04:00
3

Man, I had the issue in one of my eclipse projects, amazingly the issue was the order of the jars in my .project file. believe it or not!

MadMad666
  • 955
  • 3
  • 11
  • 19
2

You will find de log4j.properties in solr/examples/resources.

If you don't find the file, i put it here:

#  Logging level
solr.log=logs/
log4j.rootLogger=INFO, file, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n

#- size rotation with log cleanup.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=4MB
log4j.appender.file.MaxBackupIndex=9

#- File to log to and log format
log4j.appender.file.File=${solr.log}/solr.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m\n

log4j.logger.org.apache.zookeeper=WARN
log4j.logger.org.apache.hadoop=WARN

# set to INFO to enable infostream log messages
log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF

Regards

Carlos Espeleta
  • 129
  • 1
  • 1
  • 12
1

Move Your log4j.properties file into the src folder. Open Windows explorer, browse to the directory where your project resides in. Browse to bin directory and delete all the folders with in it.

Now in eclipse click project---->clean---->OK

This would force it to build the project again,all the contents delete from bin directory will be recreated.

I took while to figure this out. At times directly clicking clean doesn't work

ramit girdhar
  • 2,272
  • 1
  • 25
  • 26
0

put the folder which has the properties file for log in java build path source. You can add it by right clicking the project ----> build path -----> configure build path ------> add t