Doug Hauf Doug Hauf - 2 months ago 12
Java Question

Log4J not working properly?

I am new to Log4J. i am trying to get this logger to work. i downloaded the .JAR file and I have built that into the Referenced Libraries and can open the folder and see it in there. So I think it is built into my project.

I think that I am missing how to declare the logger so that i can use it below. Some of the code I got from stackoverflow.

Question: Which part of the Log4J declaration am I missing so that I can use it as a logger in my simple example?

Code:

import java.io.*;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class TestLogs {
private static Logger logger = Logger.getLogger(FileChooserDemo.class.getName());

FileHandler fh;

public TestLogs() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
fh = new FileHandler("C:\\Users\\itpr13266\\Desktop\\MyNewLogFile.log");
logger.addHandler(fh);
logger.info("Logging an INFO-level message");
logger.warning("Your file cannot be written!!");
} catch (Exception ex) {
Logger.getLogger(FileChooserDemo.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void LogTester() {
Logger.getRootLogger().getLoggerRepository().resetConfiguration();
ConsoleAppender console = new ConsoleAppender(); //create appender
//configure the appender
String PATTERN = "%d [%p|%c|%C{1}] %m%n";
console.setLayout(new PatternLayout(PATTERN));
console.setThreshold(Level.FATAL);
console.activateOptions();
//add appender to any Logger (here is root)
Logger.getRootLogger().addAppender(console);

FileAppender fa = new FileAppender();
fa.setName("FileLogger");
fa.setFile("mylog.log");
fa.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
fa.setThreshold(Level.DEBUG);
fa.setAppend(true);
fa.activateOptions();

//add appender to any Logger (here is root)
Logger.getRootLogger().addAppender(fa)
}

public static void main(String[] args) {
TestLogs tls = new TestLogs();
tls.LogTester();
}
}


enter image description here

new image- I rebuilt the paths and it put them in a slightly different position in the file structure. But the error still persists.

enter image description here

Code that works --- But how do I log it to a text file on my computer.

package TestMenu;

import org.apache.log4j.*;

import javax.swing.*;

public class TestLogs {
private static Logger logger = Logger.getLogger(TestLogs.class.getName());

public TestLogs() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
}

private void LogTester() {
logger.info("It works");
TestLogs tls = new TestLogs();
}

public static void main(String[] args) {
TestLogs tls = new TestLogs();
tls.LogTester();
}
}


At least the log4j is working but how to change the properties file around so that I can log all warnings, info and to a file not to the console?

Log4j Properties:

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to 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=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


new image -- not working -- error shown on bottom

enter image description here

enter image description here

Errors:

log4j:WARN No appenders could be found for logger (TestMenu.TestLogs).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.


enter image description here

vkg vkg
Answer

You are trying to use java util logging not log4j. you need to import

import org.apache.log4j.Logger;

Another thing I see that is wrong in your code is

change this:

private static Logger logger = Logger.getLogger(FileChooserDemo.class.getName());

To

private static Logger logger = Logger.getLogger(TestLogs.class.getName());

Here is a sample

package com.vkg;

import org.apache.log4j.Logger;

public class TestLogs {
    private static Logger logger = Logger.getLogger(TestLogs.class.getName());

    public static void main(String[] args) {
        logger.info("It works");
        TestLogs tls = new TestLogs();
    }
}

A basic log4j config file

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

# Write to file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=[sompath to your log file e.g. C:\\mylog.log]
log4j.appender.file.MaxFileSize=50MB
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

# Write to console
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

And output

2014-05-12 13:18:01 INFO  TestLogs:9 - It works

Image below shows where to keep log4j.properties

enter image description here