1414
1515package helloworld ;
1616
17+ import static software .amazon .lambda .powertools .logging .argument .StructuredArguments .entry ;
1718import static software .amazon .lambda .powertools .metrics .MetricsUtils .metricsLogger ;
1819import static software .amazon .lambda .powertools .metrics .MetricsUtils .withSingleMetric ;
1920import static software .amazon .lambda .powertools .tracing .TracingUtils .putMetadata ;
3132import java .util .stream .Collectors ;
3233import org .apache .logging .log4j .LogManager ;
3334import org .apache .logging .log4j .Logger ;
35+ import org .slf4j .MDC ;
3436import software .amazon .cloudwatchlogs .emf .model .DimensionSet ;
3537import software .amazon .cloudwatchlogs .emf .model .Unit ;
3638import software .amazon .lambda .powertools .logging .Logging ;
37- import software .amazon .lambda .powertools .logging .LoggingUtils ;
3839import software .amazon .lambda .powertools .metrics .Metrics ;
3940import software .amazon .lambda .powertools .tracing .CaptureMode ;
4041import software .amazon .lambda .powertools .tracing .Tracing ;
4445 * Handler for requests to Lambda function.
4546 */
4647public class App implements RequestHandler <APIGatewayProxyRequestEvent , APIGatewayProxyResponseEvent > {
47- private final static Logger log = LogManager .getLogger (App .class );
48+ private static final Logger log = LogManager .getLogger (App .class );
4849
49- // This is controlled by POWERTOOLS_LOGGER_SAMPLE_RATE environment variable
50- // @Logging(logEvent = true, samplingRate = 0.7)
51- // This is controlled by POWERTOOLS_METRICS_NAMESPACE environment variable
52- // @Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)
53- // This is controlled by POWERTOOLS_TRACER_CAPTURE_ERROR environment variable
50+ @ Logging (logEvent = true , samplingRate = 0.7 )
5451 @ Tracing (captureMode = CaptureMode .RESPONSE_AND_ERROR )
52+ @ Metrics (namespace = "ServerlessAirline" , service = "payment" , captureColdStart = true )
5553 public APIGatewayProxyResponseEvent handleRequest (final APIGatewayProxyRequestEvent input , final Context context ) {
5654 Map <String , String > headers = new HashMap <>();
5755
@@ -66,13 +64,13 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv
6664 metric .setDimensions (DimensionSet .of ("AnotherService1" , "CustomService1" ));
6765 });
6866
69- LoggingUtils . appendKey ("test" , "willBeLogged" );
67+ MDC . put ("test" , "willBeLogged" );
7068
7169 APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent ()
7270 .withHeaders (headers );
7371 try {
7472 final String pageContents = this .getPageContents ("https://checkip.amazonaws.com" );
75- log .info (pageContents );
73+ log .info ("" , entry ( "ip" , pageContents ) );
7674 TracingUtils .putAnnotation ("Test" , "New" );
7775 String output = String .format ("{ \" message\" : \" hello world\" , \" location\" : \" %s\" }" , pageContents );
7876
0 commit comments