Log exceptions and messages to a file

Discussion in 'Resources' started by p000ison, Jul 12, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    p000ison

    Heyho Community,

    I wrote this some time ago and I saw this: http://forums.bukkit.org/threads/making-a-log-file-for-your-plugins.85430/

    And I think this is more a logging system :p
    So yeah you only have to initialise this once if you want to log to a file.

    Code:
    /*
     * Copyright (C) 2012 p000ison
     * 
     * This work is licensed under the Creative Commons
     * Attribution-NonCommercial-NoDerivs 3.0 Unported License. To view a copy of
     * this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ or send
     * a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco,
     * California, 94105, USA.
     * 
     */
     
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.FileHandler;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.util.logging.SimpleFormatter;
    import org.bukkit.plugin.Plugin;
     
    /**
     * This should be get initialised when the plugin loads (It contains the Logging
     * system)
     *
     * @author Max
     */
    public class Logging
    {
     
        private static Logger log;
        private static final Logger debugLogger = Logger.getLogger("Logging");
        private FileHandler debugFileHandler;
     
        /**
         * Gets the logger for your plugin
         *
         * @param plugin The plugin to apply
         */
        public Logging(Plugin plugin)
        {
     
            log = plugin.getLogger();
     
            try {
     
                debugFileHandler = new FileHandler(plugin.getDataFolder().getAbsolutePath() + File.pathSeparator + plugin.getName() + ".log", true);
                SimpleFormatter formatter = new SimpleFormatter();
     
                debugLogger.addHandler(debugFileHandler);
                debugFileHandler.setFormatter(formatter);
     
            } catch (IOException ex) {
                debug(null, ex, false);
            } catch (SecurityException ex) {
                debug(null, ex, false);
            }
        }
     
        /**
         * Logs a message
         *
         * @param level The level to log
         * @param msg The message to log
         * @param toFile log to own log?
         */
        public static void debug(Level level, String msg, boolean toFile)
        {
            if (toFile) {
                if (debugLogger != null) {
                    debugLogger.log(level, msg);
                }
            }
            log.log(level, msg);
        }
     
        /**
         * Logs a Exception
         *
         * @param msg The message to log
         * @param exception the exception
         * @param toFile log to own log?
         */
        public static void debug(String msg, Throwable exception, boolean toFile)
        {
            if (toFile) {
                if (debugLogger != null) {
                    debugLogger.log(Level.SEVERE, msg, exception);
                }
            }
            log.log(Level.SEVERE, msg, exception);
        }
    }
    
     
    Xp10d3 and regaw_leinad like this.
Thread Status:
Not open for further replies.

Share This Page