ADFLogger日志与weblogic 日志配置与应用

最近要用到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种

日志按严重性由高到低的顺序提供了如下的级别

  1. SEVERE 严重的错误,导致系统中止。期望这类信息能立即显示在状态控制台上
  2. WARNING 潜在问题的级别,它是不符合预期的状态但还不至于成为“错误”,例如使用了废弃的API等等。期望这类信息能立即显示在状态控制台上
  3. INFO 运行时产生的有意义的一些信息,主要用于报告消息的目的。期望这类信息能立即显示在状态控制台上
  4. CONFIG 静态配置消息,用来输出一些系统配置信息。期望这类信息仅被写入日志文件中
  5. FINE 指示提供跟踪信息,简单输出一些跟踪信息。期望这类信息仅被写入日志文件中
  6. FINER 指示提供一条相当详细的跟踪消息。期望这类信息仅被写入日志文件中
  7. FINEST 指示提供一条最详细的跟踪消息。期望这类信息仅被写入日志文件中

日志记录方法划分为 5 个主要类别:

  1. 一系列的 "log" 方法,这种方法带有日志级别、消息字符串,以及可选的一些消息字符串参数。
  2. 一系列的 "logp" 方法(即 "log precise"),其与 "log" 方法相似,但是带有显式的源类名称和方法名称。
  3. 一系列的 "logrb" 方法(即 "log with resource bundle"),其与 "logp" 方法相似,但是带有显式的在本地化日志消息中使用的资源包名称。
  4. 还有跟踪方法条目("entering" 方法)、方法返回("exiting" 方法)和抛出异常("throwing" 方法)的便捷方法。
  5. 最后,还有一系列在非常简单的情况下(如开发人员只想为给定的日志级别记录一条简单的字符串)使用的便捷方法。这些方法按标准级别名称命名("severe"、"warning"、"info" 等等),并带有单个参数,即一个消息字符串。

 

如severe级别的方法有如下:

  1. severe(java.lang.String msg)
  2. severe(java.lang.String message, java.lang.Object param)
  3. severe(java.lang.String message, java.lang.Object[] params)
  4. severe(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String msg)
  5. severe(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String msg, java.lang.Object param1)
  6. severe(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String msg, java.lang.Object[] params)
  7. severe(java.lang.String message, java.lang.Throwable t)
  8. severe(java.lang.Throwable t)

 

其它的方法和方法的详细信息可以查看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 theIntegratedWeblogicServer and select  Configure Oracle Diagnostic Logging for "IntegratedWeblogicServer".

第三 From the Log window (usually docked underneath your editor) pull down the Actions menu  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:

Image of the Logging configuration Screen

 So what we see here is a hierarchy of 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 labeled Hide Transient Loggers, well you can guess what that does now.

Step 3 - Configure Some Logging

 The rest is all fairly self explanatory. 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:

  • odl-handler
  • wls-domain
  • console

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 to Tools Preferences and 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 the sample I'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>

 

时间有点急,写的有点乱,有时间再整理!

 

 

 

 

           

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值