I'm having some problems putting my log4j.properties file on classpath. I can use it when I'm developing (Eclipse Indigo) but, when I export my app as a JAR, I can't.
I've made by hand a MANIFEST.MF file for the exported JAR:
Manifest-Version: 1.0
Main-Class: main.Program
Class-Path: lib/log4j.properties lib/log4j-1.2.15.jar
And then with put the JAR on this file organization:
folder
|-------- app.jar
|-------- lib
|--------- log4j.properties
|--------- log4j-1.2.15.jar
When I try to run app.jar, they find log4j.jar but not log4j.properties:
log4j:WARN No appenders could be found for logger (main.Program).
log4j:WARN Please initialize the log4j system properly.
My log4j.properties file it's like this:
log4j.rootLogger=INFO, stdout, file
PATTERN=[%d] [%p] [%c{1}]: %m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=${PATTERN}
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.File=${logger_file_path}
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=${PATTERN}
The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file.
rootLogger property sets the Level (DEBUG here) and Appender (A1 here) for root Logger. This is not mandatory. Root logger does not have a default appender attached and it can exist without an appender.
Go to Project Structure | Modules | Your Module | Dependencies, click Add, Single-Entry Module Library, specify the path to the "resources" folder. Yet another solution would be to put the log4j. properties file directly under the Source root of your project (in the default package directory).
The log4j. properties file is a log4j configuration file which stores properties in key-value pairs. The log4j properties file contains the entire runtime configuration used by log4j. This file will contain log4j appenders information, log level information and output file names for file appenders.
println() over Log4j, of course for testing purposes, because it doesn't require any configuration, you can just use it, without bothering about XML or properties file configuration, but the most programmer will agree that they would prefer to use Log4j over println statements, even for test programs if it's easy to ...
one more way to do this:
-Dlog4j.configuration=file:"./log4j.properties"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With