skip to Main Content

I have a java Spring Boot application deployed to an Azure App Service environment. I am using log4J2 to handle my logging for the application and writing the logs to the same location as the other log files created for the app service. I am able to find the log file (spring.log) my spring boot application is creating and logging to in the KUDU editor, but I cannot figure out where to go from there. My goal is to make the lines that the java application is writing to the log file available from Azure portal so I can, for example, setup alerts when my application writes "Some specific exception". However, I cannot find a way to see anything from this log file from the "logs" option of the app service from azure portal. I can see some log entries saying that this log file was created/updated/deleted, but the actual contents of the log file are what I am after. Does anyone know how to do this or if it is even possible? For additional context my application is deployed as a jsp app on a tomcat server deployed to the app service as a war file along with the following web.config file

<?xml version="1.0" encoding="UTF-8"?>      `
<configuration>
      <system.web>
          <customErrors mode="Off" />
      </system.web>
      <system.webServer>
         <httpErrors errorMode="Detailed"/>
         <handlers>
             <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
         </handlers>
         <httpPlatform processPath="%JAVA_HOME%binjava.exe" arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar &quot;%HOME%sitewwwrootmy-application=name.war&quot;">
         </httpPlatform>
     </system.webServer>
 </configuration>

I have yet to even be able to find the console output from the deployed version, but of course I can see the console output when I run it from my IDE locally.

I’ve turned on ‘diagnostic settings’ for the application and am sending all available options to a log analytics workspace, but querying this workspace still does not give me any results related to the actual contents contained within the file.

2

Answers


  1. I created a sample Spring Boot application to generate logs in spring.log locally. I successfully found the logs in the Azure Web App KUDU under File Manager > LogFiles > Application > spring.xxxxx.log.

    LoggingController.java :

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/api")
    public class LoggingController {
        private static final Logger logger = LoggerFactory.getLogger(LoggingController.class);
    
        @GetMapping("/log")
        public String logSomething() {
            logger.info("Info log message");
            logger.warn("Warning log message");
            logger.error("Error log message");
            return "Logs have been generated!";
        }
    }
    

    application.properties :

    logging.file.path=<folderpath>/LogFiles
    logging.file.name=spring.log
    logging.level.root=INFO
    

    log4j2.xml :

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

    web.config :

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <handlers>
                <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
            </handlers>
            <httpPlatform processPath="%JAVA_HOME%binjava.exe"
                          arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar &quot;%HOME%sitewwwrootmy-spring-app.war&quot;">
            </httpPlatform>
        </system.webServer>
    </configuration>
    

    pom.xml :

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
        <version>3.4.1</version>
    </dependency>
    

    I got the below output in the browser.

    enter image description here

    I got the below logs in the spring.log file.

    enter image description here

    Azure Web Apps :

    enter image description here

    We can view the spring.log data in the Azure Web App using KUDU by navigating to File Manager > LogFiles > Application > spring.xxxxx.log.

    https://kamsbappxxxxxx.scm.canadacentral-01.azurewebsites.net/newui/fileManager
    

    enter image description here

    Logs :

    https://kamsbappxxxxxxx.scm.canadacentral-01.azurewebsites.net/api/vfs/LogFiles/Application/spring.ln0xxxxxx.log
    

    enter image description here

    Login or Signup to reply.
  2. You can integrate Azure Application Insights to your Spring Boot application: https://learn.microsoft.com/en-us/azure/azure-monitor/app/java-spring-boot?source=recommendations

    All you need is a dependency, a line in main method of your application to initiate the agent and the configuration properties that point to your Application Insights.

    With that done, the Application Insights agent will auto-collect logs from Log4j2 and send them to Azure Application Insights. From there you can set up alerts and have better control on your logs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search