Ori Refael
Ori Refael

Reputation: 3008

Java Logger doesn't log to Console

I've a created to following class.

package bgu.spl.utils;

import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class AppLogger 
{
    private final static Logger _logger = Logger.getLogger("MyAppLogger");
    private static boolean _isInited = false;

    private synchronized static void init()
    {
        _logger.setLevel(Level.ALL);
        ConsoleHandler handler = new ConsoleHandler();
        handler.setFormatter(new SimpleFormatter());
        handler.setLevel(Level.ALL);
        _logger.addHandler(handler);

        _isInited = true;
    }

    public synchronized static void general (String text)
    {
        if (_isInited)
            init();

        System.out.println(text);
    }

    private static String tickStr(int tick)
    {
        return tick < 0 ? "" : (" - Tick: " + tick);
    }

    public synchronized static void severe (Class<?> type, String text, int tick)
    {
        if (_isInited)
            init();


        _logger.severe(type.getName() + tickStr(tick) + ". " + text);
    }

    public synchronized static void warning (Class<?> type, String text, int tick)
    {
        if (_isInited)
            init();

        _logger.warning(type.getName() + tickStr(tick) + ". " + text);
    }

    public synchronized static void info (Class<?> type, String text, int tick)
    {
        if (_isInited)
            init();

        _logger.info(type.getName() + tickStr(tick) + ". " + text);
    }

    public synchronized static void config (Class<?> type, String text, int tick)
    {
        if (_isInited)
            init();

        _logger.config(type.getName() + tickStr(tick) + ". " + text);
    }

    public synchronized static void fine (Class<?> type, String text, int tick)
    {
        if (_isInited)
            init();

        _logger.fine(type.getName() + tickStr(tick) + ". " + text);
    }

    public synchronized static void finer (Class<?> type, String text, int tick)
    {
        if (_isInited)
            init();

        _logger.finer(type.getName() + tickStr(tick) + ". " + text);
    }

    public synchronized static void finest (Class<?> type, String text, int tick)
    {
        if (_isInited)
            init();

        _logger.finest(type.getName() + tickStr(tick) + ". " + text);
    }
}

my problem is, that nothing is being printed to the console..ive tried mostly to play with the handlers and levels... of course that the normal System.out.println works. I've created this class to be my entire's multi-threaded app logger. Any suggestions? Thanks.

Upvotes: 1

Views: 176

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201399

In general, this

if (_isInited)
    init();

should be

if (!_isInited)
    init();

Otherwise it won't call init() when isInited is false. Alternatively, you could use a static initialization block like

static {
    init(); // <-- will run once.
}

Upvotes: 2

Related Questions