Repository navigation
Expand file tree
/
Copy pathShepherdLogManager.java
More file actions
74 lines (64 loc) · 2.18 KB
/
Copy pathShepherdLogManager.java
File metadata and controls
74 lines (64 loc) · 2.18 KB
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
package utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
public class ShepherdLogManager {
private static final Logger log = LogManager.getLogger(ShepherdLogManager.class);
public static void setRequestIp(String theIp) {
ThreadContext.put("RemoteAddress", theIp);
}
public static void logEvent(String theIp, String theMessage) {
setRequestIp(theIp);
log.debug(theMessage);
}
public static void setRequestIp(String theIp, String theForwardedIp) {
if (theForwardedIp != null
&& !theForwardedIp.isEmpty()) // If string is not null and not empty set normal message
{
ThreadContext.put("RemoteAddress", theIp + " from " + theForwardedIp);
} else // No Forward Header detected so Log that
{
ThreadContext.put("RemoteAddress", theIp + " from ?.?.?.?");
}
}
public static void logEvent(String theIp, String theForwardedIp, String theMessage) {
setRequestIp(theIp, theForwardedIp);
log.debug(theMessage);
}
/**
* Logs Event with username at beginning of log
*
* @param theIp
* @param theForwardedIp
* @param theMessage
* @param theUser
*/
public static void logEvent(
String theIp, String theForwardedIp, String theMessage, Object theUser) {
String userName = new String();
if (theUser != null) {
userName = theUser.toString();
}
if (userName.isEmpty()) {
userName = new String("UnknownUser");
}
setRequestIp(theIp, theForwardedIp, userName);
log.debug(theMessage);
}
/**
* Sets IP of request and preceeds it with the username of the logged in user
*
* @param theIp
* @param theForwardedIp
*/
public static void setRequestIp(String theIp, String theForwardedIp, String userName) {
if (theForwardedIp != null
&& !theForwardedIp.isEmpty()) // If string is not null and not empty set normal message
{
ThreadContext.put("RemoteAddress", userName + " at " + theIp + " from " + theForwardedIp);
} else // No Forward Header detected so Log that
{
ThreadContext.put("RemoteAddress", userName + " at " + theIp + " from ?.?.?.?");
}
}
}