001package com.github.sarxos.webcam.log;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileNotFoundException;
006import java.io.IOException;
007import java.io.InputStream;
008
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012
013/**
014 * Configure loggers.
015 * 
016 * @author Bartosz Firyn (SarXos)
017 */
018public class WebcamLogConfigurator {
019
020        /**
021         * Logger instance.
022         */
023        private static final Logger LOG = LoggerFactory.getLogger(WebcamLogConfigurator.class);
024
025        /**
026         * Configure SLF4J.
027         * 
028         * @param is input stream to logback configuration xml
029         */
030        public static void configure(InputStream is) {
031
032                ClassLoader cl = Thread.currentThread().getContextClassLoader();
033
034                try {
035
036                        String[] names = {
037                                "ch.qos.logback.classic.LoggerContext",
038                                "ch.qos.logback.classic.joran.JoranConfigurator",
039                        };
040                        for (String name : names) {
041                                Class.forName(name, false, cl);
042                        }
043
044                        ch.qos.logback.classic.LoggerContext context = (ch.qos.logback.classic.LoggerContext) LoggerFactory.getILoggerFactory();
045                        ch.qos.logback.classic.joran.JoranConfigurator configurator = new ch.qos.logback.classic.joran.JoranConfigurator();
046                        configurator.setContext(context);
047                        context.reset();
048                        configurator.doConfigure(is);
049
050                } catch (ClassNotFoundException e) {
051                        System.err.println("WLogC: Logback JARs are missing in classpath");
052                } catch (NoClassDefFoundError e) {
053                        System.err.println("WLogC: Logback JARs are missing in classpath");
054                } catch (Throwable e) {
055                        e.printStackTrace();
056                }
057        }
058
059        /**
060         * Configure SLF4J.
061         * 
062         * @param file logback configuration file
063         */
064        public static void configure(File file) {
065                FileInputStream fis = null;
066                try {
067                        fis = new FileInputStream(file);
068                        configure(fis);
069                } catch (FileNotFoundException e) {
070                        LOG.error("File not found " + file, e);
071                        e.printStackTrace();
072                } finally {
073                        if (fis != null) {
074                                try {
075                                        fis.close();
076                                } catch (IOException e) {
077                                        LOG.error("Cannot close file " + file, e);
078                                        e.printStackTrace();
079                                }
080                        }
081                }
082        }
083
084        /**
085         * Configure SLF4J.
086         * 
087         * @param file logback configuration file path
088         */
089        public static void configure(String file) {
090                configure(new File(file));
091        }
092}