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