I'm running a Vaadin Servlet with a very simple code and I want to log with Log4j when the application starts/ends, and the same for the sessions of the clients. However, only some of the messages that are logged end up being written on the log file itself.
The code of the servlet is the following:
#WebServlet(value = "/*", asyncSupported = true)
#VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class, widgetset = "som.vaadin.AppWidgetSet")
public static class Servlet extends VaadinServlet implements SessionInitListener, SessionDestroyListener {
private static final Logger LOGGER = LogManager.getLogger(Servlet.class);
#Override
protected void servletInitialized() throws ServletException {
super.servletInitialized();
LOGGER.info("App started");
getService().addSessionInitListener(this);
getService().addSessionDestroyListener(this);
}
#Override
public void destroy() {
LOGGER.info("App stopped");
super.destroy();
}
#Override
public void sessionInit(SessionInitEvent event) {
LOGGER.info("Session started");
}
#Override
public void sessionDestroy(SessionDestroyEvent event) {
LOGGER.info("Session expired");
}
}
My log4j log configuration file is the following:
log4j.rootLogger=DEBUG, console, RollingAppender
log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingAppender.File=c:/temp/log/error_som.log
log4j.appender.RollingAppender.DatePattern='.'yyyy-ww
log4j.appender.RollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingAppender.layout.ConversionPattern= [%d{ISO8601}] %5p%6.6r[%t]%x(%F:%L) - %m%n
And finally, what I get in the log file is just:
[2015-06-09 14:17:18,197] INFO 6042 (MainBusiness.java:122) - App started
I have checked by debugging that sessionInit() is called, and the LOGGER.info() call inside it is also performed. However, the log4j Appender only received the message corresponding to the servletInitialized() function.
Any idea on what can be happening?
Thanks,
Cris
Finally, I got what was going on. Though, I cannot understand it.
The issue was caused by conflicting servlet configurations and init-params from the annotations shown in the above code and the contents of the web.xml.
The VaadinServlet was define twice, with different configurations including the configuration file for the log4j library. And sometimes, the servlet was using one and sometimes it was using the other one, even inside the same object!
So, I got it solved, but I cannot see why it was really happening.
Related
I have an application with management.server enabled:
management.server.port=8081
When I start application, I have:
10 threads for 8080 HTTP nio connector
10 threads for 8081 HTTP nio connector
But I would like to reduce min-spare only for management (8081) and not for the web application (8080)
Looking at Spring code, it seems it's not possible, can someone confirm ?
EDIT: The approach below is not sufficient as the ManagementWebServerFactoryCustomizer is also a ConfigurableWebServerFactory and will thus be applied to the main server.
Adding logic to check againgst the management port is not helping as the management context has its very own wiring and won't pick up the bean.
Looks like it's not possible to hook into the management server configuration easily (would be easier if ServletManagementContextFactory were public).
You can look into ServletManagementChildContextConfiguration to see how the management server is wired.
You could hook into the management server configuration by providing a ManagementWebServerFactoryCustomizer like this (not sure if there's an easier way):
#Configuration
public class TomcatManagementCustomizerConfiguration {
#Bean
ManagementWebServerFactoryCustomizer<ConfigurableServletWebServerFactory> servletManagementWebServerFactoryCustomizer(
#Value("${management.server.threads.min-spare:5}") int managementMinSpareThreads,
ListableBeanFactory beanFactory) {
return new TomcatManagementCustomizer(beanFactory, managementMinSpareThreads);
}
static class TomcatManagementCustomizer extends ManagementWebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
private final int managementMinSpareThreads;
protected TomcatManagementCustomizer(ListableBeanFactory beanFactory, int managementMinSpareThreads) {
super(beanFactory, TomcatWebServerFactoryCustomizer.class);
this.managementMinSpareThreads = managementMinSpareThreads;
}
#Override
#SuppressWarnings("rawtypes")
protected void customize(ConfigurableServletWebServerFactory factory, ManagementServerProperties managementServerProperties, ServerProperties serverProperties) {
super.customize(factory, managementServerProperties, serverProperties);
((TomcatServletWebServerFactory) factory).addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractProtocol) {
AbstractProtocol protocol = (AbstractProtocol) handler;
protocol.setMinSpareThreads(managementMinSpareThreads);
}
});
}
}
}
Can you not just put the following in either properties file or YAML file?
Or is there something I misunderstood?
server.tomcat.threads.min-spare=2
(This is for properties file)
Just to verify (You don't need this as you have been checking the updated value in the log)
Put the following in either properties file or YAML file
management.endpoints.web.exposure.include=health,info,metrics,env
(This is for properties file)
And visit /actuator/env/server.tomcat.threads.min-spare
You need actuator dependency for the link above to work.
You can use #ManagementConfigurationContext and add the configuration class to to your META-INF/spring.properties file.
It is also important to place the configuration class in a package which is not the main package or sub-package of your main application context. This is so that this configuration only applies to the management context.
Below is the sampel configuration following #Holgzn's response.
#ManagementContextConfiguration
public class TomcatManagementCustomizerConfiguration {
#Bean
ManagementWebServerFactoryCustomizer<ConfigurableServletWebServerFactory> servletManagementWebServerFactoryCustomizer(
#Value("${management.server.threads.min-spare:5}") int managementMinSpareThreads,
ListableBeanFactory beanFactory) {
return new TomcatManagementCustomizer(beanFactory, managementMinSpareThreads);
}
static class TomcatManagementCustomizer extends ManagementWebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
private final int managementMinSpareThreads;
protected TomcatManagementCustomizer(ListableBeanFactory beanFactory, int managementMinSpareThreads) {
super(beanFactory, TomcatWebServerFactoryCustomizer.class);
this.managementMinSpareThreads = managementMinSpareThreads;
}
#Override
#SuppressWarnings("rawtypes")
protected void customize(ConfigurableServletWebServerFactory factory, ManagementServerProperties managementServerProperties, ServerProperties serverProperties) {
super.customize(factory, managementServerProperties, serverProperties);
((TomcatServletWebServerFactory) factory).addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractProtocol) {
AbstractProtocol protocol = (AbstractProtocol) handler;
protocol.setMinSpareThreads(managementMinSpareThreads);
}
});
}
}
}
The spring.properties file
org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=<package>.TomcatManagementCustomizerConfiguration
I am trying to add a ErrorHandler via the EventProcessingConfigurer.registerErrorHandler() method and while it is showing on the configuration the class itself is not being called.
Am currently using Axon 4.1.1 (With out Axon server) and Spring Boot 2.1.6.RELEASE.
i have based my code off github/AxonFramework but it isn't acting the same.
Config:
#Autowired
public void configure(final EventProcessingConfigurer config) {
TestErrorHandler testErrorHandler = new TestErrorHandler();
config.registerErrorHandler("SolrProjection", configuration -> testErrorHandler);
}
ErrorHander:
public class TestErrorHandler implements ErrorHandler, ListenerInvocationErrorHandler {
#Override
public void handleError(final ErrorContext errorContext) throws Exception {
System.out.println("TestErrorHandler.handleError()");
}
#Override
public void onError(final Exception exception, final EventMessage<?> event, final EventMessageHandler eventHandler) {
System.out.println("TestErrorHandler.onError()");
}
}
Projection:
#Configuration
#RequiredArgsConstructor
#ProcessingGroup("SolrProjection")
public class SolrProjection {
#EventHandler
public void onEvent(final TestEvent event,
#SequenceNumber Long sequenceNumber,
#Timestamp final Instant requestTimestamp,
#MessageIdentifier final String messageIdentifier,
final MetaData metaData) {
if (true) {
throw new IllegalStateException();
}
}
even thou i am directly throwing an error, i do not ever see the two system.out's in console. and putting log statements in the #EventHandler are properly being called.
The ErrorHandler is tasked to dealing with different exceptions than what you expect.
When it comes to handling events, Axon Framework deduces two layers:
The internal EventProcessor layer
The Event Handling Components written by framework users
Exceptions thrown within the EventProcessor are dealt with by the ErrorHandler you've configured.
For customizing the process for handling exceptions from your own Event Handlers, you
will have to configure the ListenerInvocationErrorHandler.
To configure a general/default ListenerInvocationErrorHandler, you can use the following method in your first snippet:
EventProcessingConfigurer#registerDefaultListenerInvocationErrorHandler(
Function<Configuration, ListenerInvocationErrorHandler>
)
You can also check out Axon's Reference Guide at this page for more info on this.
Hope this helps you out #sherring!
The HttpClient introduced experimentally in Java 9 is now stable in Java 11, but not surprisingly, very few projects seem to actually use it. Documentation is almost non-existing.
One of the most commons asks while making a HTTP call is logging of request/response. How would you do that using the HttpClient, without of course, logging it manually in every single call? Is there an interceptor mechanism like that offered by all other HTTP clients?
You can log request and responses by specifying-Djdk.httpclient.HttpClient.log=requests on the Java command line.
As for testing/mocking you might want to have a look at the offline test:
http://hg.openjdk.java.net/jdk/jdk/file/tip/test/jdk/java/net/httpclient/offline/
Depending on what you are looking to achieve you could use a "DelegatingHttpClient" to intercept and log requests and responses too.
Besides the Java API documentation there's also some high level documentation at http://openjdk.java.net/groups/net/httpclient/index.html
Additional note:
The jdk.httpclient.HttpClient.log property is an implementation specific property whose value is a comma separated list which can be configured on the Java command line for diagnosis/debugging purposes with the following values:
-Djdk.httpclient.HttpClient.log=
errors,requests,headers,
frames[:control:data:window:all],content,ssl,trace,channel,all
If we look at jdk.internal.net.http.common.DebugLogger source code we can see a few loggers using System.Logger, which in turn will useSystem.LoggerFinder to select the logger framework. JUL is the default choice. The logger names are:
jdk.internal.httpclient.debug
jdk.internal.httpclient.websocket.debug
jdk.internal.httpclient.hpack.debug
They can be enabled by setting them as a system property. For example running with -Djdk.internal.httpclient.debug=true will produce:
DEBUG: [main] [147ms] HttpClientImpl(1) proxySelector is sun.net.spi.DefaultProxySelector#6dde5c8c (user-supplied=false)
DEBUG: [main] [183ms] HttpClientImpl(1) ClientImpl (async) send https://http2.github.io/ GET
DEBUG: [main] [189ms] Exchange establishing exchange for https://http2.github.io/ GET,
proxy=null
DEBUG: [main] [227ms] PlainHttpConnection(?) Initial receive buffer size is: 43690
DEBUG: [main] [237ms] PlainHttpConnection(SocketTube(1)) registering connect event
DEBUG: [HttpClient-1-SelectorManager] [239ms] SelectorAttachment Registering jdk.internal.net.http.PlainHttpConnection$ConnectEvent#354bf356 for 8 (true)
...
On our side, we did not find the logging provided by -Djdk.internal.httpclient.debug readable enough. The solution we came up with is to wrap the HttpClient with a decorator that will be able to intercept the calls and provide logging. Here how it somehow looks (should be done not only for send but sendAsync methods) :
public class HttpClientLoggingDecorator extends HttpClient {
private static final Logger logger = Logger.getLogger(HttpClientLoggingDecorator.class.getName());
private final HttpClient client;
...
#Override
public <T> HttpResponse<T> send(HttpRequest req, HttpResponse.BodyHandler<T> responseBodyHandler)
throws IOException,
InterruptedException
{
subscribeLoggerToRequest(req);
HttpResponse<T> response = client.send(req, responseBodyHandler);
logResponse(response);
return response;
}
private void subscribeLoggerToRequest(HttpRequest req) {
// define a consumer for how you want to log
// Consumer<String> bodyConsumer = ...;
if (req.bodyPublisher().isPresent()) {
req.bodyPublisher().get().subscribe(new HttpBodySubscriber(bodyConsumer)));
} else {
bodyConsumer.accept(NO_REQUEST_BODY);
}
}
private <T> void logResponse(HttpResponse<T> response) {
// String responseLog = ...;
logger.info(responseLog);
}
}
And here is the HttpBodySubscriber:
public class HttpBodySubscriber implements Flow.Subscriber<ByteBuffer> {
private static final long UNBOUNDED = Long.MAX_VALUE;
private final Consumer<String> logger;
public HttpBodySubscriber(Consumer<String> logger) {
this.logger = logger;
}
#Override
public void onSubscribe(Flow.Subscription subscription) {
subscription.request(UNBOUNDED);
}
#Override
public void onNext(ByteBuffer item) {
logger.accept(new String(item.array(), StandardCharsets.UTF_8));
}
#Override
public void onError(Throwable throwable) {
}
#Override
public void onComplete() {
}
}
I'm consuming a soap webservice inside a spring boot application. The response/request logging is too big, because of one attribute which is too large. So I want to intercept that logging and remove the offending attribute.
I've been messing about with SoapEnvelopeLoggingInterceptor, but i think that is just for Server side logging. It will not get picked up.
I have configured my soap logging inside yml as follows:
logging:
pattern:
...
level:
...
org.springframework.ws.client.MessageTracing.sent: TRACE
org.springframework.ws.client.MessageTracing.received: TRACE
org.springframework.ws.server.MessageTracing: DEBUG
That works fine for logging both request and response, but I need to remove a very large problematic attribute from the envelope. Any ideas?
You can extend ClientInterceptorAdapter the abstract implementation ClientInterceptors and oveeride handleRequest and handleResponse to parse, modify and log your custom message.
The below code delegates to AbstractLoggingInterceptor handleRequest and handleResponse and overrides the logMessage to create a custom message.
Something like
public class MyInterceptor extend ClientInterceptorAdapter {
private final Logger logger = LoggerFactory.getLogger(MyInterceptor.class);
private EndpointInterceptor endpointInterceptor = new AbstractLoggingInterceptor() {
#Override
protected Source getSource(WebServiceMessage webServiceMessage) {
// Base logic same as SoapEnvelopeLoggingInterceptor getSource method.You can adjust to your preference.
if(webServiceMessage instanceof SoapMessage) {
SoapMessage soapMessage = (SoapMessage)webServiceMessage;
return soapMessage.getEnvelope().getSource();
} else {
return null;
}
}
#Override
protected void logMessage(String message) {
// You can use your regex to remove the attribute and log the message.
this.logger.debug(message);
}
};
#Override
public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
return endpointInterceptor.handleRequest(messageContext, null);
}
#Override
public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException {
return endpointInterceptor.handleResponse(messageContext, null);
}
}
There is no simple way of doing that. You ether implement your own Logger by extending sl4j API or wrap log calls and do the transformations there.
The first way will require some efforts because you'll need to implement batch of classes and make sure that the other parts of log system are not broken.
But second part is pretty strait forward.
You have ti create a logger wrapper which implements Logger interface, could be something like this:
public class LoggerWrapper extends MarkerIgnoringBase {
private final Logger logger;
public LoggerWrapper(Logger logger) {
this.logger = logger;
}
private String transformMessage(String input) {
// do all needed regexp transformations here
}
#Override
public void debug(String msg) {
String transformedMessage = transformMessage(msg);
// delegate log call to inner logger
logger.debug(transformedMessage);
}
// implement the rest of the methods here
}
And in the code you might use it like this:
private static final Logger log = new LoggerWrapper(
LoggerFactory.getLogger(SomeClass.class)
);
You can also wrap the logger without implementing Logger interface (in my case it is MarkerIgnoringBase class). In that case you'll not need to implement number of methods from the interface, however you'll louse interchangeability.
The drawback of this solution is that you have to log the messages in advance on your side (not via MessageTracing) but if it is possible I would go this way. On the other hand the first solution does it out of the box.
could anybody explain to me, how to set up java Logger for various classes from a concrete package ?
for example:
if I get this one and set it up
Logger logger = Logger.getLogger("com.google.api.client.*");
logger.setLevel(Level.CONFIG);
logger.addHandler(new Handler() {
#Override
public void close() throws SecurityException {
}
#Override
public void flush() {
}
#Override
public void publish(LogRecord record) {
// default ConsoleHandler will take care of >= INFO
if (record.getLevel().intValue() < Level.INFO.intValue()) {
System.out.println(record.getMessage());
}
}
});
there are conditions like this
Logger.getLogger(HttpTransport.class.getName()).isLoggable(Level.CONFIG);
in the library where HttpTransport is part of com.google.api.client.*
But the problem is, that
Logger.getLogger(HttpTransport.class.getName()).isLoggable(Level.CONFIG);
is false ... like if a different logger was obtained
How else should I set it for all classes from the same package? if there are conditions for loggers for concrete classes like HttpTransport.
You do not want the .* in your package string.
Change
Logger logger = Logger.getLogger("com.google.api.client.*");
to
Logger logger = Logger.getLogger("com.google.api.client");