Skip to content

Support Logging in Spring Cloud Function deployed as Azure function #477

@chaitanya-mittapalli

Description

@chaitanya-mittapalli

*Unable the Get the com.microsoft.azure.functions.ExecutionContext Bean in the Spring Cloud Function that can be used for logging

I am trying to get the ExecutionContext Bean in the Spring Cloud Function to get the logger to log some Debug Information .

  @Bean
    public Function<String, String> hello(ExecutionContext targetContext) {
        return user -> {
            targetContext.getLogger().info("Invoking HelloFunction with " + user);
            return "Welcome, " + user;
        };
    }

Following is how my pom looks like for the versions.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.spring</groupId>
    <artifactId>batch_loadplanning_360_migration_factoringCompany</artifactId>
    <packaging>jar</packaging>
    <version>1.2-SNAPSHOT</version>
    <name>Factoring Company Migration</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <azure.functions.maven.plugin.version>1.3.2</azure.functions.maven.plugin.version>
        <azure.functions.java.library.version>1.3.0</azure.functions.java.library.version>
        <functionAppName>my-spring-function</functionAppName>
        <functionAppRegion>westus</functionAppRegion>
        <stagingDirectory>${project.build.directory}/azure-functions/${functionAppName}</stagingDirectory>
        <functionResourceGroup>my-resource-group</functionResourceGroup>
        <start-class>com.spring.factoringcompanymigration.Application</start-class>
        <wrapper.version>1.0.22.RELEASE</wrapper.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-adapter-azure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-function-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-compiler</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.microsoft.azure</groupId>
                    <artifactId>azure-functions-maven-plugin</artifactId>
                    <version>${azure.functions.maven.plugin.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>3.1.1</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-functions-maven-plugin</artifactId>
                <configuration>
                    <resourceGroup>${functionResourceGroup}</resourceGroup>
                    <appName>${functionAppName}</appName>
                    <region>${functionAppRegion}</region>
                    <appSettings>
                        <!-- Run Azure Function from package file by default -->
                        <property>
                            <name>WEBSITE_RUN_FROM_PACKAGE</name>
                            <value>1</value>
                        </property>
                        <property>
                            <name>FUNCTIONS_EXTENSION_VERSION</name>
                            <value>~2</value>
                        </property>
                        <property>
                            <name>FUNCTIONS_WORKER_RUNTIME</name>
                            <value>java</value>
                        </property>
                        <property>
                            <name>MAIN_CLASS</name>
                            <value>com.spring.factoringcompanymigration.Application</value>
                        </property>
                    </appSettings>
                </configuration>
                <executions>
                    <execution>
                        <id>package-functions</id>
                        <goals>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <overwrite>true</overwrite>
                            <outputDirectory>
                                ${project.build.directory}/azure-functions/${functionAppName}
                            </outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.basedir}</directory>
                                    <includes>
                                        <include>host.json</include>
                                        <include>local.settings.json</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${stagingDirectory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                            <includeScope>runtime</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--Remove obj folder generated by .NET SDK in maven clean-->
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>obj</directory>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot.experimental</groupId>
                        <artifactId>spring-boot-thin-layout</artifactId>
                        <version>${wrapper.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-snapshot-local</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/release</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-snapshot-local</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

Getting the following Error when trying to run the mvn azure-functions:run and trying to hit the http://localhost:7071/api/hello with POST Request.

4/1/2020 3:17:02 PM]   .   ____          _            __ _ _
[4/1/2020 3:17:02 PM]  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
[4/1/2020 3:17:02 PM] ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
[4/1/2020 3:17:02 PM]  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
[4/1/2020 3:17:02 PM]   '  |____| .__|_| |_|_| |_\__, | / / / /
[4/1/2020 3:17:02 PM]  =========|_|==============|___/=/_/_/_/
[4/1/2020 3:17:02 PM]  :: Spring Boot ::        (v2.1.6.RELEASE)
[4/1/2020 3:17:02 PM] 2020-04-01 10:17:02.269  INFO 25656 --- [pool-2-thread-2] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : *************************************************
[4/1/2020 3:17:17 PM] 2020-04-01 10:17:17.357  INFO 25656 --- [pool-2-thread-2] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=function_loadplanning_360_migration_factoringcompany, profiles=[TEST], label=null, version=ffa8f7f4f5245b991d9a900d973c8049e00c5666, state=null
[4/1/2020 3:17:17 PM] 2020-04-01 10:17:17.357  INFO 25656 --- [pool-2-thread-2] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='file:///C:\sw\wwwapp/config-repo/function_loadplanning_360_migration_factoringcompany-TEST.yml'}, MapPropertySource {name='file:///C:\sw\wwwapp/config-repo/application.yml (document #0)'}]}
[4/1/2020 3:17:22 PM] Executed 'Functions.hello' (Failed, Id=50d69bfc-d5e3-42f9-a9d9-a9d20fa0fcc5)
[4/1/2020 3:17:22 PM] System.Private.CoreLib: Exception while executing function: Functions.hello. System.Private.CoreLib: Result: Failure
Exception: NoSuchBeanDefinitionException: No qualifying bean of type 'com.microsoft.azure.functions.ExecutionContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Stack: java.lang.reflect.InvocationTargetException
[4/1/2020 3:17:22 PM] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[4/1/2020 3:17:22 PM] 	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[4/1/2020 3:17:22 PM] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[4/1/2020 3:17:22 PM] 	at java.lang.reflect.Method.invoke(Unknown Source)
[4/1/2020 3:17:22 PM] 	at com.microsoft.azure.functions.worker.broker.JavaMethodInvokeInfo.invoke(JavaMethodInvokeInfo.java:22)
[4/1/2020 3:17:22 PM] 	at com.microsoft.azure.functions.worker.broker.JavaMethodExecutor.execute(JavaMethodExecutor.java:54)
[4/1/2020 3:17:22 PM] 	at com.microsoft.azure.functions.worker.broker.JavaFunctionBroker.invokeMethod(JavaFunctionBroker.java:53)
[4/1/2020 3:17:22 PM] 	at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:33)
[4/1/2020 3:17:22 PM] 	at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:10)
[4/1/2020 3:17:22 PM] 	at com.microsoft.azure.functions.worker.handler.MessageHandler.handle(MessageHandler.java:45)
[4/1/2020 3:17:22 PM] 	at com.microsoft.azure.functions.worker.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:92)
[4/1/2020 3:17:22 PM] 	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
[4/1/2020 3:17:22 PM] 	at java.util.concurrent.FutureTask.run(Unknown Source)
[4/1/2020 3:17:22 PM] 	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
[4/1/2020 3:17:22 PM] 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
[4/1/2020 3:17:22 PM] 	at java.lang.Thread.run(Unknown Source)
[4/1/2020 3:17:22 PM] Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hello' defined in com.jbhunt.factoringcompanymigration.Application: Unsatisfied dependency expressed through method 'hello' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.microsoft.azure.functions.ExecutionContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769)
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:509)
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321)
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160)
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845)
[4/1/2020 3:17:22 PM] 	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
[4/1/2020 3:17:22 PM] 	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
[4/1/2020 3:17:22 PM] 	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
[4/1/2020 3:17:22 PM] 	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
[4/1/2020 3:17:22 PM] 	at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
[4/1/2020 3:17:22 PM] 	at org.springframework.cloud.function.adapter.azure.AzureSpringFunctionInitializer.initialize(AzureSpringFunctionInitializer.java:149)
[4/1/2020 3:17:22 PM] 	at org.springframework.cloud.function.adapter.azure.AzureSpringBootRequestHandler.handleRequest(AzureSpringBootRequestHandler.java:52)
[4/1/2020 3:17:22 PM] 	at com.jbhunt.factoringcompanymigration.FunctionHandler.helloFunction(FunctionHandler.java:21)
[4/1/2020 3:17:22 PM] 	... 16 more
[4/1/2020 3:17:22 PM] Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.microsoft.azure.functions.ExecutionContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1658)
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1217)
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171)
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
[4/1/2020 3:17:22 PM] 	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
[4/1/2020 3:17:22 PM] 	... 34 more
[4/1/2020 3:17:22 PM] .

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions