I've read dozen of azure docs and I still don't understand how to log in my java azure function

Ilminsky, Mykola-ext 6 Reputation points
2022-12-08T12:56:57.57+00:00

I have a simple azure function written in java.
I need to log some information (I use slfj & logback) in my function. And I want to see those logs in application insights. As simple as that.
I'm reading azure documentation for a few days for now, and the more I read - the less I understand how to do that.
Here https://learn.microsoft.com/en-us/azure/azure-monitor/app/java-in-process-agent it's suggested to manually download applicationinsights-agent-3.4.6.jar and to put into my classpath as a java-agent. But managing classpath is out of my control when I use azure functions. Where I should put that jar?
Here https://learn.microsoft.com/en-us/azure/azure-monitor/app/monitor-functions it's suggested to turn on (by adding few app setting entries) some "distributed tracing". But I don't need "distributed tracing" it seems to be an overkill to me. I just want to see my logs in appinsights, nothing more.

There are also some answers on stackoverflow which suggest to use applicationinsights-core java dependency in my app but I still can't understand if those suggestions are still relevant in 2022 (that dependencies seem to be updated a year ago last time)

So, assuming there is
LoggerFactory.getLogger().info("my log")
somewhere inside my azure function, is it possible at all to see that "my log" somewere in appinsight?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,929 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MughundhanRaveendran-MSFT 12,506 Reputation points
    2022-12-14T06:55:34.537+00:00

    Hi @Ilminsky, Mykola-ext ,

    Thanks for reaching out to Q&A forum.

    I am sorry that you had to go through a lot of documents for logging. A simple approach is to use the inbuilt getlogger to log warning/critical/informational messages in functions so that it can be viewed in the Application insights

    public class Function {
    public String echo(@HttpTrigger(name = "req", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) String req, ExecutionContext context) {
    if (req.isEmpty()) {
    context.getLogger().warning("Empty request body received by function " + context.getFunctionName() + " with invocation " + context.getInvocationId());
    }
    return String.format(req);
    }
    }

    Reference : https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-java?tabs=bash%2Cconsumption#logger

    You can configure the log levels in the host.json file as mentioned in the below article,

    https://learn.microsoft.com/en-us/azure/azure-functions/functions-monitoring#log-levels-and-categories

    You can view the logs in the monitor section of the Function app which has the invocation logs or you can also view the additional and granular logging in the log analytics workspace as well. In the log analytics workspace, you can run queries on the Traces table

    270442-image.png

    You can use the Azure CLI to stream Java stdout and stderr logging, as well as other application logging : https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-java?tabs=bash%2Cconsumption#view-logs-and-trace

    Hope this helps! Feel free to reach out to me if you have any queries or concerns.


  2. Kaibo Cai 76 Reputation points Microsoft Employee
    2022-12-19T13:56:42.877+00:00

    Hi @Ilminsky, Mykola-ext

    You can config whatever log framework you would like to use in function app (logback, log4j2 whatever), you can use those log frameworks to log in your function app. The only thing you need is add appsetting "APPLICATIONINSIGHTS_ENABLE_AGENT=true", this will enable you to see those logs in your appinsights. Thanks.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.