1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.util.internal.logging;
17
18 import java.util.concurrent.atomic.AtomicReference;
19
20 import static java.util.Objects.requireNonNull;
21
22
23
24
25
26
27
28
29
30
31 public abstract class InternalLoggerFactory {
32
33
34
35
36 static final class InternalLoggerFactoryHolder {
37 static final InternalLoggerFactoryHolder HOLDER = new InternalLoggerFactoryHolder();
38
39 private final AtomicReference<InternalLoggerFactory> reference;
40
41 InternalLoggerFactoryHolder() {
42 reference = new AtomicReference<>();
43 }
44
45 InternalLoggerFactoryHolder(final InternalLoggerFactory delegate) {
46 reference = new AtomicReference<>(delegate);
47 }
48
49 InternalLoggerFactory getFactory() {
50 InternalLoggerFactory factory = reference.get();
51 if (factory == null) {
52 factory = newDefaultFactory(InternalLoggerFactory.class.getName());
53 if (!reference.compareAndSet(null, factory)) {
54 factory = reference.get();
55 }
56 }
57 return factory;
58 }
59
60 void setFactory(final InternalLoggerFactory factory) {
61 requireNonNull(factory, "factory");
62 if (!reference.compareAndSet(null, factory)) {
63 throw new IllegalStateException(
64 "factory is already set to [" + reference + "], rejecting [" + factory + ']');
65 }
66 }
67
68 InternalLogger getInstance(final Class<?> clazz) {
69 return getInstance(clazz.getName());
70 }
71
72 InternalLogger getInstance(final String name) {
73 return newInstance(name);
74 }
75
76 InternalLogger newInstance(String name) {
77 return getFactory().newInstance(name);
78 }
79
80 private static InternalLoggerFactory newDefaultFactory(String name) {
81 InternalLoggerFactory f = useSlf4JLoggerFactory(name);
82 if (f != null) {
83 return f;
84 }
85
86 f = useLog4J2LoggerFactory(name);
87 if (f != null) {
88 return f;
89 }
90
91 return useJdkLoggerFactory(name);
92 }
93
94 private static InternalLoggerFactory useSlf4JLoggerFactory(String name) {
95 try {
96 InternalLoggerFactory f = Slf4JLoggerFactory.getInstanceWithNopCheck();
97 f.newInstance(name).debug("Using SLF4J as the default logging framework");
98 return f;
99 } catch (LinkageError | Exception ignore) {
100 return null;
101 }
102 }
103
104 private static InternalLoggerFactory useLog4J2LoggerFactory(String name) {
105 try {
106 InternalLoggerFactory f = Log4J2LoggerFactory.INSTANCE;
107 f.newInstance(name).debug("Using Log4J2 as the default logging framework");
108 return f;
109 } catch (LinkageError | Exception ignore) {
110 return null;
111 }
112 }
113
114 private static InternalLoggerFactory useJdkLoggerFactory(String name) {
115 InternalLoggerFactory f = JdkLoggerFactory.INSTANCE;
116 f.newInstance(name).debug("Using java.util.logging as the default logging framework");
117 return f;
118 }
119 }
120
121
122
123
124
125 public static InternalLoggerFactory getDefaultFactory() {
126 return InternalLoggerFactoryHolder.HOLDER.getFactory();
127 }
128
129
130
131
132
133
134
135 public static void setDefaultFactory(InternalLoggerFactory defaultFactory) {
136 requireNonNull(defaultFactory, "defaultFactory");
137 InternalLoggerFactoryHolder.HOLDER.setFactory(defaultFactory);
138 }
139
140
141
142
143 public static InternalLogger getInstance(Class<?> clazz) {
144 return InternalLoggerFactoryHolder.HOLDER.getInstance(clazz);
145 }
146
147
148
149
150 public static InternalLogger getInstance(String name) {
151 return InternalLoggerFactoryHolder.HOLDER.getInstance(name);
152 }
153
154
155
156
157 protected abstract InternalLogger newInstance(String name);
158 }