Log4J creates a log file but does not write to it

I am trying to configure log4j to write messages to a file. Right now the file is being created with the name that I provide, but the logs are not written to the file. My code is:

public class Main extends Application
{
    private static Logger logger = Logger.getLogger( Main.class.getName() );
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        logger.warning("test");
        logger.severe("oh noes");
    }


    public static void main(String[] args) {
        launch(args);
    }
}

My file content log4j.properties:

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log.log
log4j.appender.file.MaxFileSize=1024MB
log4j.appender.file.MaxBackupIndex=1
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

When I run this, I get this output in the console:

Jun 23, 2014 3:19:16 AM com.foo.Main start
WARNING: test
Jun 23, 2014 3:19:16 AM com.foo.Main start
SEVERE: oh noes

The file log.logis created in my main directory. But its empty.

Any ideas what I'm doing wrong? I am using log4j version 1.2.17.

+5
source share
4 answers

The output appears to have a default format that emits the standard Java logging environment (JUL).

So there are two possibilities (that come to mind):

  • java.util.logging.Logger, org.apache.log4j.Logger.
  • , Log4J JUL.
+7

log4j.jar path path.your. .

# Root logger option
log4j.rootLogger=INFO, file,stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender


log4j.appender.file.File=C:\\logigng.log
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
+1

, . INFO DEBUG - :

log4j.rootLogger=DEBUG, file, stdout

:

logger.debug("Hello world");

.

0

, . , - . - maven .

log4j.properties :

# Root logger option
log4j.rootLogger=DEBUG, file

# Direct log messages to a log file
# configuration to print into file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=10
# Define the layout for file appender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Set the name of the file
log4j.appender.file.File=C:\\log\\logging.log
# Set the append to false, overwrite
log4j.appender.file.Append=false

POM:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.6.2</version>
</dependency>

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
</dependency>

Yes, he created for me the file I needed, but the logs were in the console. Then I changed it to another dependency, for example:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
</dependency>

And I finally got this in the file instead of the console. Even without any explicit commands, such as PropertyConfigurator.configure().

0
source

Source: https://habr.com/ru/post/1545597/


All Articles