Reputation: 814
As documented in a previous StackOverflow question, I am learning how to use JAX-WS (Java API for XML Web Services). I've used Log4j2 in previous executable Java programs, and I'd like to use it for logging web service requests. How can I add Log4j2 to the base code in this JAX-WS tutorial?
Upvotes: 5
Views: 3823
Reputation: 814
Here's how I solved this question:
lib
folder.
lib
folder:
Source Packages
folder (see screenshot above). The contents of my log4j2.xml file are below to use as a starting point.
{$Tomcat}\logs
folder. I originally had placed the application's log file in the {$Tomcat}\webapps\HelloWorld
folder. However, I learned that Tomcat keeps file locks on the log files that will prevent "hot deploy/undeploy", leading to an incomplete deployment and a web application that doesn't work. For more info, search for antiResourceLocking on this page.<Configuration status='off'> <Properties> <Property name='logFilePath'>logs/HelloWorld.log</Property> </Properties> <Appenders> <Console name='Console' target='SYSTEM_OUT'> <PatternLayout pattern='%m%n'/> </Console> <File name='File' fileName='${logFilePath}'> <PatternLayout> <Pattern>%d %-5p %m%n</Pattern> </PatternLayout> </File> <SMTP name='Email' subject='JAX-WS Hello World Tutorial Web Service' to='[email protected]' from='[email protected]' smtpHost='intranet-smtp.plexus.com' smtpPort='25' bufferSize='10000'> <PatternLayout> <Header>One or more errors occurred! Please see the full log file at: ${logFilePath}.%n</Header> <Pattern>%d %-5p %m%n</Pattern> </PatternLayout> </SMTP> </Appenders> <Loggers> <Root level='debug'> <AppenderRef ref='File'/> <AppenderRef ref='Console'/> <AppenderRef ref='Email'/> </Root> </Loggers> </Configuration>
<context-param> <param-name>log4jConfiguration</param-name> <param-value>log4j2.xml</param-value> </context-param>
HelloWorldImpl.java
file.
log4j2.xml
configuration to make sure an email is sent when an Exception is logged.package com.mkyong.ws; import javax.jws.WebService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @WebService(endpointInterface = "com.mkyong.ws.HelloWorld") public class HelloWorldImpl implements HelloWorld { private static final Logger logger = LogManager.getLogger(HelloWorldImpl.class); @Override public String getHelloWorldAsString() throws Exception { logger.entry(); logger.info("Request received"); logger.exit(); final Exception exception = new Exception("What the heck happened?"); logger.error(exception); throw exception; } }
I hope this is helpful! - Pflugs
Upvotes: 5