@@ -45,8 +45,8 @@ class GcpJsonFormatterTest {
4545
4646 private static final String LOG_TEMPLATE =
4747 """
48- {"severity":"@@SEVERITY@@","logging.googleapis.com/sourceLocation":{"file":"GcpJsonFormatterTest.java","line":"@@LINE@@","function":"google.registry.util.GcpJsonFormatterTest.@@FUNCTION@@"},"message":"@@MESSAGE@@"}
49- """ ;
48+ {"severity":"@@SEVERITY@@","logging.googleapis.com/sourceLocation":{"file":"GcpJsonFormatterTest.java","line":"@@LINE@@","function":"google.registry.util.GcpJsonFormatterTest.@@FUNCTION@@"},"message":"@@MESSAGE@@"}
49+ """ ;
5050
5151 private static String makeJson (String severity , int line , String function , String message ) {
5252 return LOG_TEMPLATE
@@ -71,14 +71,15 @@ void beforeEach() {
7171 void afterEach () {
7272 jdkLogger .removeHandler (handler );
7373 GcpJsonFormatter .setCurrentTraceId (null );
74+ GcpJsonFormatter .unsetCurrentRequest ();
7475 }
7576
7677 @ Test
7778 void testSuccess () {
7879 logger .atInfo ().log ("Something I have to say" );
7980 handler .close ();
8081 String output = ostream .toString (StandardCharsets .US_ASCII );
81- assertThat (output ).isEqualTo (makeJson ("INFO" , 78 , "testSuccess" , "Something I have to say" ));
82+ assertThat (output ).isEqualTo (makeJson ("INFO" , 79 , "testSuccess" , "Something I have to say" ));
8283 }
8384
8485 @ Test
@@ -87,21 +88,37 @@ void testSuccess_traceId() {
8788 logger .atInfo ().log ("Something I have to say" );
8889 handler .close ();
8990 String output = ostream .toString (StandardCharsets .US_ASCII );
90- String expected = makeJson ("INFO" , 87 , "testSuccess_traceId" , "Something I have to say" );
91+ String expected = makeJson ("INFO" , 88 , "testSuccess_traceId" , "Something I have to say" );
9192 // Remove the last two characters (}, \n) from the template and add the trace ID.
9293 expected =
9394 expected .substring (0 , expected .length () - 2 )
9495 + ",\" logging.googleapis.com/trace\" :\" trace_id\" }\n " ;
9596 assertThat (output ).isEqualTo (expected );
9697 }
9798
99+ @ Test
100+ void testSuccess_currentRequest () {
101+ GcpJsonFormatter .setCurrentRequest ("GET" , "/path" , "My-Agent" , "HTTP/1.1" );
102+ logger .atInfo ().log ("Something I have to say" );
103+ handler .close ();
104+ String output = ostream .toString (StandardCharsets .US_ASCII );
105+ String expected =
106+ makeJson ("INFO" , 102 , "testSuccess_currentRequest" , "Something I have to say" );
107+ // Remove the last two characters (}, \n) from the template and add the request.
108+ expected =
109+ expected .substring (0 , expected .length () - 2 )
110+ + ",\" httRequest\" :{\" requestMethod\" :\" GET\" ,\" requestUrl\" :\" /path\" "
111+ + ",\" userAgent\" :\" My-Agent\" ,\" protocol\" :\" HTTP/1.1\" }}\n " ;
112+ assertThat (output ).isEqualTo (expected );
113+ }
114+
98115 @ Test
99116 void testSuccess_logLevel () {
100117 logger .atSevere ().log ("Something went terribly wrong" );
101118 handler .close ();
102119 String output = ostream .toString (StandardCharsets .US_ASCII );
103120 assertThat (output )
104- .isEqualTo (makeJson ("ERROR" , 100 , "testSuccess_logLevel" , "Something went terribly wrong" ));
121+ .isEqualTo (makeJson ("ERROR" , 117 , "testSuccess_logLevel" , "Something went terribly wrong" ));
105122 }
106123
107124 @ Test
@@ -110,7 +127,7 @@ void testSuccess_withCause() {
110127 handler .close ();
111128 String output = ostream .toString (StandardCharsets .US_ASCII );
112129 String prefix =
113- makeJson ("ERROR" , 109 , "testSuccess_withCause" , "Something went terribly wrong" );
130+ makeJson ("ERROR" , 126 , "testSuccess_withCause" , "Something went terribly wrong" );
114131 // Remove the last two characters (}, \n) from the template as the actual output contains
115132 // the full stack trace.
116133 prefix = prefix .substring (0 , prefix .length () - 2 );
@@ -124,7 +141,7 @@ void testSuccess_withStackTrace() {
124141 handler .close ();
125142 String output = ostream .toString (StandardCharsets .US_ASCII );
126143 String prefix =
127- makeJson ("ERROR" , 123 , "testSuccess_withStackTrace" , "Something is worth checking" );
144+ makeJson ("ERROR" , 140 , "testSuccess_withStackTrace" , "Something is worth checking" );
128145 // Remove the last three characters (}, \n) from the template as the actual output contains
129146 // the full stack trace.
130147 prefix = prefix .substring (0 , prefix .length () - 2 );
@@ -150,7 +167,7 @@ void testSuccess_timeLimiter() throws Exception {
150167 TimeLimiter .create ().callWithTimeout (this ::logSomething , 1 , TimeUnit .SECONDS );
151168 handler .close ();
152169 String output = ostream .toString (StandardCharsets .US_ASCII );
153- assertThat (output ).isEqualTo (makeJson ("INFO" , 171 , "logSomething" , "Something I have to say" ));
170+ assertThat (output ).isEqualTo (makeJson ("INFO" , 188 , "logSomething" , "Something I have to say" ));
154171 }
155172
156173 @ Test
@@ -159,7 +176,7 @@ void testSuccess_timeLimiter_traceId() throws Exception {
159176 TimeLimiter .create ().callWithTimeout (this ::logSomething , 1 , TimeUnit .SECONDS );
160177 handler .close ();
161178 String output = ostream .toString (StandardCharsets .US_ASCII );
162- String expected = makeJson ("INFO" , 171 , "logSomething" , "Something I have to say" );
179+ String expected = makeJson ("INFO" , 188 , "logSomething" , "Something I have to say" );
163180 // Remove the last two characters (}, \n) from the template and add the trace ID.
164181 expected =
165182 expected .substring (0 , expected .length () - 2 )
0 commit comments