Is there a way to get a more concise / readable exception for a FileNotFoundException logged by Spring Framework for a missing File Resource?
The stack trace generated by Spring is big and ugly when the problem is just a File Resource missing:
11:25:09.757 [main] INFO c.m.m.MarketDataProvider - Starting service: MarketDataProvider
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'marketDataProvider' defined in file [C:\projects\myproj\assembly\target\myproj-dev\config\marketdata-ctx.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot load list of symbols at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at o开发者_运维问答rg.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075)
at com.myproj.marketdata.MarketDataProvider.getBean(MarketDataProvider.java:38)
at com.myproj.marketdata.MarketDataSubscriber.main(MarketDataSubscriber.java:49)
Caused by: java.lang.IllegalStateException: Cannot load list of symbols
at com.myproj.marketdata.MarketDataProvider.start(MarketDataProvider.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1544)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 9 more
Caused by: com.myproj.marketdata.SymbolLoaderException: Unable to load symbols file java.io.FileNotFoundException: File 'var/staticData/symbols.txt' not found using search path [file:/C:\projects\myproj\assembly\target\myproj-dev/]. Make sure the location is specified relative to the search path.
at com.myproj.marketdata.SymbolsFileLoader.load(SymbolsFileLoader.java:37)
at com.myproj.marketdata.MarketDataProvider.start(MarketDataProvider.java:48)
... 16 more
The problem is that this giant stack trace is difficult for our ops team to parse. They often miss the java.io.FileNotFoundException: File 'var/staticData/symbols.txt'
at the bottom of the stack.
While one obvious solution is "train your ops team to read Java exceptions," I would prefer that Spring Framework have the option to generate a more concise error for missing FileResources. Is there such an option, or will I need to code some kind of custom File Resource adapter that "pre-screens" them and verifies that they exist before passing them to Spring?
I don't think so. By the way, in your case, you are the one wrapping a FileNotFoundException
in IllegalStateException
.
The "problem" is not spring. It is the way exceptions work - they bubble up until someone catches them. Then he may choose to rethrow them or wrap them. Spring wraps a failure in the creation of a bean in a .. BeanCreationException
- quite logical.
Something that is not advisable, but that would help shorten the stacktrace, is that you don't rethrow the exception - catch a FileNotFoundException
in your init()
method and simply log it as fatal
. And leave your bean to continue its creation peacefully. That's the "catch & log" approach (not always considered a good practice ;) )
Have you considered creating your own logging implementation for this?
It isn't clear which logging system you are using, but in all of them you can create your own logging output handler. That logging handler receives the Exception instance. You can loop through the attached exceptions to find the FileNotFound exception you are looking for. If you find that, you can log it in a way that brings that particular problem right to the top. (I would print out the stack trace too, for debugging purposes, but put it below the error).
精彩评论