6

I don't use config or xml files for configuring log4j as there no need to configure it deeply. I only need to print messages to the console. so I do this:

BasicConfigurator.configure();

and to print only info and higher messages I do this:

Logger LOG = Logger.getLogger(MyClass.class)
LOG.setLevel(Level.INFO);

But nevertheless I see debug messages in the console too. But I only need info and actually error messages to be printed.

How to do this?

UPD: I created a config file with the which contains, as described in the tutorial here: manual the following:

  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

log4j.logger.com.messagedna.facade=INFO
log4j.logger.com.messagedna.receiver=INFO
log4j.logger.com.messagedna.util=INFO
log4j.logger.com.messagedna.parser=INFO

I put it to the com.messagedna

In every class I need logger in I wrote the following:

Properties props = new Properties();
try {
    props.load(new FileInputStream("/log4j.properties"));
    } catch (Exception e){
      LOG.error(e);
      }
PropertyConfigurator.configure(props);

but when I run my app I get the following:

log4j:WARN No appenders could be found for logger (com.messagedna.server.util.Config).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Nikitin Mikhail
  • 2,983
  • 9
  • 42
  • 67

3 Answers3

4

You are only setting the level for messages logged using the logger for MyClass.class, however any messages at debug level on other loggers will still get printed.

What you really need to do is to set the log level on the console log handler (which is responsible for printing all log messages to the console). However doing this whilst using BasicConfigurator will be quite tricky to do, you are better off moving to having your logging configuration specified in a properties file (which is much more flexible).

I would advise working through the log4j logging configuration tutorial - this will help you put together exactly the configuration you want, and should prove a worthwhile investment of your time. However, if you want to get this done quickly, try adding the content below to the file log4j.properties (example taken from here):

# 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

And then changing your code to do the following:

Properties props = new Properties();
props.load(new FileInputStream("/my/path/to/log4j.properties"));
PropertyConfigurator.configure(props);
robjohncox
  • 3,639
  • 3
  • 25
  • 51
  • where shall I get Properties? – Nikitin Mikhail Jul 03 '13 at 08:35
  • This is found at `java.util.Properties` - sorry should have made that clear – robjohncox Jul 03 '13 at 08:36
  • If I use your variant i get the following: log4j:WARN No appenders could be found for logger (com.messagedna.server.util.Config). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. – Nikitin Mikhail Jul 03 '13 at 08:46
  • According to the link in the error, 'No appenders could be found for logger' happens when the configuration file cannot be found - did you set the path to the file correctly? The configuration is setting up the root logger, therefore there should be an appender for the logger `com.messagedna.server.util.Config` if the configuration is read. – robjohncox Jul 03 '13 at 08:50
  • ok, I configured settings for every class and still have the same output – Nikitin Mikhail Jul 03 '13 at 11:41
  • This shouldn't make a difference - the root logger is configured in the settings and should cover all the other loggers. All I suspect is that the logger configuration is not being read - could you print out the contents of `props` after calling `props.load` and verify that the properties in the file are all loaded into the Properties object? – robjohncox Jul 03 '13 at 11:44
  • I tryed to print the random property like System.out.println(props.getProperty("log4j.appender.stdout")) and it prints null – Nikitin Mikhail Jul 03 '13 at 12:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32826/discussion-between-robjohncox-and-nikitin-mikhail) – robjohncox Jul 03 '13 at 12:07
3

Configure log4j.properties:

log4j.rootLogger=DEBUG, A1

log4j.logger.com.MYPACKAGE=INFO, A1

log4j.appender.A1=org.apache.log4j.ConsoleAppender
surfealokesea
  • 4,971
  • 4
  • 28
  • 38
  • I see. and to which place shall I put this file? to the package I use logger in? – Nikitin Mikhail Jul 03 '13 at 08:17
  • Ok look: http://stackoverflow.com/questions/8965946/configuring-log4j-loggers-programmatically but I don't recommend it, why complicate everithing when not need it – surfealokesea Jul 03 '13 at 08:18
  • @NikitinMikhail it may seem simpler to have this in the code, but you will find it much more powerful and flexible to move the config to a file (this will become more and more useful as your application grows) and it's really not much effort to move to using a configuration file. – robjohncox Jul 03 '13 at 08:21
  • As you need for your project!! @robjohncox is giving you the alternative – surfealokesea Jul 03 '13 at 08:26
1

Since you want to use to do it only by code

Heres a alternative.

Do this at the start, a static code initialization will work

logger = Logger.getLogger(Class.getName(), factory);
logger.setLevel(Level.INFO);

Having said that, theres no excuse for not using log4j.xml. Its very easy to use one.

S Kr
  • 1,831
  • 2
  • 25
  • 50