Deployment of enterprise applications and other administrative tasks in WebSphere Application Servers environments can be fully automated using Jython based scripts. Depending on the size of the environment and the level of automation these scripts may become rather big and complex. In case your management has the requirement that certain or all calls to the WebSphere API have be recorded into an audit log – e.g. calls that modify the environment configuration (e.g. installation of EARs, creation/deletion of data sources, etc.) or the runtime state of WebSphere processes (e.g. stop/start/restart of servers, applications, or listeners) – have a look at the following solution.
Save the code listing below into a file called wslogging.py.
from __future__ import nested_scopes def __wrapMethod(methodToWrap, objectName): def __inner(*args): # YOUR LOGGING CODE GOES HERE print "wsadmin: %s.%s%s" % (objectName, methodToWrap.__name__, args[1:]) return methodToWrap(*args) return __inner # calls to the listed methods of these admin modules will not be logged __methodCallsToIgnore = { "AdminConfig" : [ "attributes", "defaults", "getid", "list", "listTemplates", "show", "showall", "showdump", "types" ], "AdminControl" : [ "queryNames" ] } def __logwsadmin(adminClass, adminObjectName): # iterate over all features of the given admin class for (featureName, featureInstance) in adminClass.__dict__.items(): # ignore class features that are no methods, e.g. properties if not callable(featureInstance): continue # ignore internal methods if featureName.startswith("_"): continue # ignore toString() method if featureName == "toString": continue # ignore methods from logging blacklist if __methodCallsToIgnore.has_key(adminObjectName) and featureName in __methodCallsToIgnore[adminObjectName]: continue # replace methods with logging delegates adminClass.__dict__[featureName] = __wrapMethod(featureInstance, adminObjectName) # iterate over all loaded modules and enhance modules whose name starts with Admin for (varName, varInstance) in locals().items(): if varName.startswith("Admin"): __logwsadmin(varInstance.__class__, varName)
You can now transparently enable logging of calls to WebSphere admin objects in your custom Jython scripts using this launch pattern:
wsadmin.sh -lang jython -profile wslogging.py -f [FILENAME_OF_YOUR_SCRIPT]
Example: