/
WebcamLogConfigurator.java
89 lines (77 loc) · 2.05 KB
/
WebcamLogConfigurator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.github.sarxos.webcam.log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
/**
* Configure loggers.
*
* @author Bartosz Firyn (SarXos)
*/
public class WebcamLogConfigurator {
/**
* Logger instance.
*/
private static final Logger LOG = LoggerFactory.getLogger(WebcamLogConfigurator.class);
/**
* Configure SLF4J.
*
* @param is input stream to logback configuration xml
*/
public static void configure(InputStream is) {
try {
Class.forName("ch.qos.logback.classic.LoggerContext");
} catch (ClassNotFoundException e1) {
LOG.error("Cannot configure logger because logback LoggerContext is not available in classpath");
return;
}
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
context.reset();
try {
configurator.doConfigure(is);
} catch (JoranException e) {
LOG.error("Joran configuration exception", e);
e.printStackTrace();
}
}
/**
* Configure SLF4J.
*
* @param file logback configuration file
*/
public static void configure(File file) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
configure(fis);
} catch (FileNotFoundException e) {
LOG.error("File not found " + file, e);
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
LOG.error("Cannot close file " + file, e);
e.printStackTrace();
}
}
}
}
/**
* Configure SLF4J.
*
* @param file logback configuration file path
*/
public static void configure(String file) {
configure(new File(file));
}
}