Murphy's Law: when you think there may be a bug in a place, there will be a bug in this place-learn about localhost. log which is rarely noticed by Tomcat.

Source: Internet
Author: User
I. Problem Overview

The question is a little long, but it should be worth the back-end Java to understand the localhost. Log, let me make a long short talk.

The blogger is engaged in Java backend. The background is a simple spring MVC + Spring framework.

Today, I tested an interface on the Controller layer, but the breakpoint never came in.

I didn't make much changes. I added the spring-hosted bean mqmsgfactoryutils (the Code took over others, so I found the original part a little bloated and extracted the mqmsgfactoryutils public class.

, Mqmsgfactoryutils needs to depend on bean, and it will also make itself bean ).

As follows:

 

 

As an old driver who has been working for a few years, the idea is still clear. When I break a breakpoint in the filter, I find that I can come in.

Then break the point at the AOP layer. The AOP layer has a @ before method that switches the Controller layer method, mainly for some additional parameter verification.

At the breakpoint of the AOP layer, the code goes to the following position and returns:

 

Because this class was just restructured by me, I thought, after using return, did I directly return the request like the spring interceptor?

 

So I rolled back the changes to this AOP class and found that the problem still exists.

This is amazing. The filter is normal and the changes to AOP are also rolled back. However, the Controller still cannot get in.

 

Ii. Cause

After some hard work, I lost my nap. It was very sleepy and I had no idea at the moment. Then a request was initiated to find the problem.

However, this time, I clicked the following log: (if the image is small, you can open the new tab to make it clear)

 

Haha? Is there a log here? At first glance, it really has something to do with this issue.

Now, the truth is clear.

When I took over the code, I wondered why many controllers have to do this:

 

At that time, I felt that I had taken over the code and thought that "Everything was successful." It may be because the previous buddies thought that this controller class may have concurrency problems and thread synchronization problems.

Add it. I can't move it. Although it feels a little different from before, there should be no major problems. The controller is not a singleton.

It is no big deal to create more.

The above is my previous mentality. Until this problem occurs.

 

Some students may not understand this. I will explain that when the Controller adds prototype, it will not create the Controller Singleton bean when the application starts, instead, create the instance when necessary.

When is the request necessary? After Spring MVC's handlermappingadapter analyzes and routes the request to the Controller, it is found that the scope of bean definition of the bean is prototype,

The Controller is created.

At the time of creation, it is time to inject services. However, the service package is not within the scanning range (I added a package separately, and our configuration file is not configured to scan only the base package, is to scan a series of specific packages ).

If this bean is not scanned, the dependency is not met. Of course, this controller fails to be created.

Therefore, an exception is thrown.

 

3. Why is the exception log recorded here?

Then let's talk about the problem. After the reason is clear, I tried again: Remove prototype and restart the application. As a result, this startup failed.

Then, you may have doubts. If you have an exception, what does localhost. Log mean by logging? Generally (such as me), the main focus is on the console output panel of the application.

I just thought, this exception information, hit this file? Why? What does this file do?

I checked some information and found that Google has less information than Baidu... Let's take a look.

Localhost. {yyyy-mm-dd}. Log mainly refers to the logs output by Tomcat for unhandled exceptions during application initialization (listener, filter, Servlet ).

In this case, spring fails to create a bean and an exception is thrown directly to Tomcat. Below is the stack.

