// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package test;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.Introspector;

import javax.swing.JApplet;
import javax.swing.JButton;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class LogApplet extends JApplet {
    private Logger log = Logger.getLogger(LogApplet.class);
    private JButton debugbtn = new JButton("Log debug");
    private JButton infobtn = new JButton("Log info");
    private JButton warnbtn = new JButton("Log warning");
    private JButton errorbtn = new JButton("Log error");
    private JButton consolebtn = new JButton("Log to console");
    private JButton guibtn = new JButton("Log to GUI");
    private boolean console = false;
    private boolean gui = false;
    public void init() {
        //Introspector.setBeanInfoSearchPath(new String[0]);
        debugbtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                log.debug("This is a test");
            }
        });
        infobtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                log.info("This is a test");
            }
        });
        warnbtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                log.warn("This is a test");
            }
        });
        errorbtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                log.error("This is a test");
            }
        });
        consolebtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                console = !console;
                ConsoleAppender app = (ConsoleAppender)log.getAppender("console");
                if(console) {
                    app.setThreshold(Level.DEBUG);
                } else {
                    app.setThreshold(Level.OFF);
                }
            }
        });
        guibtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                gui = !gui;
                GuiAppender app = (GuiAppender)log.getAppender("gui");
                if(gui) {
                    app.setThreshold(Level.DEBUG);
                    app.open();
                } else {
                    app.setThreshold(Level.OFF);
                }
            }
        });
        getContentPane().setLayout(new GridLayout(3,2 ));
        getContentPane().add(debugbtn);
        getContentPane().add(infobtn);
        getContentPane().add(warnbtn);
        getContentPane().add(errorbtn);
        getContentPane().add(consolebtn);
        getContentPane().add(guibtn);
    
    }
}
