-1

I'm new to util.logging so this might seem a trivial question to some but anyway here goes..I'm using Java's Util.logger to log messages. I'm trying to declare a logger in a single class and accessing it from other classes for logging messages.

Logger class

package util;

import java.util.logging.*;

public class Logger {

public Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

 }

I'm trying to access this logger in other class as follows..

package mycode;

import util.Logger;

public class MYcode{

private void test(){
 LOGGER.fine("Sample message");
}
}

Compilation Error message..

error: cannot find symbol
[javac]LOGGER.fine("Sample message");

When I'm declaring logger in Mycode class then I'm not getting any error but when declaring it another class I'm getting an error. Any idea where I'm going wrong??

Lucy
  • 1,812
  • 15
  • 55
  • 93

2 Answers2

0

Just keep using in all classes needing the logger the same line:

public Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

This way you are creating many references to one instance of logger identified by the global name.

diginoise
  • 7,352
  • 2
  • 31
  • 39
0

The problem is your 'LOGGER' is NOT static, so you are NOT able to access.

Logger class:

package util;

import java.util.logging.*;

public class Logger {

public static final Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

 }

Accessing logger in other class:

package mycode;

        import util.Logger;

        public class MYcode{

        public void test(){
         LOGGER.fine("Sample message");

        }    
    }
Vasu
  • 21,832
  • 11
  • 51
  • 67
  • By mistake I made test method public in my question.It's actually private, I've edited my question accordingly..will your answer work then also?? – Lucy Mar 04 '16 at 12:32