-
-
Notifications
You must be signed in to change notification settings - Fork 730
/
JDALogger.java
151 lines (140 loc) · 4.84 KB
/
JDALogger.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright 2015-2017 Austin Keener & Michael Ritter & Florian Spieß
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.core.utils;
import org.apache.commons.collections4.map.CaseInsensitiveMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;
/**
* This class serves as a LoggerFactory for JDA's internals.
* <br>It will either return a Logger from a SLF4J implementation via {@link org.slf4j.LoggerFactory} if present,
* or an instance of a custom {@link SimpleLogger} (From slf4j-simple).
* <p>
* It also has the utility method {@link #getLazyString(LazyEvaluation)} which is used to lazily construct Strings for Logging.
*/
public class JDALogger
{
/**
* Marks whether or not a SLF4J <code>StaticLoggerBinder</code> was found. If false, JDA will use its fallback logger.
* <br>This variable is initialized during static class initialization.
*/
public static final boolean SLF4J_ENABLED;
static
{
boolean tmp = false;
try
{
Class.forName("org.slf4j.impl.StaticLoggerBinder");
tmp = true;
}
catch (ClassNotFoundException e)
{
//prints warning of missing implementation
LoggerFactory.getLogger(JDALogger.class);
}
SLF4J_ENABLED = tmp;
}
private static final Map<String, Logger> LOGS = new CaseInsensitiveMap<>();
private JDALogger() {}
/**
* Will get the {@link org.slf4j.Logger} with the given log-name
* or create and cache a fallback logger if there is no SLF4J implementation present.
* <p>
* The fallback logger will be an instance of a slightly modified version of SLF4Js SimpleLogger.
*
* @param name
* The name of the Logger
*
* @return Logger with given log name
*/
public static Logger getLog(String name)
{
synchronized (LOGS)
{
if (SLF4J_ENABLED)
return LoggerFactory.getLogger(name);
return LOGS.computeIfAbsent(name, SimpleLogger::new);
}
}
/**
* Will get the {@link org.slf4j.Logger} for the given Class
* or create and cache a fallback logger if there is no SLF4J implementation present.
* <p>
* The fallback logger will be an instance of a slightly modified version of SLF4Js SimpleLogger.
*
* @param clazz
* The class used for the Logger name
*
* @return Logger for given Class
*/
public static Logger getLog(Class<?> clazz)
{
synchronized (LOGS)
{
if (SLF4J_ENABLED)
return LoggerFactory.getLogger(clazz);
return LOGS.computeIfAbsent(clazz.getName(), (n) -> new SimpleLogger(clazz.getSimpleName()));
}
}
/**
* Utility function to enable logging of complex statements more efficiently (lazy).
*
* @param lazyLambda
* The Supplier used when evaluating the expression
*
* @return An Object that can be passed to SLF4J's logging methods as lazy parameter
*/
public static Object getLazyString(LazyEvaluation lazyLambda)
{
return new Object()
{
@Override
public String toString()
{
try
{
return lazyLambda.getString();
}
catch(Exception ex)
{
StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
return "Error while evaluating lazy String... " + sw.toString();
}
}
};
}
/**
* Functional interface used for {@link #getLazyString(LazyEvaluation)} to lazily construct a String.
*/
@FunctionalInterface
public interface LazyEvaluation
{
/**
* This method is used by {@link #getLazyString(LazyEvaluation)}
* when SLF4J requests String construction.
* <br>The String returned by this is used to construct the log message.
*
* @throws Exception
* To allow lazy evaluation of methods that might throw exceptions
*
* @return The String for log message
*/
String getString() throws Exception;
}
}