1

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.

1 Answer 1

2

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.
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.