Announcement Announcement Module
Collapse
No announcement yet.
Adding logging to spring we application Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding logging to spring we application

    Hi all.
    I have a spring-ws application. It is a simpel web service, that is placed between legacy systeem and external clients. It uses a SOAP as an input and generates the SOAP message to be sent to external systems. Now I want to add logging to this project. I have tried to add log4j to my pom.xml in this way:
    Code:
    <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
          </dependency>
          <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
          </dependency>
    After that maven install gives an error:
    Code:
    [ERROR] 
    
    Mojo: 
    
        org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile
    
    FAILED for project: 
    
        com.hti:Labor:war:0.0.1-SNAPSHOT
    
    Reason:
    
    C:\LaboratoryNew\labor\src\main\java\com\hti\service\OrderServiceImpl.java:[11,2] annotations are not supported in -source 1.3
    (use -source 5 or higher to enable annotations)
    	@Autowired
    All the class fields marked with @Autowired are listed in this error message.
    Also the stack trace is:
    Code:
    Error stacktrace:
    org.apache.maven.ProjectBuildFailureException: Build for project: com.hti:Labor:war:0.0.1-SNAPSHOT failed during execution of mojo: org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile
    	at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegmentForProject(DefaultLifecycleExecutor.java:293)
    	at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:191)
    	at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:149)
    	at org.apache.maven.DefaultMaven.execute_aroundBody0(DefaultMaven.java:223)
    	at org.apache.maven.DefaultMaven.execute_aroundBody1$advice(DefaultMaven.java:304)
    	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:1)
    	at org.apache.maven.embedder.MavenEmbedder.execute_aroundBody2(MavenEmbedder.java:904)
    	at org.apache.maven.embedder.MavenEmbedder.execute_aroundBody3$advice(MavenEmbedder.java:304)
    	at org.apache.maven.embedder.MavenEmbedder.execute(MavenEmbedder.java:1)
    	at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:176)
    	at org.apache.maven.cli.MavenCli.main(MavenCli.java:63)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    	at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    	at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:408)
    	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:351)
    	at org.codehaus.classworlds.Launcher.main(Launcher.java:31)
    Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure
    	at org.apache.maven.plugin.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:516)
    	at org.apache.maven.plugin.CompilerMojo.execute(CompilerMojo.java:114)
    	at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:579)
    	at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:498)
    	at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegmentForProject(DefaultLifecycleExecutor.java:265)
    	... 19 more
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILED
    I use STS version 2.1.0.SR01. The prtoject is compiled with java version 1.5. If I remove log4j dependecies from pom.xml all is comiled successfully. May be you can advise me, how to correctly add logging ability to my project and is it a good idea to use log4j libraries?

  • #2
    Finally I have found a solution. maven dependency should be set like
    Code:
    <dependency>
    			<groupId>log4j</groupId>
    			<artifactId>log4j</artifactId>
    			<version>1.2.14</version>
    		</dependency>
    This works.
    But now I have a problem with writing logs to file. I have declared appender in a log4j.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
     
    	<appender name="roll" class="org.apache.log4j.DailyRollingFileAppender">
    		<param name="File" value="logs/application.log" />
    		<param name="DatePattern" value=".yyyy-MM-dd" />
    		<layout class="org.apache.log4j.PatternLayout">
    			<param name="ConversionPattern"
    				value="%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n  %-5p %m%n" />
    		</layout>
    	</appender>
    	
    	<root>
    		<priority value="roll" />
    		<appender-ref ref="roll" />
    	</root>
    	 
    </log4j:configuration>
    Also the web.xml looks like
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	id="WebApp_ID" version="2.5">
    
    	<context-param>
    		<param-name>log4jConfigLocation</param-name>
    		<param-value>/WEB-INF/log4j.xml</param-value>
    	</context-param>
    	
    	<listener>
    		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    	</listener>
    
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>WEB-INF/context.xml</param-value>
    	</context-param>
    
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	<listener>
    		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    	</listener>
    
    	<display-name>Labor</display-name>
    	<servlet>
    		<display-name>LaborServlet</display-name>
    		<servlet-name>LaborServlet</servlet-name>
    		<servlet-class>com.hti.web.LaborServlet</servlet-class>
    	</servlet>
    
    	<servlet-mapping>
    		<servlet-name>LaborServlet</servlet-name>
    		<url-pattern>/hti</url-pattern>
    	</servlet-mapping>
    </web-app>
    When I change appender type to console it works. How can I manage the goal of writing logs to file and have file names with sysdate date included?

    Comment


    • #3
      This is not a spring issue but a lack of knowledge in log4j in order to know how to write the logs in file instead of the console or am i wrong about your need ?

      Comment


      • #4
        You are actually right. It was the first tme I have configured such type of logging so I have made some mistakes. Now all is ok and configuration file looks like
        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
        
        <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
         
        	<appender name="roll" class="org.apache.log4j.DailyRollingFileAppender">
        		<param name="File" value="labor_logs/application.log" />
        		<param name="DatePattern" value=".yyyy-MM-dd" />
        		<layout class="org.apache.log4j.PatternLayout">
        			<param name="ConversionPattern"
        				value="%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n  %-5p %m%n" />
        		</layout>
        	</appender>
        	
        	<root>
        		<priority value="debug" />
        		<appender-ref ref="roll" />
        	</root>
        	 
        </log4j:configuration>

        Comment

        Working...
        X