@@ -39,15 +39,22 @@ public class LogUtil {
3939 * @param t the exception to log
4040 */
4141 public static void logThrowable (Log log , String message , Throwable t ) {
42+ String throwableString = constructThrowableString (message , t );
43+ log .onErrorEvent (throwableString );
44+ }
45+
46+ private static String constructThrowableString (String message , Throwable t ) {
4247 final StringWriter stringWriter = new StringWriter ();
4348 final PrintWriter printWriter = new PrintWriter (stringWriter );
4449 printWriter .println (message );
45- t .printStackTrace (printWriter );
46- if (t .getCause () != null ) {
47- printWriter .println ("Cause: " + t .getCause ().getMessage ());
48- t .getCause ().printStackTrace (printWriter );
50+ if (t != null ) {
51+ t .printStackTrace (printWriter );
52+ if (t .getCause () != null ) {
53+ printWriter .println ("Cause: " + t .getCause ().getMessage ());
54+ t .getCause ().printStackTrace (printWriter );
55+ }
4956 }
50- log . onErrorEvent ( stringWriter .toString () );
57+ return stringWriter .toString ();
5158 }
5259
5360 /**
@@ -68,5 +75,51 @@ public static void logThrowable(SessionID sessionID, String message, Throwable t
6875 log .error (message , t );
6976 }
7077 }
78+
79+ /**
80+ * Logs a throwable including the stack trace as a session warning event.
81+ * If session cannot be found, the general log is used.
82+ *
83+ * @param sessionID sessionID of Session to lookup
84+ * @param message the message to log
85+ * @param throwable throwable to log
86+ */
87+ public static void logWarning (SessionID sessionID , String message , Throwable throwable ) {
88+ String throwableString = constructThrowableString (message , throwable );
89+ logWarning (sessionID , throwableString );
90+ }
91+
92+ /**
93+ * Logs a warning as a session event if the session is registered, otherwise
94+ * the general log is used.
95+ *
96+ * @param sessionID sessionID of Session to lookup
97+ * @param message the message to log
98+ */
99+ public static void logWarning (SessionID sessionID , String message ) {
100+ final Session session = Session .lookupSession (sessionID );
101+ final String messageToLog ;
102+ if (session != null ) {
103+ messageToLog = message ;
104+ } else {
105+ messageToLog = message + " sessionID=" + sessionID ;
106+ }
107+ logWarning (session , messageToLog );
108+ }
109+
110+ /**
111+ * Logs a warning as a session event if the session is not NULL, otherwise
112+ * the general log is used.
113+ *
114+ * @param session the session to use
115+ * @param message the message to log
116+ */
117+ static void logWarning (final Session session , String message ) {
118+ if (session != null ) {
119+ session .getLog ().onWarnEvent (message );
120+ } else {
121+ log .warn (message );
122+ }
123+ }
71124
72125}
0 commit comments