02-nov-2018 16:36:17. 882 severe [http-nio-8080-exec-4] Org. apache. catalina. core. applicationdispatcher. invoke servlet. service () for servlet [dispatcherservlet] threw exception Org. springframework. beans. factory. nosuchbeandefinitionexception: no qualifying bean of Type 'com. CEIEC. service. mqmsg. mqmsgfactoryutils 'available: expected at least 1 bean which qualifies as autowire candidate. dependency Annotations: {@ Org. springframework. beans. factory. annotation. autowired (required = true)} At Org. springframework. beans. factory. support. defaultlistablebeanfactory. raisenomatchingbeanfound (defaultlistablebeanfactory. java: 1486) at Org. springframework. beans. factory. support. defaultlistablebeanfactory. doresolvedependency (defaultlistablebeanfactory. java: 1104) at Org. springframework. beans. factory. support. defaultlistablebeanfactory. resolvedependency (defaultlistablebeanfactory. java: 1066) at Org. springframework. beans. factory. annotation. autowiredannotationbeanpostprocessor $ autowiredfieldelement. inject (autowiredannotationbeanpostprocessor. java: 585) at Org. springframework. beans. factory. annotation. injectionmetadata. inject (injectionmetadata. java: 88) at Org. springframework. beans. factory. annotation. autowiredannotationbeanpostprocessor. postprocesspropertyvalues (autowiredannotationbeanpostprocessor. java: 366) at Org. springframework. beans. factory. support. abstractautowirecapablebeanfactory. populatebean (abstractautowirecapablebeanfactory. java: 1264) at Org. springframework. beans. factory. support. abstractautowirecapablebeanfactory. docreatebean (abstractautowirecapablebeanfactory. java: 553) at Org. springframework. beans. factory. support. abstractautowirecapablebeanfactory. createbean (abstractautowirecapablebeanfactory. java: 483) at Org. springframework. beans. factory. support. abstractbeanfactory. dogetbean (abstractbeanfactory. java: 325) at Org. springframework. beans. factory. support. abstractbeanfactory. getbean (abstractbeanfactory. java: 197) at Org. springframework. web. method. handlermethod. createwithresolvedbean (handlermethod. java: 259) at Org. springframework. web. servlet. handler. abstracthandlermethodmapping. gethandlerinternal (abstracthandlermethodmapping. java: 323) at Org. springframework. web. servlet. handler. abstracthandlermethodmapping. gethandlerinternal (abstracthandlermethodmapping. java: 61) at Org. springframework. web. servlet. handler. abstracthandlermapping. gethandler (fig. java: 352) at Org. springframework. web. servlet. dispatcherservlet. gethandler (dispatcherservlet. java: 1156) at Org. springframework. web. servlet. dispatcherservlet. dodispatch (dispatcherservlet. java: 936) at Org. springframework. web. servlet. dispatcherservlet. doservice (dispatcherservlet. java: 897) at Org. springframework. web. servlet. frameworkservlet. processrequest (frameworkservlet. java: 970) at Org. springframework. web. servlet. frameworkservlet. dopost (frameworkservlet. java: 872) at javax. servlet. HTTP. httpservlet. service (httpservlet. java: 661) at Org. springframework. web. servlet. frameworkservlet. service (frameworkservlet. java: 846) at javax. servlet. HTTP. httpservlet. service (httpservlet. java: 742) at Org. apache. catalina. core. applicationfilterchain. internaldofilter (applicationfilterchain. java: 231) at Org. apache. catalina. core. applicationfilterchain. dofilter (applicationfilterchain. java: 166) at Org. apache. tomcat. websocket. server. wsfilter. dofilter (wsfilter. java: 52) at Org. apache. catalina. core. applicationfilterchain. internaldofilter (applicationfilterchain. java: 193) at Org. apache. catalina. core. applicationfilterchain. dofilter (applicationfilterchain. java: 166) at Org. apache. catalina. core. applicationdispatcher. invoke (applicationdispatcher. java: 728) at Org. apache. catalina. core. applicationdispatcher. processrequest (applicationdispatcher. java: 467) at Org. apache. catalina. core. applicationdispatcher. doforward (applicationdispatcher. java: 392) at Org. apache. catalina. core. applicationdispatcher. forward (applicationdispatcher. java: 311) at com. CEIEC. webService. filter. webrequestfilter. dofilterinternal (webrequestfilter. java: 116) at Org. springframework. web. filter. onceperrequestfilter. dofilter (onceperrequestfilter. java: 107) at Org. springframework. web. filter. delegatingfilterproxy. invokedelegate (delegatingfilterproxy. java: 346) at Org. springframework. web. filter. delegatingfilterproxy. dofilter (delegatingfilterproxy. java: 262) at Org. apache. catalina. core. applicationfilterchain. internaldofilter (applicationfilterchain. java: 193) at Org. apache. catalina. core. applicationfilterchain. dofilter (applicationfilterchain. java: 166) at Org. springframework. orm. hibernate4.support. opensessioninviewfilter. dofilterinternal (opensessioninviewfilter. java: 151) at Org. springframework. web. filter. onceperrequestfilter. dofilter (onceperrequestfilter. java: 107) at Org. apache. catalina. core. applicationfilterchain. internaldofilter (applicationfilterchain. java: 193) at Org. apache. catalina. core. applicationfilterchain. dofilter (applicationfilterchain. java: 166) at Org. springframework. web. filter. characterencodingfilter. dofilterinternal (characterencodingfilter. java: 197) at Org. springframework. web. filter. onceperrequestfilter. dofilter (onceperrequestfilter. java: 107) at Org. apache. catalina. core. applicationfilterchain. internaldofilter (applicationfilterchain. java: 193) at Org. apache. catalina. core. applicationfilterchain. dofilter (applicationfilterchain. java: 166) at Org. apache. catalina. core. standardwrappervalve. invoke (standardwrappervalve. java: 199) at Org. apache. catalina. core. standardcontextvalve. invoke (standardcontextvalve. java: 96) at Org. apache. catalina. authenticator. authenticatorbase. invoke (authenticatorbase. java: 478) at Org. apache. catalina. core. standardhostvalve. invoke (standardhostvalve. java: 140) at Org. apache. catalina. valves. errorreportvalve. invoke (errorreportvalve. java: 81) at Org. apache. catalina. valves. abstractaccesslogvalve. invoke (abstractaccesslogvalve. java: 650) at Org. apache. catalina. core. standardenginevalve. invoke (standardenginevalve. java: 87) at Org. apache. catalina. connector. coyoteadapter. service (coyoteadapter. java: 342) at Org. apache. coyote. http11.http11processor. service (http11processor. java: 803) at Org. apache. coyote. abstractprocessorlight. process (abstractprocessorlight. java: 66) at Org. apache. coyote. abstractprotocol $ connectionhandler. process (abstractprotocol. java: 868) at org.apache.tomcat.util.net. nioendpoint $ socketprocessor. dorun (nioendpoint. java: 1459) at org.apache.tomcat.util.net. socketprocessorbase. run (socketprocessorbase. java: 49) at java. util. concurrent. threadpoolexecutor. runworker (threadpoolexecutor. java: 1149) at java. util. concurrent. threadpoolexecutor $ worker. run (threadpoolexecutor. java: 624) at Org. apache. tomcat. util. threads. taskthread $ wrappingrunnable. run (taskthread. java: 61) at java. lang. thread. run (thread. java: 748)

 

