Micronaut AWS Lambda logs are not showing in cloudfront

Viewed 382

I setup a micronaut java aws lambda project like explained here: https://guides.micronaut.io/micronaut-function-aws-lambda/guide/index.html

In the main function of the Function class I have an example:

System.out.println("LOG TEST LOG LOG");

Running locally as described in the documentation via

echo '{"param1": "23"}' | java -jar build/lib/myjar-full.jar

Shows correctly the console output.

Running the same function online in AWS lambda, does not log that message in cloudwatch logs.

The same problem happens also running locally via SAM CLI as explained in Micronaut docs.

Also using slf4j logger, still I am not getting the output. I am providing below all the relevant code of my micronaut lambda function

package lambda.test;

import io.micronaut.function.executor.FunctionInitializer;
import io.micronaut.function.FunctionBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.function.Function;

@FunctionBean("example-lambda")
public class LambdaFunction extends FunctionInitializer implements Function<Request, Response> {
    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass().getSimpleName());


    /**
     * This main method allows running the function as a CLI application using: echo '{}' | java -jar function.jar
     * where the argument to echo is the JSON to be parsed.
     */
    public static void main(String... args) throws IOException {
        System.out.println("LOG TEST LOG LOG");
        logger.info(String.format("Received the following payload: %s", String.join(";", args)));
        LambdaFunction function = new LambdaFunction();
        function.run(args, (context) -> function.apply(context.get(Request.class)));
    }

I also created log4j2.xml config file in the resources, with the following content

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

The default logback.xml config that comes with micronaut has these content:

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <withJansi>true</withJansi>
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="error">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

Both ways of trying to log are not working online or with SAM CLI. I am still researching on how to do this, but so far I cannot find any information on their documentation or elsewhere.

0 Answers
Related