1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.util.internal.logging;
18
19 import io.netty.util.internal.ObjectUtil;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 public abstract class InternalLoggerFactory {
37
38 private static volatile InternalLoggerFactory defaultFactory;
39
40 @SuppressWarnings("UnusedCatchParameter")
41 private static InternalLoggerFactory newDefaultFactory(String name) {
42 InternalLoggerFactory f = useSlf4JLoggerFactory(name);
43 if (f != null) {
44 return f;
45 }
46
47 f = useLog4J2LoggerFactory(name);
48 if (f != null) {
49 return f;
50 }
51
52 f = useLog4JLoggerFactory(name);
53 if (f != null) {
54 return f;
55 }
56
57 return useJdkLoggerFactory(name);
58 }
59
60 private static InternalLoggerFactory useSlf4JLoggerFactory(String name) {
61 try {
62 InternalLoggerFactory f = Slf4JLoggerFactory.getInstanceWithNopCheck();
63 f.newInstance(name).debug("Using SLF4J as the default logging framework");
64 return f;
65 } catch (LinkageError ignore) {
66 return null;
67 } catch (Exception ignore) {
68
69 return null;
70 }
71 }
72
73 private static InternalLoggerFactory useLog4J2LoggerFactory(String name) {
74 try {
75 InternalLoggerFactory f = Log4J2LoggerFactory.INSTANCE;
76 f.newInstance(name).debug("Using Log4J2 as the default logging framework");
77 return f;
78 } catch (LinkageError ignore) {
79 return null;
80 } catch (Exception ignore) {
81
82 return null;
83 }
84 }
85
86 private static InternalLoggerFactory useLog4JLoggerFactory(String name) {
87 try {
88 InternalLoggerFactory f = Log4JLoggerFactory.INSTANCE;
89 f.newInstance(name).debug("Using Log4J as the default logging framework");
90 return f;
91 } catch (LinkageError ignore) {
92 return null;
93 } catch (Exception ignore) {
94
95 return null;
96 }
97 }
98
99 private static InternalLoggerFactory useJdkLoggerFactory(String name) {
100 InternalLoggerFactory f = JdkLoggerFactory.INSTANCE;
101 f.newInstance(name).debug("Using java.util.logging as the default logging framework");
102 return f;
103 }
104
105
106
107
108
109 public static InternalLoggerFactory getDefaultFactory() {
110 if (defaultFactory == null) {
111 defaultFactory = newDefaultFactory(InternalLoggerFactory.class.getName());
112 }
113 return defaultFactory;
114 }
115
116
117
118
119 public static void setDefaultFactory(InternalLoggerFactory defaultFactory) {
120 InternalLoggerFactory.defaultFactory = ObjectUtil.checkNotNull(defaultFactory, "defaultFactory");
121 }
122
123
124
125
126 public static InternalLogger getInstance(Class<?> clazz) {
127 return getInstance(clazz.getName());
128 }
129
130
131
132
133 public static InternalLogger getInstance(String name) {
134 return getDefaultFactory().newInstance(name);
135 }
136
137
138
139
140 protected abstract InternalLogger newInstance(String name);
141
142 }