At the beginning, I felt that it was a bit wrong with spring. I directly threw the exception to Tomcat. However, when a bean fails to be created at startup, it is directly thrown to Tomcat and tomcat to the user for processing.

You can understand this. But I still feel: Why can't I create a log in the application? Make a log and at least find the problem.

 

4. Tomcat logs

Here is a brief introduction to several types of logs.

Catalina. Out, we all know that both stdout and stderr will hit this. (That is, the application's system. Out. println, and Tomcat's own stdout and stderr)

Then there are several other types of logs under the logs directory, where the red arrow points to, very important (of course, I used to think localhost_access.log is important. After today's lesson ...) :

 

Where are these logs configured?

Logging. properties under conf In the tomcat installation directory.

 

However, Tomcat's complete server cannot be so easy.

Actually, (the link will be provided at the end of the official document ):

1. Global mode:

That is, logging. properties under conf in Catalina. base. You can also specify it through java. util. Logging. config. file at startup. If the file is unreadable or not configured, the file under lib under javahome will be used.

2. In the application, the default location is WEB-INF/classes/logging. properties.

 

The default log framework of Tomcat is Juli. Modified Based on Java. util. logging. You can also switch to log4j, which is not cumbersome. The link is as follows:

Https://tomcat.apache.org/tomcat-8.0-doc/logging.html#Using_Log4j

 

5. Experiences

Here, let us remember again: if a bug occurs, remember to look at localhost. log.

 

See:

Tomcat log Catalina. log & localhost. Log

Https://tomcat.apache.org/tomcat-8.0-doc/logging.html #

 

Murphy's Law: when you think there may be a bug in a place, there will be a bug in this place-learn about localhost. log which is rarely noticed by Tomcat.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.