2222import ch .qos .logback .classic .spi .ILoggingEvent ;
2323import ch .qos .logback .core .ConsoleAppender ;
2424import ch .qos .logback .core .FileAppender ;
25+ import com .google .gson .JsonArray ;
26+ import com .google .gson .JsonObject ;
27+ import com .google .gson .JsonPrimitive ;
2528import io .supertokens .pluginInterface .LOG_LEVEL ;
2629import io .supertokens .storage .postgresql .ResourceDistributor ;
2730import io .supertokens .storage .postgresql .Start ;
2831import io .supertokens .storage .postgresql .config .Config ;
2932import io .supertokens .storage .postgresql .utils .Utils ;
3033import org .slf4j .LoggerFactory ;
3134
35+ import java .io .ByteArrayOutputStream ;
36+ import java .io .PrintStream ;
37+
3238public class Logging extends ResourceDistributor .SingletonResource {
3339
3440 private static final String RESOURCE_ID = "io.supertokens.storage.postgresql.output.Logging" ;
@@ -65,24 +71,59 @@ public static void debug(Start start, String msg) {
6571 try {
6672 msg = msg .trim ();
6773 if (getInstance (start ) != null ) {
68- getInstance (start ).infoLogger .debug (msg );
74+ getInstance (start ).infoLogger .debug (getFormattedMessage ( msg ) );
6975 }
7076 } catch (NullPointerException ignored ) {
7177 }
7278 }
7379
80+ public static String throwableStacktraceToString (Throwable e ) {
81+ if (e == null ) {
82+ return "" ;
83+ }
84+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
85+ try (PrintStream ps = new PrintStream (baos )) {
86+ e .printStackTrace (ps );
87+ }
88+ return baos .toString ();
89+ }
90+
91+ private static String getFormattedMessage (String msg , Exception e ) {
92+ JsonObject msgObj = new JsonObject ();
93+ msgObj .addProperty ("message" , msg );
94+
95+ if (e != null ) {
96+ String stackTrace = throwableStacktraceToString (e );
97+ String [] stackTraceArr = stackTrace .split ("\n " );
98+ JsonArray stackTraceArrObj = new JsonArray ();
99+ for (String stackTraceElement : stackTraceArr ) {
100+ stackTraceArrObj .add (new JsonPrimitive (stackTraceElement ));
101+ }
102+
103+ msgObj .add ("exception" , stackTraceArrObj );
104+ }
105+
106+ return msgObj .toString ();
107+ }
108+
109+ private static String getFormattedMessage (String msg ) {
110+ return getFormattedMessage (msg , null );
111+ }
112+
74113 public static void info (Start start , String msg , boolean toConsoleAsWell ) {
75114 if (!Config .getLogLevels (start ).contains (LOG_LEVEL .INFO )) {
76115 return ;
77116 }
78117 try {
79118 msg = msg .trim ();
80- if (getInstance (start ) != null ) {
81- getInstance (start ).infoLogger .info (msg );
82- }
83119 if (toConsoleAsWell ) {
84120 systemOut (msg );
85121 }
122+ msg = getFormattedMessage (msg );
123+
124+ if (getInstance (start ) != null ) {
125+ getInstance (start ).infoLogger .info (msg );
126+ }
86127 } catch (NullPointerException ignored ) {
87128 }
88129 }
@@ -93,6 +134,7 @@ public static void warn(Start start, String msg) {
93134 }
94135 try {
95136 msg = msg .trim ();
137+ msg = getFormattedMessage (msg );
96138 if (getInstance (start ) != null ) {
97139 getInstance (start ).errorLogger .warn (msg );
98140 }
@@ -113,7 +155,7 @@ public static void error(Start start, String err, boolean toConsoleAsWell) {
113155 try {
114156 err = err .trim ();
115157 if (getInstance (start ) != null ) {
116- getInstance (start ).errorLogger .error (err );
158+ getInstance (start ).errorLogger .error (getFormattedMessage ( err ) );
117159 }
118160 if (toConsoleAsWell || getInstance (start ) == null ) {
119161 systemErr (err );
@@ -122,7 +164,8 @@ public static void error(Start start, String err, boolean toConsoleAsWell) {
122164 }
123165 }
124166
125- public static void error (Start start , String message , boolean toConsoleAsWell , Exception e ) {
167+ public static void error (Start start , String message , boolean toConsoleAsWell ,
168+ Exception e ) {
126169 try {
127170 if (!Config .getLogLevels (start ).contains (LOG_LEVEL .ERROR )) {
128171 return ;
@@ -142,7 +185,7 @@ public static void error(Start start, String message, boolean toConsoleAsWell, E
142185 if (message != null ) {
143186 message = message .trim ();
144187 if (getInstance (start ) != null ) {
145- getInstance (start ).errorLogger .error (message );
188+ getInstance (start ).errorLogger .error (getFormattedMessage ( message , e ) );
146189 }
147190 if (toConsoleAsWell || getInstance (start ) == null ) {
148191 systemErr (message );
@@ -220,5 +263,4 @@ private Logger createLoggerForConsole(Start start, String name, LOG_LEVEL logLev
220263
221264 return logger ;
222265 }
223-
224266}
0 commit comments