最近要用到ADF日志,所以找了一下文档,自我理解了一下,记录在案,以备后用
之前没做到日志,所以对日志也不是很了解,所以这次看资料也辛苦的
在ADF代码中根据需求记录日志信息,确保日志信息在内容上和反应问题的严重程度上的准确而且恰当,是程序员需要做好的重要任务
要用ADF日志,首先要引包,但是JEV工具已集成了日志包,剩下的就是代码中直接写了,其中最重要的一句就是创建ADFLogger
如private static ADFLogger logger=ADFLogger.createADFLogger(DataServiceBase.class);
其中createADFLogger()方法有多个构造函数,大概有6个,
然后就可以在代码中编写要输出的日志了,其中有两种方式。
方式一:logger.log(Level.INFO,param);
方式二:logger.info(param);
其中param为字符串类型的要输出的日志。
例如servere消息
log.log(Level.SEVERE, "exception", nfe);
og.severe("Unexpected exception doing complex thing",nfe);
日志Level,分为5种
日志按严重性由高到低的顺序提供了如下的级别
日志记录方法划分为 5 个主要类别:
如severe级别的方法有如下:
其它的方法和方法的详细信息可以查看Java Doc
在代码中编写好了,可是怎么运行和输出呢,输出又分为输出到控制台和日志文件,运行又分为在测试时JDV自带的WLS和独产的WLS服务器内。
private static ADFLogger log=ADFLogger.createADFLogger(TestService.class);
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String aa="sss";
try {
int n = Integer.valueOf(aa);
} catch (NumberFormatException nfe) {
System.out.println(nfe.getMessage());
log.log(Level.SEVERE, "exception", nfe);
log.severe("Unexpected exception doing complex thing",
nfe);
}
log.log(Level.WARNING, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
以上代码首先要在JDV开发工具中调试使用的话,先打开文件loggin.xml配置文件,打开方式有三种
第一,直接在按照路径直接找到,大概位置如下C:\Users\Administrator\AppData\Roaming\JDeveloper\system11.1.1.7.40.64.93\DefaultDomain\config\fmwconfig\servers\DefaultServer,这个只是我电脑上的,看了以后也许你就知道在哪了
There are two ways to get to the logging configuration screen.
第二 Open the Application Server Navigator (Ctrl + Shift + G), right mouse click on theIntegratedWeblogicServerand select Configure Oracle Diagnostic Logging for "IntegratedWeblogicServer".
第三 From the Log window (usually docked underneath your editor) pull down theActionsmenu and selectConfigure Oracle diagnostic Logging.
找到后在配置文件中修改消息类别,不自己写了,直接COPY一下
Both approaches open the same editor on top of the logging.xml file. Here it is, I've expanded the first couple of levels in the tree to make it interesting:
So what we see here is ahierarchyof all the packages in the running app server. Notice that some of the icons are green, these are packages or classes that have a presence in the logging.xml already and some are yellow. This latter group are transient, that is, we can temporarily set up logging on that package or class for the duration of the application server, but once the application server shuts down all the logging configuration will be reverted.. You'll notice the checkbox at the top of this screen that is labeledHide Transient Loggers, well you can guess what that does now.
The rest is all fairly selfexplanatory. To configure logging at any level you click on the level column and from the pull down select the level you want to look at. In the image above you can see that I've set the logging for the Root Logger to the INFO level. This implicitly means that any child of that node (everything in this case because it's the root) will also be logging at the INFO level unless it explicitly overrides. Normally your root logger would log at WARNING or SEVERE, you just want to keep an eye out for any and all problems at that level.
So perhaps we wanted to switch on CONFIG level logging for everything under the oracle.demo root we would just set that in the level column. Of course with Config level Info messages would still be printed out as well. So you can be as fine grained about this as you want, controlling the logging level on a class by class or package by package basis.
If need to set up a logger on a class / package that is not visible in the structure (yet) you can use the green add (+) button at the top left of the editor to add a new one in. The loggers you add can be persistent (stored in the logging.xml) or transient (discarded at the end of the session) Note that transient loggers can only be created when you are actually running the internal WLS instance.
As well as the logging levels it is also possible to configure the handlers. In logging, the handler is the thing that takes your log message and does something with it - prints it to the console, writes it to a file etc. If you select the root logger in the editor here you will see that it defines three handlers listed in the Handler Declaration section of the screen at the bottom of the editor:
The first two would be the default for a standalone WLS instance, but the console we've automatically added for you in the case of the embedded WLS so that you will be able to see the output of your carefully placed logging commands in the log window of the IDE (Hint-Now is a good time to go toTools>Preferencesand increase the size of your logging buffer). I recommend that you do not change these handlers and just continue to inherit from the root. The reason for this will become evident in a later installment.
For example in thesampleI'm running here I have a few CONFIG and INFO level logging calls in the code and this generates the following output on the console:
<Library> <initSampleData> Setting up Library sample data <MainPageBackingBean> <<init>> Creating a new Instance <MainPageBackingBean> <handleSlotCartridgeMove> Dropping Module : 1 : Left-12 <MainPageBackingBean> <<init>> Creating a new Instance <MainPageBackingBean> <handleSlotCartridgeMove> Dropping Module : 1 : Left-14 <TaskFlowUtils> <diagnosticInitializer> Key:"displaySlot" Value:"Module : 1 : Left-9" Type:[oracle.demo.whippet.model.Slot]
Cryptic yes - but I know what it all means in the context of this application and it's exactly the information that I need to monitor what's going on. It tells me that by backing bean classes are being created for every request, and in this case the drophander for some drag and drop processing has received the payload I expect. Finally one of my standard pieces of instrumentation which I embed in my task flow initializer call is reporting all of the parameters passed to it. All good stuff.
Next time we'll take a look at this output information in more detail and explore some of the real power of this logging once you start to relate all of this information together into sequences of events.
接下来就是,添加运行配置器的JVM选项
运行配置器的JVM选项中添加如下的选项
-Djbo.debugoutput=adflogger -Djbo.adflogger.level=FINE
添加了这个选项之后,JDeveloper控制台中也会打印出日志信息,如果希望将日志信息输出到一个日志文件,可以配置logging.xml文件,添加 log_handler 来输出重定向,具体可以看上面给出的示例
二 如果在独立服务器上的话,就需要直接进行EM控制台进行日志配置
登陆EM ,打开内部应用程序,找到目标应用,右键点击,日志选日志配置,点开日志文件,然后选择创建日志,
在创建日志中,选择日志路径和生成记录地时间段,有效期等。
其实,在配置好后,打开服务器上的logging.xml,会发现生成了和DEV中一样的代码,贴一下如下
<log_handler name='odl-handler' class='oracle.core.ojdl.logging.ODLHandlerFactory' filter='oracle.dfw.incident.IncidentDetectionLogFilter'>
<property name='path' value='${domain.home}/servers/${weblogic.Name}/logs/${weblogic.Name}-diagnostic.log'/>
<property name='maxFileSize' value='10485760'/>
<property name='maxLogSize' value='104857600'/>
<property name='rotationFrequency' value='daily'/>
<property name='retentionPeriod' value='week'/>
<property name='encoding' value='UTF-8'/>
<property name='useThreadName' value='true'/>
<property name='supplementalAttributes' value='J2EE_APP.name,J2EE_MODULE.name,WEBSERVICE.name,WEBSERVICE_PORT.name,composite_instance_id,component_instance_id,composite_name,component_name,DSID'/>
</log_handler>
时间有点急,写的有点乱,有时间再整理!