Thursday, May 31, 2012

WLST Script changing logfile location

While I was migrating Forms6i to Forms11g patch set #5, the configuration tool of Forms11g is a bit strict. In the silent install it is not possible to set de locations of the log files. Here is the script that will set new filename locations of all the Managed Servers and Admin Server in the domain. It also set the filename location of all the ODL logging.

fmwlogging.py:
#
# usage:
#
# ${ORACLE_HOME}/common/bin/wlst.sh [domain-name] [admin-server-url] [password]
#

import os
import sys
import traceback
import getopt

loggingEnabled=True
# rotationType="none", "bySize", "byTime"
# logFileSeverity="Trace", "Debug", "Info", "Notice", "Warning"
# rotateLogOnStartup=False, True
rotationType="none"
logFileSeverity="Warning"
rotateLogOnStartup=True

def editMode():
  edit()
  startEdit()
 
def editActivate():
  save()
  activate(block="true")
 
def updateLog(domain_name, logMB, logType):
  print "**** Start updateLog()" 

  fileName = ""
  if logType == "Access":
    logMB.setLoggingEnabled(loggingEnabled)
    fileName = "/data/logs/" + domain_name + "/" + logMB.getName() + "_access.log"
  elif logType == "Server":
    fileName = "/data/logs/" + domain_name + "/" + logMB.getName() + ".log"
  elif logType == "Datasource":
    fileName = "/data/logs/" + domain_name + "/" + logMB.getName() + "_datasource.log"
  elif logType == "Domain":
    logMB.setLogFileSeverity(logFileSeverity)
    fileName = "/data/logs/" + domain_name + "/" + domain_name + ".log"

  print "**** " + logType + " " + fileName
  logMB.setFileName(fileName)
  logMB.setRotationType(rotationType)


  logMB.setRotateLogOnStartup(rotateLogOnStartup)

  print "**** Finished updateLog()" 
  
def changeLogPath(domain_name): 
  print "**** Start changeLogPath()"

  domainConfig()
  editMode()

  logMB = getMBean("/Log/" + domain_name)
  updateLog(domain_name, logMB, logType="Domain")

  editActivate()

  servers = cmo.getServers()
 
  editMode()
  for server in servers:
    serverName = server.getName()

    logMB = getMBean("/Servers/" + serverName + "/Log/" + serverName)
    updateLog(domain_name, logMB, "Server")

    httpLogMB = getMBean("/Servers/" + serverName + "/WebServer/" + serverName + "/WebServerLog/" + serverName)
    updateLog(domain_name, httpLogMB, "Access")

    DSLogMB = getMBean("/Servers/" + serverName + "/DataSource/" + serverName + "/DataSourceLogFile/" + serverName)
    updateLog(domain_name, DSLogMB, "Datasource")

  editActivate()
  print "**** Finished changeLogPath()"

def usage(): 
  print "Usage" 
  print "./fmwlogging.py    $1"

def parse_input():
  print "***** Start parse_input()"

  domain_name = sys.argv[1]
  admin_server = sys.argv[2]
  admin_password = sys.argv[3]

  return domain_name, admin_server, admin_password

  print "***** Start parse_input()"
                
# Connectionsettings
def connectToServer(username, password, adminurl):
  print "***** Start connectToServer()"

  connect(username, password, adminurl)

  print "***** Finished connectToServer()"

#Definition to disconnect from a server
def disconnectFromServer():
  print "***** Start disconnectFromServer()"

  disconnect()

  print "***** Finished disconnectFromServer()"
  exit()

def changeODLPath(domain_name):
  print "***** Start changeODLPath()"

  domainConfig()
  managedServers=cmo.getServers()

  #Get Runtime for our server
  for managedServer in managedServers:
    sname=managedServer.getName()
    path = "/Servers/" + sname
    cd(path)

    print "***** Changing server: " + sname
    lh = listLogHandlers(target=sname)
    for l in lh:
      lname = l.get("name")
      lprops = l.get("properties")
      removeprops=[]
      for prop in lprops:
        if prop.get("name") == "maxFileSize":
          removeprops.append("maxFileSize")
        elif prop.get("name") == "maxLogSize":
          removeprops.append("maxLogSize")

      odlfile = "/data/logs/" + domain_name + "/" + sname + "-" + lname + "-diagnostic.log"
      configureLogHandler(target=sname, name=lname, path=odlfile,removeProperty=removeprops)

  print "***** Finished changeODLPath()"

def main(domain_name, admin_server, admin_password):
  print "***** Start main()"
   
  connectToServer("weblogic", admin_password, admin_server)

  # do the change the ODL log files on all servers ( Admin, managed)
  #  changeODLPath(domain_name)

  # do the change the standard log files on all servers ( Admin, managed)
  changeLogPath(domain_name)

  # Calling disconnectFromServer definition with no arguments
  disconnectFromServer()

  print "***** Finished main()"
   
try:  
  print "** start()"

  domain_name, admin_server, admin_password = parse_input()
  main(domain_name, admin_server, admin_password)

  print "** finished()"
 
except  Exception, (e):
  print "ERROR: An unexpected error occurred!"
  traceback.print_exc()
  dumpStack()
  print "ERROR: Failed to configure fmw diagnostic logging " + domain_name + "!!"

#EOF


Reference: Weblogic MBean Documentation