Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze STAF in JDK Log Framework

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to do STAF analysis in the JDK log framework, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

The example of JDK logging framework combined with the concept of STAF,STAF logging service, what is the situation in this respect? Let's start with what STAF is.

STAF (Software Testing Automation Framework) is an automated software testing framework, which can realize distributed automated software test management. We can use the Java API of the STAF library to do applications based on the STAF framework, while STAF also provides logging services. Its log service is used to record the information in the automated test process, to facilitate the recording of automated test operations in the automated testing of 24x7, and to find potential problems in automated test management scripts.

Since we can use STAF's Java API for STAF-based applications, we can also combine JDK's logging framework with STAF's logging service interface.

Listing 7 STAFLog class definition

You can see from listing 7 that the STAFLog class provides a way to store log information in STAF's log library, which can be either a local file or a JDK log library on another STAF server. This is determined by the configuration of the local STAF server. The STAFLog.log () method is only used to record log information.

To combine java API of STAF CLS with JDK logging framework, you need to do the following steps:

Create a STAF log Handler class

This class encapsulates the interface of the STAF log service API. At the same time, the Java API of STAF requires a global STAFHandle object to represent the local STAF service handle. This can be done by creating a static STAFHandle object. The code is shown below, and we define a STAFHandler class as shown in listing 8.

Listing 8. The STAFHandler class implementation

Import java.util.logging.*; import com.ibm.staf.wrapper.STAFLog; public class STAFHandler extends Handler {private String logName; private static STAFHandle stafHandle = null; public STAFHandler (String name) {configure (); logName = name;} public STAFHandler () {configure ();} @ Override public void close () throws SecurityException {if (stafHandle! = null) {try {stafHandle.unRegister () } catch (STAFException e) {/ / ignore} @ Override public void flush () {/ / nothing} @ Override public void publish (LogRecord record) {if (! isLoggable (record)) {return;} String msg; try {msg = getFormatter (). Format (record);} catch (Exception ex) {reportError (null, ex, ErrorManager.FORMAT_FAILURE); return } try {STAFLog.log (stafHandle, STAFLog.MACHINE, logName, record.getLevel (). GetName (), msg);} catch (Exception ex) {reportError (null, ex, ErrorManager.WRITE_FAILURE);}.

There are several key points when implementing the STAFHandler class:

1. Since the call to STAF API requires a STAFHandle object to represent the local STAF service, a global variable is declared in this class to store STAFHandle.

2. The close method is used to clean up system resources. The global variable STAFHandle object is released in the close method of the above code.

3. The publish method is to get the formatted message, and then directly call STAF's log API to send the log to the STAF service.

But so far, we haven't added a configuration code to the STAFHandler class to support the configuration file. Next we define a function configure whose code is shown in listing 9.

Listing 9 configuration function implementation

Private void configure () {if (stafHandle = = null) {try {stafHandle = new STAFHandle ("my application");} catch (STAFException e) {reportError ("registe staf handle error", e, ErrorManager.OPEN_FAILURE);}} LogManager manager = LogManager.getLogManager (); String cname = getClass (). GetName (); / / set staflog name logName = manager.getProperty (cname + ".name"); if (logName = = null) logName = "demo.staflog" / / set formatter String sformatter = manager.getProperty (cname + ".formatter"); Formatter formatter = null; if (sformatter! = null) {try {formatter = (Formatter) Class.forName (sformatter). NewInstance ();} catch (Exception e) {/ / ignore}} setFormatter (formatter = = null? New STAFFormatter (): formatter); / / set level String sLevel = manager.getProperty (cname + ".level"); Level level = null; if (sLevel! = null) {try {level = STAFLevel.parse (sLevel);} catch (Exception e) {/ / ignore}} setLevel (level = = null? STAFLevel.DEBUG: level);}

1. To initialize STAF API, you need to register the STAFHandle object. And the registration can only be performed once. We decide whether to register the object based on the value of the global variable stafHandle.

2. JDK's log framework has a global singleton management class STAFManager, which is used to manage log classes and provides a member function getProperty to read log configuration files.

It is important to note that because our log level is custom, the Level object is generated by our custom Level class STAFLevel.

4. We can also define the attributes we need. For example, in listing 9, we define a .name attribute that stores the STAF log name and reads the .name attribute from the configuration file through the getProperty function.

The above is how to analyze STAF in the JDK log framework. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report